Sources
This commit is contained in:
@ -130,4 +130,47 @@ class Currencies {
|
|||||||
}
|
}
|
||||||
return $this->withJson($response, $output);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
73
app/common/Controller/Sources.php
Normal file
73
app/common/Controller/Sources.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\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);
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@ use ProVM\Money\Common\Controller\API;
|
|||||||
|
|
||||||
include_once 'currencies.php';
|
include_once 'currencies.php';
|
||||||
include_once 'values.php';
|
include_once 'values.php';
|
||||||
|
include_once 'sources.php';
|
||||||
|
|
||||||
$app->get('/', API::class);
|
$app->get('/', API::class);
|
||||||
|
|
||||||
|
13
app/resources/routes/sources.php
Normal file
13
app/resources/routes/sources.php
Normal 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']);
|
||||||
|
});
|
@ -20,19 +20,13 @@ final class CreateSources extends AbstractMigration
|
|||||||
{
|
{
|
||||||
$this->table('sources', [
|
$this->table('sources', [
|
||||||
'id' => false,
|
'id' => false,
|
||||||
'primary_key' => ['id'],
|
'primary_key' => ['currency_id', 'url'],
|
||||||
'engine' => 'InnoDB',
|
'engine' => 'InnoDB',
|
||||||
'encoding' => 'utf8mb4',
|
'encoding' => 'utf8mb4',
|
||||||
'collation' => 'utf8mb4_general_ci',
|
'collation' => 'utf8mb4_general_ci',
|
||||||
'comment' => '',
|
'comment' => '',
|
||||||
'row_format' => 'DYNAMIC',
|
'row_format' => 'DYNAMIC',
|
||||||
])
|
])
|
||||||
->addColumn('id', 'integer', [
|
|
||||||
'null' => false,
|
|
||||||
'limit' => '10',
|
|
||||||
'signed' => false,
|
|
||||||
'identity' => 'enable',
|
|
||||||
])
|
|
||||||
->addColumn('currency_id', 'integer', [
|
->addColumn('currency_id', 'integer', [
|
||||||
'null' => false,
|
'null' => false,
|
||||||
'limit' => '10',
|
'limit' => '10',
|
||||||
|
@ -79,4 +79,10 @@ class Currency extends Model {
|
|||||||
$result = Value::add($this->factory, $arr);
|
$result = Value::add($this->factory, $arr);
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
public function addSource($info) {
|
||||||
|
$arr = (array) $info;
|
||||||
|
$arr['currency_id'] = (int) $this->id;
|
||||||
|
$result = Source::add($this->factory, $arr);
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ use Carbon\CarbonInterval;
|
|||||||
use ProVM\Common\Alias\Model;
|
use ProVM\Common\Alias\Model;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @property int $id
|
|
||||||
* @property Currency $currency_id
|
* @property Currency $currency_id
|
||||||
* @property string $url
|
* @property string $url
|
||||||
* @property \DateInterval $frecuency
|
* @property \DateInterval $frecuency
|
||||||
@ -24,4 +23,42 @@ class Source extends Model {
|
|||||||
}
|
}
|
||||||
$this->frecuency = CarbonInterval::getDateIntervalSpec($frecuency);
|
$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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user