Compare commits
14 Commits
5129cca099
...
master
Author | SHA1 | Date | |
---|---|---|---|
180157ea91 | |||
78b58d4900 | |||
b8524880a5 | |||
b86e69b60e | |||
446834c100 | |||
3b20fbd66f | |||
f9a00578f4 | |||
3651969c33 | |||
76205250ad | |||
9fcd8c367c | |||
b6bc007590 | |||
60cd73078c | |||
1dab43ca90 | |||
e749dfc904 |
73
app/common/Controller/Aliases.php
Normal file
73
app/common/Controller/Aliases.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
namespace ProVM\Money\Common\Controller;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use ProVM\Common\Factory\Model as ModelFactory;
|
||||
use ProVM\Common\Define\Controller\Json;
|
||||
use ProVM\Money\Alias;
|
||||
|
||||
class Aliases {
|
||||
use Json;
|
||||
|
||||
public function __invoke(Request $request, Response $response, ModelFactory $factory): Response {
|
||||
$aliases = $factory->find(Alias::class)->array();
|
||||
$output = compact('aliases');
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function get(Request $request, Response $response, ModelFactory $factory, $alias_id): Response {
|
||||
$alias = $factory->find(Alias::class)->one($alias_id);
|
||||
$output = [
|
||||
'get_data' => compact('alias_id'),
|
||||
'alias' => null
|
||||
];
|
||||
if ($alias) {
|
||||
$output['alias'] = $alias->asArray();
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function add(Request $request, Response $response, ModelFactory $factory): Response {
|
||||
$post = json_decode($request->getBody()->getContents());
|
||||
$aliases = [];
|
||||
if (is_array($post)) {
|
||||
foreach ($post as $obj) {
|
||||
if (!is_object($obj)) {
|
||||
continue;
|
||||
}
|
||||
$aliases []= Alias::add($factory, $obj);
|
||||
}
|
||||
} else {
|
||||
$aliases []= Alias::add($factory, $post);
|
||||
}
|
||||
$output = [
|
||||
'post_data' => $post,
|
||||
'aliases' => $aliases
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function edit(Request $request, Response $response, ModelFactory $factory, $alias_id) {
|
||||
$post = json_decode($request->getBody()->getContents());
|
||||
$output = [
|
||||
'get_data' => compact('alias_id'),
|
||||
'post_data' => $post
|
||||
];
|
||||
$alias = $factory->find(Alias::class)->one($alias_id);
|
||||
$edited = false;
|
||||
if ($alias) {
|
||||
$edited = $alias->edit($post);
|
||||
$output['alias'] = $alias->asArray();
|
||||
$output['edited'] = $edited;
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function delete(Request $request, Response $response, ModelFactory $factory, $alias_id): Response {
|
||||
$alias = $factory->find(Alias::class)->one($alias_id);
|
||||
$output = ['get_data' => compact('alias_id'), 'alias' => null, 'deleted' => false];
|
||||
if ($alias) {
|
||||
$output['alias'] = $alias->asArray();
|
||||
$status = $alias->delete();
|
||||
$output['deleted'] = $status;
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
@ -72,6 +72,49 @@ class Currencies {
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function getAliases(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
|
||||
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||
$output = [
|
||||
'get_data' => compact('currency_id'),
|
||||
'currency' => null,
|
||||
'aliases' => []
|
||||
];
|
||||
if ($currency) {
|
||||
$output['currency'] = $currency->asArray();
|
||||
if ($currency->aliases()) {
|
||||
$output['aliases'] = array_map(function($item) {
|
||||
return $item->asArray();
|
||||
}, $currency->aliases());
|
||||
}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function addAliases(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
|
||||
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||
$post = json_decode($request->getBody()->getContents());
|
||||
$output = [
|
||||
'get_data' => compact('currency_id'),
|
||||
'post_data' => $post,
|
||||
'currency' => null,
|
||||
'aliases' => []
|
||||
];
|
||||
if ($currency) {
|
||||
$output['currency'] = $currency->asArray();
|
||||
$aliases = [];
|
||||
if (is_array($post)) {
|
||||
foreach ($post as $obj) {
|
||||
if (!is_object($obj)) {
|
||||
continue;
|
||||
}
|
||||
$aliases []= $currency->addAlias($obj);
|
||||
}
|
||||
} else {
|
||||
$aliases []= $currency->addAlias($post);
|
||||
}
|
||||
$output['aliases'] = $aliases;
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function getValues(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
|
||||
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||
$output = [
|
||||
@ -173,4 +216,65 @@ class Currencies {
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
|
||||
public function find(Request $request, Response $response, ModelFactory $factory, $query): Response {
|
||||
$currency = Currency::find($factory, $query);
|
||||
$output = [
|
||||
'get_data' => compact('query'),
|
||||
'currency' => null
|
||||
];
|
||||
if ($currency) {
|
||||
$output['currency'] = $currency->asArray();
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function findValues(Request $request, Response $response, ModelFactory $factory, $query): Response {
|
||||
$currency = Currency::find($factory, $query);
|
||||
$output = [
|
||||
'get_data' => compact('query'),
|
||||
'currency' => null,
|
||||
'values' => []
|
||||
];
|
||||
if ($currency) {
|
||||
$output['currency'] = $currency->asArray();
|
||||
if ($currency->values()) {
|
||||
$output['values'] = array_map(function($item) {
|
||||
return $item->asArray();
|
||||
}, $currency->values());
|
||||
}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function findLatest(Request $request, Response $response, ModelFactory $factory, $query): Response {
|
||||
$currency = Currency::find($factory, $query);
|
||||
$output = [
|
||||
'get_data' => compact('currency_id'),
|
||||
'currency' => null,
|
||||
'value' => null
|
||||
];
|
||||
if ($currency) {
|
||||
$output['currency'] = $currency->asArray();
|
||||
if ($currency->latest()) {
|
||||
$output['value'] = $currency->latest()->asArray();
|
||||
}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function findSources(Request $request, Response $response, ModelFactory $factory, $query): Response {
|
||||
$currency = Currency::find($factory, $query);
|
||||
$output = [
|
||||
'get_data' => compact('query'),
|
||||
'currency' => null,
|
||||
'sources' => []
|
||||
];
|
||||
if ($currency) {
|
||||
$output['currency'] = $currency->asArray();
|
||||
if ($currency->sources()) {
|
||||
$output['sources'] = array_map(function($item) {
|
||||
return $item->asArray();
|
||||
}, $currency->sources());
|
||||
}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
||||
|
@ -4,83 +4,14 @@ namespace ProVM\Money\Common\Controller;
|
||||
use Psr\Container\ContainerInterface as Container;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use GuzzleHttp\ClientInterface as Client;
|
||||
use Carbon\Carbon;
|
||||
use ProVM\Common\Define\Controller\Json;
|
||||
use ProVM\Common\Factory\Model as ModelFactory;
|
||||
use ProVM\Money\Currency;
|
||||
use ProVM\Money\Source;
|
||||
use ProVM\Money\Value;
|
||||
use ProVM\Money\Common\Service\Update as Updater;
|
||||
|
||||
class Update {
|
||||
use Json;
|
||||
|
||||
protected function get(Client $client, string $url, bool $exception = true) {
|
||||
$res = $client->get($url);
|
||||
if ($res->getStatusCode() < 200 or $res->getStatusCode() >= 300) {
|
||||
if ($exception) {
|
||||
throw new \Exception('Url ' . $url . ' not connected.');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return json_decode($res->getBody());
|
||||
}
|
||||
|
||||
protected function baseMap($unit) {
|
||||
$map = [
|
||||
'Dólar' => 'US Dollar',
|
||||
'Pesos' => 'Peso Chileno'
|
||||
];
|
||||
return $map[$unit] ?? $unit;
|
||||
}
|
||||
|
||||
public function __invoke(Request $request, Response $response, Client $client, ModelFactory $factory, Container $container): Response {
|
||||
ini_set('max_execution_time', 300);
|
||||
|
||||
$sources = $factory->find(Source::class)->many();
|
||||
$date = Carbon::now();
|
||||
$output = ['count' => 0, 'values' => []];
|
||||
foreach ($sources as $source) {
|
||||
$url = str_replace([
|
||||
'{year}',
|
||||
'{month}',
|
||||
'{day}',
|
||||
'{hour}',
|
||||
'{minute}',
|
||||
'{second}'
|
||||
], [
|
||||
$date->year,
|
||||
$date->month,
|
||||
$date->day,
|
||||
$date->hour,
|
||||
$date->minute,
|
||||
$date->second
|
||||
], $source->url);
|
||||
$b = $this->get($client, $url, false);
|
||||
if ($b === false) {
|
||||
continue;
|
||||
}
|
||||
$base = $factory->find(Currency::class)->where([
|
||||
['name', '%' . $this->baseMap($b->unidad_medida) . '%', 'like']
|
||||
])->one();
|
||||
if (!$base) {
|
||||
continue;
|
||||
}
|
||||
foreach ($b->serie as $info) {
|
||||
$f = Carbon::parse($info->fecha);
|
||||
$data = [
|
||||
'currency_id' => $source->currency()->id,
|
||||
'date_time' => $f->format('Y-m-d H:i:s'),
|
||||
'value' => $info->valor,
|
||||
'base_id' => $base->id
|
||||
];
|
||||
$result = Value::add($factory, $data);
|
||||
$output['values'] []= $result;
|
||||
if ($result->created === true) {
|
||||
$output['count'] ++;
|
||||
}
|
||||
}
|
||||
}
|
||||
public function __invoke(Request $request, Response $response, Updater $updater): Response {
|
||||
$output = $updater->update();
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ class Cors {
|
||||
|
||||
$response = $handler->handle($request);
|
||||
|
||||
$response = $response->withHeader('Access-Control-Allow-Origin', 'http://localhost:8080,http://localhost:8081');
|
||||
$response = $response->withHeader('Access-Control-Allow-Origin', '*');
|
||||
$response = $response->withHeader('Access-Control-Allow-Methods', implode(',', $methods));
|
||||
$response = $response->withHeader('Access-Control-Allow-Headers', $requestHeaders);
|
||||
//$response = $response->withHeader('Access-Control-Allow-Credentials', 'true');
|
||||
|
31
app/common/Middleware/Migrate.php
Normal file
31
app/common/Middleware/Migrate.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
namespace ProVM\Money\Common\Middleware;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Phinx\Wrapper\TextWrapper;
|
||||
use GuzzleHttp\ClientInterface as Client;
|
||||
use ProVM\Money\Common\Service\Update as Updater;
|
||||
|
||||
class Migrate {
|
||||
protected $phinx;
|
||||
protected $updater;
|
||||
public function __construct(TextWrapper $phinx, Updater $updater) {
|
||||
$this->phinx = $phinx;
|
||||
$this->updater = $updater;
|
||||
}
|
||||
|
||||
public function __invoke(Request $request, Handler $handler): Response {
|
||||
$query = "SHOW TABLES";
|
||||
$st = \ORM::get_db()->query($query);
|
||||
$r = $st->fetchAll(\PDO::FETCH_ASSOC);
|
||||
if (count($r) == 0) {
|
||||
$this->phinx->getMigrate();
|
||||
$this->phinx->getSeed();
|
||||
$this->updater->update();
|
||||
}
|
||||
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
90
app/common/Service/Update.php
Normal file
90
app/common/Service/Update.php
Normal file
@ -0,0 +1,90 @@
|
||||
<?php
|
||||
namespace ProVM\Money\Common\Service;
|
||||
|
||||
use GuzzleHttp\ClientInterface as Client;
|
||||
use Carbon\Carbon;
|
||||
use ProVM\Common\Factory\Model as ModelFactory;
|
||||
use ProVM\Money\Currency;
|
||||
use ProVM\Money\Source;
|
||||
use ProVM\Money\Value;
|
||||
|
||||
class Update {
|
||||
protected $factory;
|
||||
protected $client;
|
||||
public function __construct(ModelFactory $factory, Client $client) {
|
||||
$this->factory = $factory;
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
protected function get(Client $client, string $url, bool $exception = true) {
|
||||
$res = $client->get($url);
|
||||
if ($res->getStatusCode() < 200 or $res->getStatusCode() >= 300) {
|
||||
if ($exception) {
|
||||
throw new \Exception('Url ' . $url . ' not connected.');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return json_decode($res->getBody());
|
||||
}
|
||||
|
||||
protected function baseMap($unit) {
|
||||
$map = [
|
||||
'Dólar' => 'US Dollar',
|
||||
'Pesos' => 'Peso Chileno'
|
||||
];
|
||||
return $map[$unit] ?? $unit;
|
||||
}
|
||||
protected function buildUrl(Source $source) {
|
||||
$date = Carbon::now();
|
||||
return str_replace([
|
||||
'{year}',
|
||||
'{month}',
|
||||
'{day}',
|
||||
'{hour}',
|
||||
'{minute}',
|
||||
'{second}'
|
||||
], [
|
||||
$date->year,
|
||||
$date->month,
|
||||
$date->day,
|
||||
$date->hour,
|
||||
$date->minute,
|
||||
$date->second
|
||||
], $source->url);
|
||||
}
|
||||
|
||||
public function update() {
|
||||
ini_set('max_execution_time', 300);
|
||||
|
||||
$sources = $this->factory->find(Source::class)->many();
|
||||
$output = ['count' => 0, 'values' => []];
|
||||
foreach ($sources as $source) {
|
||||
$url = $this->buildUrl($source);
|
||||
$b = $this->get($this->client, $url, false);
|
||||
if ($b === false) {
|
||||
continue;
|
||||
}
|
||||
$base = $this->factory->find(Currency::class)->where([
|
||||
['name', '%' . $this->baseMap($b->unidad_medida) . '%', 'like']
|
||||
])->one();
|
||||
if ($base === false) {
|
||||
continue;
|
||||
}
|
||||
foreach ($b->serie as $info) {
|
||||
$f = Carbon::parse($info->fecha);
|
||||
$data = [
|
||||
'currency_id' => $source->currency()->id,
|
||||
'date_time' => $f->format('Y-m-d H:i:s'),
|
||||
'value' => $info->valor,
|
||||
'base_id' => $base->id
|
||||
];
|
||||
$result = Value::add($this->factory, $data);
|
||||
$output['values'] []= $result;
|
||||
if ($result->created === true) {
|
||||
$output['count'] ++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
}
|
@ -11,6 +11,7 @@
|
||||
"provm/controller": "dev-master",
|
||||
"vlucas/phpdotenv": "^5.3",
|
||||
"guzzlehttp/guzzle": "^7.3",
|
||||
"robmorgan/phinx": "^0.12.5",
|
||||
"nesbot/carbon": "^2.46"
|
||||
},
|
||||
"license": "MIT",
|
||||
@ -22,7 +23,6 @@
|
||||
],
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^8.5",
|
||||
"robmorgan/phinx": "^0.12.5",
|
||||
"odan/phinx-migrations-generator": "^5.4"
|
||||
},
|
||||
"autoload": {
|
||||
|
@ -10,7 +10,7 @@ server {
|
||||
try_files $uri /index.php$is_args$args;
|
||||
}
|
||||
|
||||
add_header 'Access-Control-Allow-Origin' 'http://localhost:8080';
|
||||
add_header 'Access-Control-Allow-Origin' "$http_origin";
|
||||
add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,OPTIONS';
|
||||
add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
|
||||
add_header 'Content-Type' 'application/json';
|
||||
|
@ -12,28 +12,28 @@ return
|
||||
'default_environment' => 'development',
|
||||
'production' => [
|
||||
'adapter' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'name' => 'production_db',
|
||||
'user' => 'root',
|
||||
'pass' => '',
|
||||
'host' => $_ENV['DB_HOST'],
|
||||
'name' => $_ENV['DB_NAME'],
|
||||
'user' => $_ENV['DB_USER'],
|
||||
'pass' => $_ENV['DB_PASSWORD'],
|
||||
'port' => '3306',
|
||||
'charset' => 'utf8',
|
||||
],
|
||||
'development' => [
|
||||
'adapter' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'name' => 'money_dev',
|
||||
'user' => 'money',
|
||||
'pass' => 'money_pass',
|
||||
'host' => $_ENV['DB_HOST'] ?? 'localhost',
|
||||
'name' => $_ENV['DB_NAME'] ?? 'money_dev',
|
||||
'user' => $_ENV['DB_USER'] ?? 'money',
|
||||
'pass' => $_ENV['DB_PASSWORD'] ?? 'money_pass',
|
||||
'port' => '3307',
|
||||
'charset' => 'utf8',
|
||||
],
|
||||
'testing' => [
|
||||
'adapter' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'name' => 'testing_db',
|
||||
'user' => 'root',
|
||||
'pass' => '',
|
||||
'host' => $_ENV['DB_HOST'],
|
||||
'name' => $_ENV['DB_NAME'],
|
||||
'user' => $_ENV['DB_USER'],
|
||||
'pass' => $_ENV['DB_PASSWORD'],
|
||||
'port' => '3306',
|
||||
'charset' => 'utf8',
|
||||
]
|
||||
|
19
app/resources/routes/api/aliases.php
Normal file
19
app/resources/routes/api/aliases.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
use ProVM\Money\Common\Controller\Aliases;
|
||||
|
||||
$app->group('/aliases', function($app) {
|
||||
$app->post('/add[/]', [Aliases::class, 'add']);
|
||||
$app->get('[/]', Aliases::class);
|
||||
$app->options('[/]', function (Request $request, Response $response): Response {
|
||||
return $response;
|
||||
});
|
||||
});
|
||||
|
||||
$app->group('/alias/{alias_id}', function($app) {
|
||||
$app->put('/edit[/]', [Aliases::class, 'edit']);
|
||||
$app->delete('/delete[/]', [Aliases::class, 'delete']);
|
||||
$app->get('[/]', [Aliases::class, 'get']);
|
||||
$app->options('[/]', function (Request $request, Response $response): Response {
|
||||
return $response;
|
||||
});
|
||||
});
|
@ -10,9 +10,16 @@ $app->group('/currencies', function($app) {
|
||||
});
|
||||
});
|
||||
|
||||
$app->group('/currency/{currency_id}', function($app) {
|
||||
$app->group('/currency/{currency_id:[0-9]+}', function($app) {
|
||||
$app->put('/edit[/]', [Currencies::class, 'edit']);
|
||||
$app->delete('/delete[/]', [Currencies::class, 'delete']);
|
||||
$app->group('/aliases', function($app) {
|
||||
$app->post('/add[/]', [Currencies::class, 'addAliases']);
|
||||
$app->get('[/]', [Currencies::class, 'getAliases']);
|
||||
$app->options('[/]', function (Request $request, Response $response): Response {
|
||||
return $response;
|
||||
});
|
||||
});
|
||||
$app->group('/values', function($app) {
|
||||
$app->get('/latest[/]', [Currencies::class, 'latestValue']);
|
||||
$app->post('/add[/]', [Currencies::class, 'addValues']);
|
||||
@ -33,3 +40,18 @@ $app->group('/currency/{currency_id}', function($app) {
|
||||
return $response;
|
||||
});
|
||||
});
|
||||
|
||||
$app->group('/currency/{query:[a-z]+}', function($app) {
|
||||
$app->group('/values', function($app) {
|
||||
$app->get('/latest[/]', [Currencies::class, 'findLatest']);
|
||||
$app->get('[/]', [Currencies::class, 'findValues']);
|
||||
$app->options('[/]', function (Request $request, Response $response): Response {
|
||||
return $response;
|
||||
});
|
||||
});
|
||||
$app->get('/sources', [Currencies::class, 'findSources']);
|
||||
$app->get('[/]', [Currencies::class, 'find']);
|
||||
$app->options('[/]', function (Request $request, Response $response): Response {
|
||||
return $response;
|
||||
});
|
||||
});
|
||||
|
2
app/setup/api/middleware.php
Normal file
2
app/setup/api/middleware.php
Normal file
@ -0,0 +1,2 @@
|
||||
<?php
|
||||
$app->add($app->getContainer()->get(ProVM\Money\Common\Middleware\Migrate::class));
|
@ -22,5 +22,11 @@ return [
|
||||
'routes'
|
||||
]);
|
||||
return (object) $arr;
|
||||
})
|
||||
}),
|
||||
'phinx' => function(Container $c) {
|
||||
return implode(DIRECTORY_SEPARATOR, [
|
||||
$c->get('locations')->base,
|
||||
'phinx.php'
|
||||
]);
|
||||
}
|
||||
];
|
||||
|
@ -4,5 +4,23 @@ use Psr\Container\ContainerInterface as Container;
|
||||
return [
|
||||
GuzzleHttp\ClientInterface::class => function(Container $c) {
|
||||
return new GuzzleHttp\Client();
|
||||
},
|
||||
ProVM\Common\Factory\Model::class => function(Container $c) {
|
||||
return new ProVM\Common\Factory\Model();
|
||||
},
|
||||
ProVM\Money\Common\Service\Update::class => function(Container $c) {
|
||||
return new ProVM\Money\Common\Service\Update($c->get(ProVM\Common\Factory\Model::class), $c->get(GuzzleHttp\ClientInterface::class));
|
||||
},
|
||||
ProVM\Money\Common\Middleware\Migrate::class => function(Container $c) {
|
||||
return new ProVM\Money\Common\Middleware\Migrate($c->get(Phinx\Wrapper\TextWrapper::class), $c->get(ProVM\Money\Common\Service\Update::class));
|
||||
},
|
||||
Phinx\Console\PhinxApplication::class => function(Container $c) {
|
||||
return new Phinx\Console\PhinxApplication();
|
||||
},
|
||||
Phinx\Wrapper\TextWrapper::class => function(Container $c) {
|
||||
$options = [
|
||||
'configuration' => $c->get('phinx')
|
||||
];
|
||||
return new Phinx\Wrapper\TextWrapper($c->get(Phinx\Console\PhinxApplication::class), $options);
|
||||
}
|
||||
];
|
||||
|
@ -34,13 +34,21 @@ class CreateValues extends Phinx\Migration\AbstractMigration
|
||||
'signed' => false,
|
||||
'after' => 'value',
|
||||
])
|
||||
->addIndex(['currency_id'], [
|
||||
/*->addIndex(['currency_id'], [
|
||||
'name' => 'currency_id',
|
||||
'unique' => false,
|
||||
])
|
||||
->addIndex(['base_id'], [
|
||||
'name' => 'base_id',
|
||||
'unique' => false,
|
||||
])*/
|
||||
->addForeignKey(['currency_id'], 'currencies', ['id'], [
|
||||
'delete' => 'CASCADE',
|
||||
'update' => 'CASCADE'
|
||||
])
|
||||
->addForeignKey(['base_id'], 'currencies', ['id'], [
|
||||
'delete' => 'CASCADE',
|
||||
'update' => 'CASCADE'
|
||||
])
|
||||
->create();
|
||||
/*$this->table('currencies', [
|
||||
|
@ -53,9 +53,13 @@ final class CreateSources extends AbstractMigration
|
||||
'encoding' => 'utf8mb4',
|
||||
'after' => 'url',
|
||||
])
|
||||
->addIndex(['currency_id'], [
|
||||
/*->addIndex(['currency_id'], [
|
||||
'name' => 'currency_id',
|
||||
'unique' => false,
|
||||
])*/
|
||||
->addForeignKey(['currency_id'], 'currencies', ['id'], [
|
||||
'delete' => 'CASCADE',
|
||||
'update' => 'CASCADE'
|
||||
])
|
||||
->create();
|
||||
}
|
||||
|
59
db/migrations/20210414015145_create_aliases.php
Normal file
59
db/migrations/20210414015145_create_aliases.php
Normal file
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use Phinx\Migration\AbstractMigration;
|
||||
|
||||
final class CreateAliases extends AbstractMigration
|
||||
{
|
||||
/**
|
||||
* Change Method.
|
||||
*
|
||||
* Write your reversible migrations using this method.
|
||||
*
|
||||
* More information on writing migrations is available here:
|
||||
* https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
|
||||
*
|
||||
* Remember to call "create()" or "update()" and NOT "save()" when working
|
||||
* with the Table class.
|
||||
*/
|
||||
public function change(): void
|
||||
{
|
||||
$this->table('aliases', [
|
||||
'id' => false,
|
||||
'primary_key' => ['id'],
|
||||
'engine' => 'InnoDB',
|
||||
'encoding' => 'utf8mb4',
|
||||
'collation' => 'utf8mb4_general_ci',
|
||||
'comment' => '',
|
||||
'row_format' => 'DYNAMIC',
|
||||
])
|
||||
->addColumn('id', 'integer', [
|
||||
'null' => false,
|
||||
'limit' => '10',
|
||||
'signed' => false,
|
||||
'identity' => 'enable',
|
||||
])
|
||||
->addColumn('currency_id', 'integer', [
|
||||
'null' => false,
|
||||
'limit' => '10',
|
||||
'signed' => false,
|
||||
'after' => 'id',
|
||||
])
|
||||
->addColumn('alias', 'string', [
|
||||
'null' => false,
|
||||
'limit' => 100,
|
||||
'collation' => 'utf8mb4_general_ci',
|
||||
'encoding' => 'utf8mb4',
|
||||
'after' => 'currency_id',
|
||||
])
|
||||
/*->addIndex(['currency_id'], [
|
||||
'name' => 'currency_id',
|
||||
'unique' => false,
|
||||
])*/
|
||||
->addForeignKey(['currency_id'], 'currencies', ['id'], [
|
||||
'delete' => 'CASCADE',
|
||||
'update' => 'CASCADE'
|
||||
])
|
||||
->create();
|
||||
}
|
||||
}
|
38
db/seeds/Currencies.php
Normal file
38
db/seeds/Currencies.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
|
||||
use Phinx\Seed\AbstractSeed;
|
||||
|
||||
class Currencies extends AbstractSeed
|
||||
{
|
||||
/**
|
||||
* Run Method.
|
||||
*
|
||||
* Write your database seeder using this method.
|
||||
*
|
||||
* More information on writing seeders is available here:
|
||||
* https://book.cakephp.org/phinx/0/en/seeding.html
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$data = [
|
||||
[
|
||||
'code' => 'CLP',
|
||||
'name' => 'Peso Chileno'
|
||||
],
|
||||
[
|
||||
'code' => 'CLF',
|
||||
'name' => 'Unidad de Fomento'
|
||||
],
|
||||
[
|
||||
'code' => 'USD',
|
||||
'name' => 'US Dollar'
|
||||
],
|
||||
[
|
||||
'code' => 'BTC',
|
||||
'name' => 'Bitcoin'
|
||||
]
|
||||
];
|
||||
$this->table('currencies')->insert($data)->saveData();
|
||||
}
|
||||
}
|
45
db/seeds/Sources.php
Normal file
45
db/seeds/Sources.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
|
||||
use Phinx\Seed\AbstractSeed;
|
||||
|
||||
class Sources extends AbstractSeed
|
||||
{
|
||||
public function getDependencies() {
|
||||
return [
|
||||
'Currencies'
|
||||
];
|
||||
}
|
||||
/**
|
||||
* Run Method.
|
||||
*
|
||||
* Write your database seeder using this method.
|
||||
*
|
||||
* More information on writing seeders is available here:
|
||||
* https://book.cakephp.org/phinx/0/en/seeding.html
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$id = $this->query("SELECT id FROM currencies WHERE code = 'USD'")->fetchAll(\PDO::FETCH_ASSOC);
|
||||
$data = [
|
||||
[
|
||||
'currency_id' => $id[0]['id'],
|
||||
'url' => 'https://mindicador.cl/api/dolar/{year}',
|
||||
'frecuency' => '1 DAY'
|
||||
]
|
||||
];
|
||||
$id = $this->query("SELECT id FROM currencies WHERE code = 'CLF'")->fetchAll(\PDO::FETCH_ASSOC);
|
||||
$data []= [
|
||||
'currency_id' => $id[0]['id'],
|
||||
'url' => 'https://mindicador.cl/api/uf/{year}',
|
||||
'frecuency' => '1 DAY'
|
||||
];
|
||||
$id = $this->query("SELECT id FROM currencies WHERE code = 'BTC'")->fetchAll(\PDO::FETCH_ASSOC);
|
||||
$data []= [
|
||||
'currency_id' => $id[0]['id'],
|
||||
'url' => 'https://mindicador.cl/api/bitcoin/{year}',
|
||||
'frecuency' => '1 DAY'
|
||||
];
|
||||
$this->table('sources')->insert($data)->saveData();
|
||||
}
|
||||
}
|
@ -21,6 +21,7 @@ services:
|
||||
ports:
|
||||
- 9123:9000
|
||||
app-cron:
|
||||
container_name: money_cron
|
||||
image: sleeck/crond
|
||||
volumes:
|
||||
- ./automation/crontab:/etc/cron.d/auto-crontab
|
||||
@ -73,9 +74,9 @@ services:
|
||||
- 3307:3306
|
||||
environment:
|
||||
MYSQL_ROOT_PASSWORD: 'money'
|
||||
MYSQL_DATABASE: 'money_dev'
|
||||
MYSQL_USER: 'money'
|
||||
MYSQL_PASSWORD: 'money_pass'
|
||||
MYSQL_DATABASE: ${DB_NAME-money_dev}
|
||||
MYSQL_USER: '${DB_USER-money}'
|
||||
MYSQL_PASSWORD: '${DB_PASSWORD-money_pass}'
|
||||
volumes:
|
||||
- dbdata:/var/lib/mysql
|
||||
adminer:
|
||||
|
56
src/Alias.php
Normal file
56
src/Alias.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
namespace ProVM\Money;
|
||||
|
||||
use ProVM\Common\Alias\Model;
|
||||
use ProVM\Common\Factory\Model as ModelFactory;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property Currency $currency_id
|
||||
* @property string $alias
|
||||
*/
|
||||
class Alias extends Model {
|
||||
public static $_table = 'aliases';
|
||||
|
||||
protected $currency;
|
||||
public function currency() {
|
||||
if ($this->currency === null) {
|
||||
$this->currency = $this->childOf(Currency::class, [Model::SELF_KEY => 'currency_id']);
|
||||
}
|
||||
return $this->currency;
|
||||
}
|
||||
|
||||
protected static $fields = ['currency_id', 'alias'];
|
||||
public static function add(ModelFactory $factory, $info) {
|
||||
$input = array_intersect_key((array) $info, array_combine(self::$fields, self::$fields));
|
||||
$alias = $factory->find(Alias::class)->where([['currency_id', $input['currency_id']], ['alias', $input['alias']]])->one();
|
||||
$created = false;
|
||||
$result = (object) compact('input', 'alias', 'created');
|
||||
if (!$alias) {
|
||||
$alias = $factory->create(Alias::class, $input);
|
||||
$created = $alias->save();
|
||||
$result->created = $created;
|
||||
}
|
||||
$result->source = $alias->asArray();
|
||||
return $result;
|
||||
}
|
||||
public function edit($info): bool {
|
||||
$data = array_intersect_key((array) $info, array_combine(self::$fields, self::$fields));
|
||||
$edited = false;
|
||||
foreach ($data as $field => $value) {
|
||||
if ($this->{$field} != $value) {
|
||||
$this->{$field} = $value;
|
||||
$edited = true;
|
||||
}
|
||||
}
|
||||
if ($edited) {
|
||||
$edited = $this->save();
|
||||
}
|
||||
return $edited;
|
||||
}
|
||||
public function asArray(): array {
|
||||
$output = parent::asArray();
|
||||
$output['currency'] = $this->currency()->asArray();
|
||||
return $output;
|
||||
}
|
||||
}
|
@ -38,6 +38,13 @@ class Currency extends Model {
|
||||
}
|
||||
return $this->sources;
|
||||
}
|
||||
protected $aliases;
|
||||
public function aliases() {
|
||||
if ($this->aliases === null) {
|
||||
$this->aliases = $this->parentOf(Alias::class, [Model::CHILD_KEY => 'currency_id']);
|
||||
}
|
||||
return $this->aliases;
|
||||
}
|
||||
|
||||
protected static $fields = ['code', 'name'];
|
||||
public static function add(ModelFactory $factory, $info) {
|
||||
@ -73,6 +80,12 @@ class Currency extends Model {
|
||||
}
|
||||
return $edited;
|
||||
}
|
||||
public function addAlias($info) {
|
||||
$arr = (array) $info;
|
||||
$arr['currency_id'] = (int) $this->id;
|
||||
$result = Alias::add($this->factory, $arr);
|
||||
return $result;
|
||||
}
|
||||
public function addValue($info) {
|
||||
$arr = (array) $info;
|
||||
$arr['currency_id'] = (int) $this->id;
|
||||
@ -85,4 +98,27 @@ class Currency extends Model {
|
||||
$result = Source::add($this->factory, $arr);
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function find(ModelFactory $factory, string $query) {
|
||||
$query = '%' . $query . '%';
|
||||
$currency = $factory->find(Currency::class)->where([
|
||||
['code', $query, 'like']
|
||||
])->one();
|
||||
if ($currency !== false and $currency !== null) {
|
||||
return $currency;
|
||||
}
|
||||
$currency = $factory->find(Currency::class)->where([
|
||||
['name', $query, 'like']
|
||||
])->one();
|
||||
if ($currency !== false and $currency !== null) {
|
||||
return $currency;
|
||||
}
|
||||
$alias = $factory->find(Alias::class)->where([
|
||||
['alias', $query, 'like']
|
||||
])->one();
|
||||
if ($alias !== false and $alias !== null) {
|
||||
return $alias->currency();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,15 @@
|
||||
|
||||
@section('content')
|
||||
<h3 class="ui header c_code"></h3>
|
||||
<table class="ui table">
|
||||
<thead>
|
||||
<th>Alias</th>
|
||||
<th class="right aligned" id="add_alias">
|
||||
<i class="plus icon"></i>
|
||||
</th>
|
||||
</thead>
|
||||
<tbody id="aliases"></tbody>
|
||||
</table>
|
||||
<table class="ui table">
|
||||
<thead>
|
||||
<th>Url</th>
|
||||
@ -87,10 +96,112 @@
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<div class="ui modal" id="add_aliases">
|
||||
<div class="header">
|
||||
Agregar Alias para <span class="c_name"></span>
|
||||
</div>
|
||||
<div class="content">
|
||||
<form class="ui form">
|
||||
<div class="inline field">
|
||||
<label>Alias</label>
|
||||
<input type="text" name="alias" />
|
||||
</div>
|
||||
<button class="ui button">Agregar</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@push('scripts')
|
||||
<script type="text/javascript">
|
||||
let aliases = {
|
||||
id: '#aliases',
|
||||
add_button: '#add_alias',
|
||||
add_modal: '#add_aliases',
|
||||
loading: '',
|
||||
loaded: false,
|
||||
aliases: [],
|
||||
setup: function() {
|
||||
$(this.id).hide()
|
||||
$(this.add_button).css('cursor', 'pointer').click((e) => {
|
||||
this.add()
|
||||
})
|
||||
this.buildModal()
|
||||
},
|
||||
get: function(currency_id, data) {
|
||||
if (!this.loaded) {
|
||||
this.aliases = data.aliases
|
||||
this.populate(currency_id)
|
||||
socket.sendMessage('currency.sources', {currency_id: currency_id})
|
||||
return
|
||||
}
|
||||
var url = '{{$urls->api}}/currency/' + currency_id + '/aliases'
|
||||
$.getJSON(url, (data) => {
|
||||
this.aliases = data.aliases
|
||||
this.populate(currency_id)
|
||||
})
|
||||
},
|
||||
buildModal: function() {
|
||||
$(this.add_modal).modal()
|
||||
$(this.add_modal).find('form').submit((e) => {
|
||||
e.preventDefault()
|
||||
this.doAdd()
|
||||
return false
|
||||
})
|
||||
},
|
||||
populate: function(currency_id) {
|
||||
$(this.id).html('')
|
||||
$.each(this.aliases, (i, el) => {
|
||||
let row = $('<tr></tr>').append(
|
||||
$('<td></td>').html(el.alias)
|
||||
).append(
|
||||
$('<td></td>').attr('class', 'remove_source right aligned').attr('data-id', el.id).append(
|
||||
$('<i></i>').attr('class', 'minus icon')
|
||||
).css('cursor', 'pointer').click((e) => {
|
||||
this.remove(currency_id, $(e.currentTarget).attr('data-id'))
|
||||
})
|
||||
)
|
||||
$(this.id).append(row)
|
||||
})
|
||||
$(this.id).show()
|
||||
},
|
||||
add: function() {
|
||||
$(this.add_modal).find('form').trigger('reset')
|
||||
$(this.add_modal).modal('show')
|
||||
},
|
||||
doAdd: function() {
|
||||
let form = $(this.add_modal).find('form')
|
||||
let info = {
|
||||
alias: form.find("[name='alias']").val()
|
||||
}
|
||||
var url = '{{$urls->api}}/currency/{{$currency_id}}/aliases/add'
|
||||
$(this.add_modal).modal('hide')
|
||||
$(this.loading).modal('show')
|
||||
$.post(url, JSON.stringify(info), (data) => {
|
||||
if (data.aliases[0].created) {
|
||||
this.get('{{$currency_id}}')
|
||||
}
|
||||
}, 'json').then(() => {
|
||||
$(this.loading).modal('hide')
|
||||
})
|
||||
},
|
||||
remove: function(id) {
|
||||
var url = '{{$urls->api}}/alias/' + id + '/delete'
|
||||
$(this.loading).modal('show')
|
||||
$.ajax({
|
||||
url: url,
|
||||
method: 'DELETE',
|
||||
dataType: 'json',
|
||||
success: (data) => {
|
||||
if (data.deleted) {
|
||||
this.get()
|
||||
}
|
||||
}
|
||||
}).then(() => {
|
||||
$(this.loading).modal('hide')
|
||||
})
|
||||
}
|
||||
}
|
||||
let sources = {
|
||||
id: '#sources',
|
||||
add_button: '#add_source',
|
||||
@ -302,6 +413,8 @@
|
||||
},
|
||||
loading: '#loading',
|
||||
setup: function() {
|
||||
aliases.loading = this.loading
|
||||
aliases.setup()
|
||||
sources.loading = this.loading
|
||||
sources.setup()
|
||||
values.loading = this.loading
|
||||
@ -317,6 +430,9 @@
|
||||
if (response.request.action == 'currency') {
|
||||
currency.get(response.body)
|
||||
}
|
||||
if (response.request.action == 'currency.aliases') {
|
||||
aliases.get(response.request.body.currency_id, response.body)
|
||||
}
|
||||
if (response.request.action == 'currency.sources') {
|
||||
sources.get(response.request.body.currency_id, response.body)
|
||||
}
|
||||
@ -331,7 +447,7 @@
|
||||
$.each(this.map, (i, el) => {
|
||||
$(el).html(data.currency[i])
|
||||
})
|
||||
socket.sendMessage('currency.sources', {currency_id: '{{$currency_id}}'})
|
||||
socket.sendMessage('currency.aliases', {currency_id: '{{$currency_id}}'})
|
||||
}
|
||||
}
|
||||
$(document).ready(() => {
|
||||
|
@ -32,7 +32,7 @@ return [
|
||||
return (object) $arr;
|
||||
}),
|
||||
'urls' => function(Container $c) {
|
||||
$arr = ['base' => ($c->has('base_url')) ? $c->get('base_url') : ''];
|
||||
$arr = ['base' => ($c->has('base_url')) ? $c->get('base_url') : '/'];
|
||||
$arr['assets'] = implode('/', [
|
||||
$arr['base'],
|
||||
'assets'
|
||||
|
@ -37,6 +37,24 @@ class Currencies {
|
||||
$response->getBody()->write($output);
|
||||
return $response;
|
||||
}
|
||||
public function getAliases(Request $request, Response $response, ModelFactory $factory): Response {
|
||||
$currency_id = $request->getBody()->read()['currency_id'];
|
||||
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||
$output = [
|
||||
'currency' => null,
|
||||
'aliases' => []
|
||||
];
|
||||
if ($currency) {
|
||||
$output['currency'] = $currency->asArray();
|
||||
if ($currency->aliases()) {
|
||||
$output['aliases'] = array_map(function($item) {
|
||||
return $item->asArray();
|
||||
}, $currency->aliases());
|
||||
}
|
||||
}
|
||||
$response->getBody()->write($output);
|
||||
return $response;
|
||||
}
|
||||
public function getSources(Request $request, Response $response, ModelFactory $factory): Response {
|
||||
$currency_id = $request->getBody()->read()['currency_id'];
|
||||
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||
|
@ -4,6 +4,7 @@ use ProVM\Money\Common\Listener\Currencies;
|
||||
$controller = new Currencies();
|
||||
$app->add('currencies', $controller);
|
||||
$app->add('currency', [$controller, 'get']);
|
||||
$app->add('currency.aliases', [$controller, 'getAliases']);
|
||||
$app->add('currency.values.latest', [$controller, 'latest']);
|
||||
$app->add('currency.sources', [$controller, 'getSources']);
|
||||
$app->add('currency.values', [$controller, 'getValues']);
|
||||
|
Reference in New Issue
Block a user