Update
This commit is contained in:
@ -15,10 +15,10 @@ class Sources {
|
|||||||
$output = compact('sources');
|
$output = compact('sources');
|
||||||
return $this->withJson($response, $output);
|
return $this->withJson($response, $output);
|
||||||
}
|
}
|
||||||
public function get(Request $request, Response $response, ModelFactory $factory, $currency_id, $url): Response {
|
public function get(Request $request, Response $response, ModelFactory $factory, $source_id): Response {
|
||||||
$source = $factory->find(Source::class)->where([['currency_id', $currency_id], ['url', $url]])->one();
|
$source = $factory->find(Source::class)->one($source_id);
|
||||||
$output = [
|
$output = [
|
||||||
'get_data' => compact('currency_id', 'url'),
|
'get_data' => compact('source_id'),
|
||||||
'source' => null
|
'source' => null
|
||||||
];
|
];
|
||||||
if ($source) {
|
if ($source) {
|
||||||
@ -45,13 +45,13 @@ class Sources {
|
|||||||
];
|
];
|
||||||
return $this->withJson($response, $output);
|
return $this->withJson($response, $output);
|
||||||
}
|
}
|
||||||
public function edit(Request $request, Response $response, ModelFactory $factory, $currency_id, $url) {
|
public function edit(Request $request, Response $response, ModelFactory $factory, $source_id) {
|
||||||
$post = json_decode($request->getBody()->getContents());
|
$post = json_decode($request->getBody()->getContents());
|
||||||
$output = [
|
$output = [
|
||||||
'get_data' => compact('currency_id', 'url'),
|
'get_data' => compact('source_id'),
|
||||||
'post_data' => $post
|
'post_data' => $post
|
||||||
];
|
];
|
||||||
$source = $factory->find(Source::class)->where([['currency_id', $currency_id], ['url', $url]])->one();
|
$source = $factory->find(Source::class)->one($source_id);
|
||||||
$edited = false;
|
$edited = false;
|
||||||
if ($source) {
|
if ($source) {
|
||||||
$edited = $source->edit($post);
|
$edited = $source->edit($post);
|
||||||
@ -60,9 +60,9 @@ class Sources {
|
|||||||
}
|
}
|
||||||
return $this->withJson($response, $output);
|
return $this->withJson($response, $output);
|
||||||
}
|
}
|
||||||
public function delete(Request $request, Response $response, ModelFactory $factory, $currency_id, $url): Response {
|
public function delete(Request $request, Response $response, ModelFactory $factory, $source_id): Response {
|
||||||
$source = $factory->find(Source::class)->where([['currency_id', $currency_id], ['url', $url]])->one();
|
$source = $factory->find(Source::class)->one($source_id);
|
||||||
$output = ['get_data' => compact('currency_id', 'url'), 'source' => null, 'deleted' => false];
|
$output = ['get_data' => compact('source_id'), 'source' => null, 'deleted' => false];
|
||||||
if ($source) {
|
if ($source) {
|
||||||
$output['source'] = $source->asArray();
|
$output['source'] = $source->asArray();
|
||||||
$status = $source->delete();
|
$status = $source->delete();
|
||||||
|
86
app/common/Controller/Update.php
Normal file
86
app/common/Controller/Update.php
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
<?php
|
||||||
|
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;
|
||||||
|
|
||||||
|
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'] ++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
|
}
|
@ -10,6 +10,7 @@
|
|||||||
"provm/models": "dev-master",
|
"provm/models": "dev-master",
|
||||||
"provm/controller": "dev-master",
|
"provm/controller": "dev-master",
|
||||||
"vlucas/phpdotenv": "^5.3",
|
"vlucas/phpdotenv": "^5.3",
|
||||||
|
"guzzlehttp/guzzle": "^7.3",
|
||||||
"nesbot/carbon": "^2.46"
|
"nesbot/carbon": "^2.46"
|
||||||
},
|
},
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -13,6 +13,7 @@ server {
|
|||||||
add_header 'Access-Control-Allow-Origin' 'http://localhost:8080';
|
add_header 'Access-Control-Allow-Origin' 'http://localhost:8080';
|
||||||
add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,OPTIONS';
|
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 "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
|
||||||
|
add_header 'Content-Type' 'application/json';
|
||||||
|
|
||||||
location ~ \.php {
|
location ~ \.php {
|
||||||
try_files $uri =404;
|
try_files $uri =404;
|
||||||
|
@ -1,9 +1,16 @@
|
|||||||
<?php
|
<?php
|
||||||
use ProVM\Money\Common\Controller\API;
|
use ProVM\Money\Common\Controller\API;
|
||||||
|
|
||||||
include_once 'currencies.php';
|
$files = new DirectoryIterator(implode(DIRECTORY_SEPARATOR, [
|
||||||
include_once 'values.php';
|
__DIR__,
|
||||||
include_once 'sources.php';
|
'api'
|
||||||
|
]));
|
||||||
|
foreach ($files as $file) {
|
||||||
|
if ($file->isDir()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
include_once $file->getRealPath();
|
||||||
|
}
|
||||||
|
|
||||||
$app->get('/', API::class);
|
$app->get('/', API::class);
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ $app->group('/sources', function($app) {
|
|||||||
$app->get('[/]', Sources::class);
|
$app->get('[/]', Sources::class);
|
||||||
});
|
});
|
||||||
|
|
||||||
$app->group('/source/{currency_id}/{url}', function($app) {
|
$app->group('/source/{source_id}', function($app) {
|
||||||
$app->put('/edit[/]', [Sources::class, 'edit']);
|
$app->put('/edit[/]', [Sources::class, 'edit']);
|
||||||
$app->delete('/delete[/]', [Sources::class, 'delete']);
|
$app->delete('/delete[/]', [Sources::class, 'delete']);
|
||||||
$app->get('[/]', [Sources::class, 'get']);
|
$app->get('[/]', [Sources::class, 'get']);
|
4
app/resources/routes/api/update.php
Normal file
4
app/resources/routes/api/update.php
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?php
|
||||||
|
use ProVM\Money\Common\Controller\Update;
|
||||||
|
|
||||||
|
$app->get('/update', Update::class);
|
8
app/setup/api/setups.php
Normal file
8
app/setup/api/setups.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
use Psr\Container\ContainerInterface as Container;
|
||||||
|
|
||||||
|
return [
|
||||||
|
GuzzleHttp\ClientInterface::class => function(Container $c) {
|
||||||
|
return new GuzzleHttp\Client();
|
||||||
|
}
|
||||||
|
];
|
Reference in New Issue
Block a user