From 99c18cb871c21dd3af02c28780f345562834581f Mon Sep 17 00:00:00 2001 From: Aldarien Date: Fri, 19 Mar 2021 22:49:09 -0300 Subject: [PATCH] Fixes --- app/common/Controller/Currencies.php | 15 +++++++++++++++ app/common/Controller/Values.php | 4 ++-- app/docker/nginx.conf | 2 ++ app/resources/routes/currencies.php | 1 + app/setup/api/settings.php | 8 ++++++++ app/setup/app.php | 2 +- docker-compose.yml | 12 ++++-------- src/Currency.php | 12 ++++++++++++ 8 files changed, 45 insertions(+), 11 deletions(-) diff --git a/app/common/Controller/Currencies.php b/app/common/Controller/Currencies.php index b5c19c8..8570db3 100644 --- a/app/common/Controller/Currencies.php +++ b/app/common/Controller/Currencies.php @@ -115,4 +115,19 @@ class Currencies { } return $this->withJson($response, $output); } + public function latestValue(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, + 'value' => null + ]; + if ($currency) { + $output['currency'] = $currency->asArray(); + if ($currency->latest()) { + $output['value'] = $currency->latest()->asArray(); + } + } + return $this->withJson($response, $output); + } } diff --git a/app/common/Controller/Values.php b/app/common/Controller/Values.php index 96e4fe9..add0f38 100644 --- a/app/common/Controller/Values.php +++ b/app/common/Controller/Values.php @@ -89,10 +89,10 @@ class Values { 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')]; + $output = ['get_data' => compact('currency_id', 'date_time', 'base_id'), 'value' => null, 'deleted' => false]; 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) { + if ($value) { $output['value'] = $value->asArray(); $status = $value->delete(); $output['deleted'] = $status; diff --git a/app/docker/nginx.conf b/app/docker/nginx.conf index 149f784..375e04c 100644 --- a/app/docker/nginx.conf +++ b/app/docker/nginx.conf @@ -50,6 +50,8 @@ server { } 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-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept"; location ~ \.php { try_files $uri =404; diff --git a/app/resources/routes/currencies.php b/app/resources/routes/currencies.php index 2d48923..63d4cec 100644 --- a/app/resources/routes/currencies.php +++ b/app/resources/routes/currencies.php @@ -14,6 +14,7 @@ $app->group('/currency/{currency_id}', function($app) { $app->put('/edit[/]', [Currencies::class, 'edit']); $app->delete('/delete[/]', [Currencies::class, 'delete']); $app->group('/values', function($app) { + $app->get('/latest[/]', [Currencies::class, 'latestValue']); $app->post('/add[/]', [Currencies::class, 'addValues']); $app->get('[/]', [Currencies::class, 'getValues']); $app->options('[/]', function (Request $request, Response $response): Response { diff --git a/app/setup/api/settings.php b/app/setup/api/settings.php index ba3a6e0..ab9de6d 100644 --- a/app/setup/api/settings.php +++ b/app/setup/api/settings.php @@ -1,7 +1,15 @@ load(); +} return [ + 'base_path' => $_ENV['BASE_PATH'] ?? null, + 'base_url' => $_ENV['BASE_URL'] ?? null, 'locations' => DI\decorate(function($prev, Container $container) { $arr = (array) $prev; $arr['base'] = dirname(__DIR__, 2); diff --git a/app/setup/app.php b/app/setup/app.php index 2140be2..e695f41 100644 --- a/app/setup/app.php +++ b/app/setup/app.php @@ -44,7 +44,7 @@ $container = $builder->build(); $app = Bridge::create($container); include_once 'databases.php'; -if ($container->has('base_url')) { +if ($container->has('base_url') and $container->get('base_path') !== null) { $app->setBasePath($container->get('base_url')); } $app->add(new ProVM\Money\Common\Middleware\Cors()); diff --git a/docker-compose.yml b/docker-compose.yml index 771459a..019a5e0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,6 +9,8 @@ services: volumes: - .:/code - ./app/docker/nginx.conf:/etc/nginx/conf.d/default.conf + depends_on: + - app-php app-php: container_name: money_app_php @@ -28,6 +30,8 @@ services: volumes: - .:/code - ./ui/docker/nginx.conf:/etc/nginx/conf.d/default.conf + depends_on: + - ui-php ui-php: container_name: money_ui_php @@ -39,14 +43,6 @@ services: ports: - 9124:9000 - ui-gulp: - container_name: money_ui_gulp - build: - context: ./ui/docker - dockerfile: GULP.Dockerfile - volumes: - - ./ui:/app - db: container_name: money_db image: mariadb:latest diff --git a/src/Currency.php b/src/Currency.php index 7be20d4..bedadd7 100644 --- a/src/Currency.php +++ b/src/Currency.php @@ -16,9 +16,21 @@ class Currency extends Model { public function values(): ?array { if ($this->values === null) { $this->values = $this->parentOf(Value::class, [Model::CHILD_KEY => 'currency_id']); + if ($this->values) { + usort($this->values, function($a, $b) { + return $b->dateTime()->timestamp - $a->dateTime()->timestamp; + }); + } } return $this->values; } + protected $latest; + public function latest(): ?Value { + if ($this->latest === null) { + $this->latest = $this->values()[0]; + } + return $this->latest; + } protected static $fields = ['code', 'name']; public static function add(ModelFactory $factory, $info) {