This commit is contained in:
2021-03-19 22:49:09 -03:00
parent ec44b0281e
commit 99c18cb871
8 changed files with 45 additions and 11 deletions

View File

@ -115,4 +115,19 @@ class Currencies {
} }
return $this->withJson($response, $output); 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);
}
} }

View File

@ -89,10 +89,10 @@ class Values {
return $this->withJson($response, $output); return $this->withJson($response, $output);
} }
public function delete(Request $request, Response $response, ModelFactory $factory, $currency_id, $base_id, $date_time = null): Response { 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) { if ($date_time !== null) {
$value = $factory->find(Value::class)->where([['currency_id', $currency_id], ['base_id', $base_id], ['date_time', $date_time]])->one(); $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(); $output['value'] = $value->asArray();
$status = $value->delete(); $status = $value->delete();
$output['deleted'] = $status; $output['deleted'] = $status;

View File

@ -50,6 +50,8 @@ server {
} }
add_header 'Access-Control-Allow-Origin' 'http://localhost:8080'; 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 { location ~ \.php {
try_files $uri =404; try_files $uri =404;

View File

@ -14,6 +14,7 @@ $app->group('/currency/{currency_id}', function($app) {
$app->put('/edit[/]', [Currencies::class, 'edit']); $app->put('/edit[/]', [Currencies::class, 'edit']);
$app->delete('/delete[/]', [Currencies::class, 'delete']); $app->delete('/delete[/]', [Currencies::class, 'delete']);
$app->group('/values', function($app) { $app->group('/values', function($app) {
$app->get('/latest[/]', [Currencies::class, 'latestValue']);
$app->post('/add[/]', [Currencies::class, 'addValues']); $app->post('/add[/]', [Currencies::class, 'addValues']);
$app->get('[/]', [Currencies::class, 'getValues']); $app->get('[/]', [Currencies::class, 'getValues']);
$app->options('[/]', function (Request $request, Response $response): Response { $app->options('[/]', function (Request $request, Response $response): Response {

View File

@ -1,7 +1,15 @@
<?php <?php
use Psr\Container\ContainerInterface as Container; use Psr\Container\ContainerInterface as Container;
use Dotenv\Dotenv;
if (file_exists(implode(DIRECTORY_SEPARATOR, [dirname(__DIR__, 2), '.env']))) {
$dotenv = Dotenv::createImmutable(dirname(__DIR__, 2));
$dotenv->load();
}
return [ return [
'base_path' => $_ENV['BASE_PATH'] ?? null,
'base_url' => $_ENV['BASE_URL'] ?? null,
'locations' => DI\decorate(function($prev, Container $container) { 'locations' => DI\decorate(function($prev, Container $container) {
$arr = (array) $prev; $arr = (array) $prev;
$arr['base'] = dirname(__DIR__, 2); $arr['base'] = dirname(__DIR__, 2);

View File

@ -44,7 +44,7 @@ $container = $builder->build();
$app = Bridge::create($container); $app = Bridge::create($container);
include_once 'databases.php'; 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->setBasePath($container->get('base_url'));
} }
$app->add(new ProVM\Money\Common\Middleware\Cors()); $app->add(new ProVM\Money\Common\Middleware\Cors());

View File

@ -9,6 +9,8 @@ services:
volumes: volumes:
- .:/code - .:/code
- ./app/docker/nginx.conf:/etc/nginx/conf.d/default.conf - ./app/docker/nginx.conf:/etc/nginx/conf.d/default.conf
depends_on:
- app-php
app-php: app-php:
container_name: money_app_php container_name: money_app_php
@ -28,6 +30,8 @@ services:
volumes: volumes:
- .:/code - .:/code
- ./ui/docker/nginx.conf:/etc/nginx/conf.d/default.conf - ./ui/docker/nginx.conf:/etc/nginx/conf.d/default.conf
depends_on:
- ui-php
ui-php: ui-php:
container_name: money_ui_php container_name: money_ui_php
@ -39,14 +43,6 @@ services:
ports: ports:
- 9124:9000 - 9124:9000
ui-gulp:
container_name: money_ui_gulp
build:
context: ./ui/docker
dockerfile: GULP.Dockerfile
volumes:
- ./ui:/app
db: db:
container_name: money_db container_name: money_db
image: mariadb:latest image: mariadb:latest

View File

@ -16,9 +16,21 @@ class Currency extends Model {
public function values(): ?array { public function values(): ?array {
if ($this->values === null) { if ($this->values === null) {
$this->values = $this->parentOf(Value::class, [Model::CHILD_KEY => 'currency_id']); $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; 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']; protected static $fields = ['code', 'name'];
public static function add(ModelFactory $factory, $info) { public static function add(ModelFactory $factory, $info) {