This commit is contained in:
2021-03-20 16:51:17 -03:00
parent 845077d2a0
commit 53a519ffbd
7 changed files with 175 additions and 8 deletions

View File

@ -130,4 +130,47 @@ class Currencies {
}
return $this->withJson($response, $output);
}
public function getSources(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,
'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);
}
public function addSources(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,
'sources' => []
];
if ($currency) {
$output['currency'] = $currency->asArray();
$sources = [];
if (is_array($post)) {
foreach ($post as $obj) {
if (!is_object($obj)) {
continue;
}
$sources []= $currency->addSource($obj);
}
} else {
$sources []= $currency->addSource($post);
}
$output['sources'] = $sources;
}
return $this->withJson($response, $output);
}
}

View 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\Source;
class Sources {
use Json;
public function __invoke(Request $request, Response $response, ModelFactory $factory): Response {
$sources = $factory->find(Source::class)->array();
$output = compact('sources');
return $this->withJson($response, $output);
}
public function get(Request $request, Response $response, ModelFactory $factory, $currency_id, $url): Response {
$source = $factory->find(Source::class)->where([['currency_id', $currency_id], ['url', $url]])->one();
$output = [
'get_data' => compact('currency_id', 'url'),
'source' => null
];
if ($source) {
$output['source'] = $source->asArray();
}
return $this->withJson($response, $output);
}
public function add(Request $request, Response $response, ModelFactory $factory): Response {
$post = json_decode($request->getBody()->getContents());
$sources = [];
if (is_array($post)) {
foreach ($post as $obj) {
if (!is_object($obj)) {
continue;
}
$sources []= Source::add($factory, $obj);
}
} else {
$sources []= Source::add($factory, $post);
}
$output = [
'post_data' => $post,
'sources' => $sources
];
return $this->withJson($response, $output);
}
public function edit(Request $request, Response $response, ModelFactory $factory, $currency_id, $url) {
$post = json_decode($request->getBody()->getContents());
$output = [
'get_data' => compact('currency_id', 'url'),
'post_data' => $post
];
$source = $factory->find(Source::class)->where([['currency_id', $currency_id], ['url', $url]])->one();
$edited = false;
if ($source) {
$edited = $source->edit($post);
$output['source'] = $source->asArray();
$output['edited'] = $edited;
}
return $this->withJson($response, $output);
}
public function delete(Request $request, Response $response, ModelFactory $factory, $currency_id, $url): Response {
$source = $factory->find(Source::class)->where([['currency_id', $currency_id], ['url', $url]])->one();
$output = ['get_data' => compact('currency_id', 'url'), 'source' => null, 'deleted' => false];
if ($source) {
$output['source'] = $source->asArray();
$status = $source->delete();
$output['deleted'] = $status;
}
return $this->withJson($response, $output);
}
}

View File

@ -3,6 +3,7 @@ use ProVM\Money\Common\Controller\API;
include_once 'currencies.php';
include_once 'values.php';
include_once 'sources.php';
$app->get('/', API::class);

View File

@ -0,0 +1,13 @@
<?php
use ProVM\Money\Common\Controller\Sources;
$app->group('/sources', function($app) {
$app->post('/add[/]', [Sources::class, 'add']);
$app->get('[/]', Sources::class);
});
$app->group('/source/{currency_id}/{url}', function($app) {
$app->put('/edit[/]', [Sources::class, 'edit']);
$app->delete('/delete[/]', [Sources::class, 'delete']);
$app->get('[/]', [Sources::class, 'get']);
});

View File

@ -20,19 +20,13 @@ final class CreateSources extends AbstractMigration
{
$this->table('sources', [
'id' => false,
'primary_key' => ['id'],
'primary_key' => ['currency_id', 'url'],
'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',

View File

@ -79,4 +79,10 @@ class Currency extends Model {
$result = Value::add($this->factory, $arr);
return $result;
}
public function addSource($info) {
$arr = (array) $info;
$arr['currency_id'] = (int) $this->id;
$result = Source::add($this->factory, $arr);
return $result;
}
}

View File

@ -5,7 +5,6 @@ use Carbon\CarbonInterval;
use ProVM\Common\Alias\Model;
/**
* @property int $id
* @property Currency $currency_id
* @property string $url
* @property \DateInterval $frecuency
@ -24,4 +23,42 @@ class Source extends Model {
}
$this->frecuency = CarbonInterval::getDateIntervalSpec($frecuency);
}
protected static $fields = ['currency_id', 'url', 'frecuency'];
public static function add(ModelFactory $factory, $info) {
$input = array_intersect_key((array) $info, array_combine(self::$fields, self::$fields));
$source = $factory->find(Source::class)->where([['currency_id', $input['currency_id']], ['url', $input['url']]])->one();
$created = false;
$result = (object) compact('input', 'source', 'created');
if (!$value) {
$source = $factory->create(Source::class, $input);
$created = $source->save();
$result->created = $created;
}
$result->value = $source->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) {
if ($field == 'currency_id' or $field == 'url') {
continue;
}
$this->{$field} = $value;
$edited = true;
}
}
if ($edited) {
$edited = $this->save();
}
return $edited;
}
public function asArray(): array {
$output = parent::asArray();
$output['currency'] = $this->currency()->asArray();
$output['frecuency'] = $this->frecuency()->format('Y-m-d H:i:s');
return $output;
}
}