diff --git a/app/common/Controller/Currencies.php b/app/common/Controller/Currencies.php index 8570db3..a1065f6 100644 --- a/app/common/Controller/Currencies.php +++ b/app/common/Controller/Currencies.php @@ -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); + } } diff --git a/app/common/Controller/Sources.php b/app/common/Controller/Sources.php new file mode 100644 index 0000000..c7a0aef --- /dev/null +++ b/app/common/Controller/Sources.php @@ -0,0 +1,73 @@ +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); + } +} diff --git a/app/resources/routes/api.php b/app/resources/routes/api.php index d9d1e96..8940f95 100644 --- a/app/resources/routes/api.php +++ b/app/resources/routes/api.php @@ -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); diff --git a/app/resources/routes/sources.php b/app/resources/routes/sources.php new file mode 100644 index 0000000..920a10f --- /dev/null +++ b/app/resources/routes/sources.php @@ -0,0 +1,13 @@ +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']); +}); diff --git a/db/migrations/20210320020631_create_sources.php b/db/migrations/20210320020631_create_sources.php index 1d45a2a..fcfba85 100644 --- a/db/migrations/20210320020631_create_sources.php +++ b/db/migrations/20210320020631_create_sources.php @@ -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', diff --git a/src/Currency.php b/src/Currency.php index 02cf51f..a53d614 100644 --- a/src/Currency.php +++ b/src/Currency.php @@ -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; + } } diff --git a/src/Source.php b/src/Source.php index a271cb2..caba155 100644 --- a/src/Source.php +++ b/src/Source.php @@ -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; + } }