diff --git a/app/common/Controller/Currencies.php b/app/common/Controller/Currencies.php index 1fa4bca..b5c19c8 100644 --- a/app/common/Controller/Currencies.php +++ b/app/common/Controller/Currencies.php @@ -15,66 +15,104 @@ class Currencies { $output = compact('currencies'); return $this->withJson($response, $output); } - public function show(Request $request, Response $response, ModelFactory $factory, $currency_id): Response { + public function get(Request $request, Response $response, ModelFactory $factory, $currency_id): Response { $currency = $factory->find(Currency::class)->one($currency_id); $output = [ - 'currency' => $currency->asArray() + 'get_data' => compact('currency_id'), + 'currency' => null ]; + if ($currency) { + $output['currency'] = $currency->asArray(); + } return $this->withJson($response, $output); } public function add(Request $request, Response $response, ModelFactory $factory): Response { - $fields = [ - 'code', - 'name' - ]; $post = json_decode($request->getBody()->getContents()); - $data = array_intersect_key((array) $post, array_combine($fields, $fields)); - $currency = $factory->find(Currency::class)->where([['code', $data['code']]])->one(); - if (!$currency) { - $currency = $factory->create(Currency::class, $data); - $currency->save(); + $currencies = []; + if (is_array($post)) { + foreach ($post as $obj) { + if (!is_object($obj)) { + continue; + } + $currencies []= Currency::add($factory, $obj); + } + } else { + $currencies []= Currency::add($factory, $post); } $output = [ 'post_data' => $post, - 'input' => $data, - 'currency' => $currency->asArray() + 'currencies' => $currencies ]; return $this->withJson($response, $output); } public function edit(Request $request, Response $response, ModelFactory $factory, $currency_id): Response { $currency = $factory->find(Currency::class)->one($currency_id); - $fields = [ - 'code', - 'name' - ]; $post = json_decode($request->getBody()->getContents()); - $data = array_intersect_key((array) $post, array_combine($fields, $fields)); $edited = false; - foreach ($data as $field => $value) { - if ($currency->{$field} != $value) { - $currency->{$field} = $value; - $edited = true; - } - } - if ($edited) { - $currency->save(); - } $output = [ 'get_data' => compact('currency_id'), 'post_data' => $post, - 'input' => $data, - 'currency' => $currency->asArray() + 'currency' => null, + 'edited' => $edited ]; + if ($currency) { + $edited = $currency->edit($post); + $output['currency'] = $currency->asArray(); + $output['edited'] = $edited; + } return $this->withJson($response, $output); } public function delete(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, 'deleted' => false]; + if ($currency) { + $output['currency'] = $currency->asArray(); + $status = $currency->delete(); + $output['deleted'] = $status; + } + 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 = [ 'get_data' => compact('currency_id'), - 'currency' => $currency->asArray() + 'currency' => null, + 'values' => [] ]; - $status = $currency->delete(); - $output['deleted'] = $status; + 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 addValues(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, + 'values' => [] + ]; + if ($currency) { + $output['currency'] = $currency->asArray(); + $values = []; + if (is_array($post)) { + foreach ($post as $obj) { + if (!is_object($obj)) { + continue; + } + $values []= $currency->addValue($obj); + } + } else { + $values []= $currency->addValue($post); + } + $output['values'] = $values; + } return $this->withJson($response, $output); } } diff --git a/app/common/Controller/Values.php b/app/common/Controller/Values.php new file mode 100644 index 0000000..96e4fe9 --- /dev/null +++ b/app/common/Controller/Values.php @@ -0,0 +1,113 @@ +find(Value::class)->many(); + $output = compact('values'); + array_walk($output['values'], function(&$item) { + $item = $item->asArray(); + }); + return $this->withJson($response, $output); + } + public function get(Request $request, Response $response, ModelFactory $factory, $currency_id, $base_id, $date_time = null): Response { + if ($date_time !== null) { + $value = $factory->find(Value::class)->where([['currency_id', $currency_id], ['base_id', $base_id], ['date_time', $date_time]])->one(); + $output = [ + 'get_data' => compact('currency_id', 'date_time', 'base_id'), + 'value' => null + ]; + if ($value) { + $output['value'] = $value->asArray(); + } + } else { + $values = $factory->find(Value::class)->where([['currency_id', $currency_id], ['base_id', $base_id]])->many(); + $output = [ + 'get_data' => compact('currency_id', 'date_time', 'base_id'), + 'values' => null + ]; + if ($values) { + $vals = []; + foreach ($values as $value) { + $vals []= $value->asArray(); + } + $output['values'] = $vals; + } + } + return $this->withJson($response, $output); + } + public function add(Request $request, Response $response, ModelFactory $factory): Response { + $post = json_decode($request->getBody()->getContents()); + $values = []; + if (is_array($post)) { + foreach ($post as $obj) { + if (!is_object($obj)) { + continue; + } + $values []= Value::add($factory, $obj); + } + } else { + $values []= Value::add($factory, $post); + } + $output = [ + 'post_data' => $post, + 'values' => $values + ]; + return $this->withJson($response, $output); + } + public function edit(Request $request, Response $response, ModelFactory $factory, $currency_id, $base_id, $date_time = null): Response { + $post = json_decode($request->getBody()->getContents()); + $output = [ + 'get_data' => compact('currency_id', 'date_time', 'base_id'), + 'post_data' => $post + ]; + if ($date_time !== null) { + $value = $factory->find(Value::class)->where([['currency_id', $currency_id], ['base_id', $base_id], ['date_time', $date_time]])->one(); + $edited = false; + if ($value) { + $edited = $value->edit($post); + $output['value'] = $value->asArray(); + $output['edited'] = $edited; + } + } else { + $values = $factory->find(Value::class)->where([['currency_id', $currency_id], ['base_id', $base_id]])->many(); + $vals = []; + foreach ($values as $value) { + $edited = $value->edit($post); + $vals []= ['value' => $value, 'edited' => $edited]; + } + $output['values'] = $vals; + } + return $this->withJson($response, $output); + } + public function delete(Request $request, Response $response, ModelFactory $factory, $currency_id, $base_id, $date_time = null): Response { + $output = ['get_data' => compact('currency_id', 'date_time', 'base_id')]; + if ($date_time !== null) { + $value = $factory->find(Value::class)->where([['currency_id', $currency_id], ['base_id', $base_id], ['date_time', $date_time]])->one(); + if ($currency) { + $output['value'] = $value->asArray(); + $status = $value->delete(); + $output['deleted'] = $status; + } + } else { + $values = $factory->find(Value::class)->where([['currency_id', $currency_id], ['base_id', $base_id]])->many(); + $vals = []; + foreach ($values as $value) { + $val = ['value' => $value]; + $status = $value->delete(); + $val['deleted'] = $status; + $vals []= $val; + } + $output['values'] = $vals; + } + return $this->withJson($response, $output); + } +} diff --git a/app/resources/routes/api.php b/app/resources/routes/api.php index a38b556..ff417ba 100644 --- a/app/resources/routes/api.php +++ b/app/resources/routes/api.php @@ -2,5 +2,6 @@ use ProVM\Money\Common\Controller\API; include_once 'currencies.php'; +include_once 'values.php'; $app->get('/', API::class); diff --git a/app/resources/routes/currencies.php b/app/resources/routes/currencies.php index 837717c..05c3a16 100644 --- a/app/resources/routes/currencies.php +++ b/app/resources/routes/currencies.php @@ -7,5 +7,11 @@ $app->group('/currencies', function($app) { }); $app->group('/currency/{currency_id}', function($app) { - $app->get('[/]', [Currencies::class, 'show']); + $app->put('/edit[/]', [Currencies::class, 'edit']); + $app->delete('/delete[/]', [Currencies::class, 'delete']); + $app->group('/values', function($app) { + $app->post('/add[/]', [Currencies::class, 'addValues']); + $app->get('[/]', [Currencies::class, 'getValues']); + }); + $app->get('[/]', [Currencies::class, 'get']); }); diff --git a/app/resources/routes/values.php b/app/resources/routes/values.php new file mode 100644 index 0000000..8e70e6d --- /dev/null +++ b/app/resources/routes/values.php @@ -0,0 +1,18 @@ +group('/values', function($app) { + $app->post('/add[/]', [Values::class, 'add']); + $app->get('[/]', Values::class); +}); + +$app->group('/value/{currency_id}/{base_id}/{date_time}', function($app) { + $app->put('/edit[/]', [Values::class, 'edit']); + $app->delete('/delete[/]', [Values::class, 'delete']); + $app->get('[/]', [Values::class, 'get']); +}); +$app->group('/value/{currency_id}/{base_id}', function($app) { + $app->put('/edit[/]', [Values::class, 'edit']); + $app->delete('/delete[/]', [Values::class, 'delete']); + $app->get('[/]', [Values::class, 'get']); +});