From 6172da2f9584a37872261ed640764fb39f07d03d Mon Sep 17 00:00:00 2001 From: Aldarien Date: Tue, 27 Jul 2021 22:29:56 -0400 Subject: [PATCH] API 1.0.0-rc --- api/Dockerfile | 5 ++ api/common/Controller/Bancos.php | 71 ++++++++++++++++++++ api/common/Controller/Base.php | 14 ++++ api/common/Controller/Categorias.php | 89 ++++++++++++++++++++++++ api/common/Controller/Cuentas.php | 93 ++++++++++++++++++++++++++ api/common/Controller/Entradas.php | 66 ++++++++++++++++++ api/common/Controller/Fuentes.php | 84 +++++++++++++++++++++++ api/common/Controller/TiposFuentes.php | 89 ++++++++++++++++++++++++ api/common/Controller/Workers.php | 21 ++++++ api/composer.json | 44 ++++++++++++ api/nginx.conf | 22 ++++++ api/public/index.php | 7 ++ api/resources/routes/bancos.php | 12 ++++ api/resources/routes/base.php | 4 ++ api/resources/routes/categorias.php | 13 ++++ api/resources/routes/cuentas.php | 13 ++++ api/resources/routes/entradas.php | 12 ++++ api/resources/routes/fuentes.php | 13 ++++ api/resources/routes/tipos_fuentes.php | 13 ++++ api/resources/routes/workers.php | 6 ++ api/setup/app.php | 46 +++++++++++++ api/setup/composer.php | 6 ++ api/setup/databases.php | 37 ++++++++++ api/setup/router.php | 9 +++ api/setup/settings/01_env.php | 4 ++ api/setup/settings/02_common.php | 19 ++++++ api/setup/settings/03_database.php | 28 ++++++++ api/src/Banco.php | 13 ++++ api/src/Categoria.php | 21 ++++++ api/src/Cuenta.php | 36 ++++++++++ api/src/Entrada.php | 38 +++++++++++ api/src/Fuente.php | 44 ++++++++++++ api/src/TipoFuente.php | 21 ++++++ 33 files changed, 1013 insertions(+) create mode 100644 api/Dockerfile create mode 100644 api/common/Controller/Bancos.php create mode 100644 api/common/Controller/Base.php create mode 100644 api/common/Controller/Categorias.php create mode 100644 api/common/Controller/Cuentas.php create mode 100644 api/common/Controller/Entradas.php create mode 100644 api/common/Controller/Fuentes.php create mode 100644 api/common/Controller/TiposFuentes.php create mode 100644 api/common/Controller/Workers.php create mode 100644 api/composer.json create mode 100644 api/nginx.conf create mode 100644 api/public/index.php create mode 100644 api/resources/routes/bancos.php create mode 100644 api/resources/routes/base.php create mode 100644 api/resources/routes/categorias.php create mode 100644 api/resources/routes/cuentas.php create mode 100644 api/resources/routes/entradas.php create mode 100644 api/resources/routes/fuentes.php create mode 100644 api/resources/routes/tipos_fuentes.php create mode 100644 api/resources/routes/workers.php create mode 100644 api/setup/app.php create mode 100644 api/setup/composer.php create mode 100644 api/setup/databases.php create mode 100644 api/setup/router.php create mode 100644 api/setup/settings/01_env.php create mode 100644 api/setup/settings/02_common.php create mode 100644 api/setup/settings/03_database.php create mode 100644 api/src/Banco.php create mode 100644 api/src/Categoria.php create mode 100644 api/src/Cuenta.php create mode 100644 api/src/Entrada.php create mode 100644 api/src/Fuente.php create mode 100644 api/src/TipoFuente.php diff --git a/api/Dockerfile b/api/Dockerfile new file mode 100644 index 0000000..1a55712 --- /dev/null +++ b/api/Dockerfile @@ -0,0 +1,5 @@ +FROM php:8-fpm + +RUN docker-php-ext-install pdo pdo_mysql + +WORKDIR /app diff --git a/api/common/Controller/Bancos.php b/api/common/Controller/Bancos.php new file mode 100644 index 0000000..968cf05 --- /dev/null +++ b/api/common/Controller/Bancos.php @@ -0,0 +1,71 @@ +find(Banco::class)->array(); + if ($bancos) { + usort($bancos, function($a, $b) { + return strcmp($a['nombre'], $b['nombre']); + }); + } + $output = [ + 'bancos' => $bancos + ]; + return $this->withJson($response, $output); + } + public function show(Request $request, Response $response, Factory $factory, $banco_id): Response { + $banco = $factory->find(Banco::class)->one($banco_id); + $output = [ + 'input' => $banco_id, + 'banco' => $banco?->toArray() + ]; + return $this->withJson($response, $output); + } + public function add(Request $request, Response $response, Factory $factory): Response { + $input = json_decode($request->getBody()); + $results = []; + if (is_array($input)) { + foreach ($input as $in) { + $banco = Banco::add($factory, $in); + $results []= ['banco' => $banco?->toArray(), 'agregado' => $banco?->save()]; + } + } else { + $banco = Banco::add($factory, $input); + $results []= ['banco' => $banco?->toArray(), 'agregado' => $banco?->save()]; + } + $output = [ + 'input' => $input, + 'bancos' => $results + ]; + return $this->withJson($response, $output); + } + public function edit(Request $request, Response $response, Factory $factory, $banco_id): Response { + $banco = $factory->find(Banco::class)->one($banco_id); + $output = [ + 'input' => $banco_id, + 'old' => $banco->toArray() + ]; + $input = json_decode($request->getBody()); + $banco->edit($input); + $output['banco'] = $banco->toArray(); + return $this->withJson($response, $output); + } + public function delete(Request $request, Response $response, Factory $factory, $banco_id): Response { + $banco = $factory->find(Banco::class)->one($banco_id); + $output = [ + 'input' => $banco_id, + 'banco' => $banco->toArray(), + 'eliminado' => $banco->delete() + ]; + return $this->withJson($response, $output); + } +} diff --git a/api/common/Controller/Base.php b/api/common/Controller/Base.php new file mode 100644 index 0000000..c2b299d --- /dev/null +++ b/api/common/Controller/Base.php @@ -0,0 +1,14 @@ +withJson($response, []); + } +} diff --git a/api/common/Controller/Categorias.php b/api/common/Controller/Categorias.php new file mode 100644 index 0000000..7809ec7 --- /dev/null +++ b/api/common/Controller/Categorias.php @@ -0,0 +1,89 @@ +find(Categoria::class)->array(); + if ($categorias) { + usort($categorias, function($a, $b) { + return strcmp($a['nombre'], $b['nombre']); + }); + } + $output = [ + 'categorias' => $categorias + ]; + return $this->withJson($response, $output); + } + public function show(Request $request, Response $response, Factory $factory, $categoria_id): Response { + $categoria = $factory->find(Categoria::class)->one($categoria_id); + $output = [ + 'input' => $categoria_id, + 'categoria' => $categoria?->toArray() + ]; + return $this->withJson($response, $output); + } + public function add(Request $request, Response $response, Factory $factory): Response { + $input = json_decode($request->getBody()); + $results = []; + if (is_array($input)) { + foreach ($input as $in) { + $categoria = Categoria::add($factory, $in); + $results []= ['categoria' => $categoria?->toArray(), 'agregado' => $categoria?->save()]; + } + } else { + $categoria = Categoria::add($factory, $input); + $results []= ['categoria' => $categoria?->toArray(), 'agregado' => $categoria?->save()]; + } + $output = [ + 'input' => $input, + 'categorias' => $results + ]; + return $this->withJson($response, $output); + } + public function edit(Request $request, Response $response, Factory $factory, $categoria_id): Response { + $categoria = $factory->find(Categoria::class)->one($categoria_id); + $output = [ + 'input' => $categoria_id, + 'old' => $categoria->toArray() + ]; + $input = json_decode($request->getBody()); + $categoria->edit($input); + $output['categoria'] = $categoria->toArray(); + return $this->withJson($response, $output); + } + public function delete(Request $request, Response $response, Factory $factory, $categoria_id): Response { + $categoria = $factory->find(Categoria::class)->one($categoria_id); + $output = [ + 'input' => $categoria_id, + 'categoria' => $categoria->toArray(), + 'eliminado' => $categoria->delete() + ]; + return $this->withJson($response, $output); + } + public function cuentas(Request $request, Response $response, Factory $factory, $categoria_id): Response { + $categoria = $factory->find(Categoria::class)->one($categoria_id); + $cuentas = null; + if ($categoria !== null) { + $cuentas = $categoria->cuentas(); + if ($cuentas !== null) { + array_walk($cuentas, function(&$item) { + $item = $item->toArray(); + }); + } + } + $output = [ + 'input' => $categoria_id, + 'categoria' => $categoria?->toArray(), + 'cuentas' => $cuentas + ]; + return $this->withJson($response, $output); + } +} diff --git a/api/common/Controller/Cuentas.php b/api/common/Controller/Cuentas.php new file mode 100644 index 0000000..49634cf --- /dev/null +++ b/api/common/Controller/Cuentas.php @@ -0,0 +1,93 @@ +find(Cuenta::class)->array(); + if ($cuentas) { + usort($cuentas, function($a, $b) { + $c = strcmp($a['categoria']['nombre'], $b['categoria']['nombre']); + if ($c == 0) { + return strcmp($a['nombre'], $b['nombre']); + } + return $c; + }); + } + $output = [ + 'cuentas' => $cuentas + ]; + return $this->withJson($response, $output); + } + public function show(Request $request, Response $response, Factory $factory, $cuenta_id): Response { + $cuenta = $factory->find(Cuenta::class)->one($cuenta_id); + $output = [ + 'input' => $cuenta_id, + 'cuenta' => $cuenta?->toArray() + ]; + return $this->withJson($response, $output); + } + public function add(Request $request, Response $response, Factory $factory): Response { + $input = json_decode($request->getBody()); + $results = []; + if (is_array($input)) { + foreach ($input as $in) { + $cuenta = Cuenta::add($factory, $in); + $results []= ['cuenta' => $cuenta?->toArray(), 'agregado' => $cuenta?->save()]; + } + } else { + $cuenta = Cuenta::add($factory, $input); + $results []= ['cuenta' => $cuenta?->toArray(), 'agregado' => $cuenta?->save()]; + } + $output = [ + 'input' => $input, + 'cuentas' => $results + ]; + return $this->withJson($response, $output); + } + public function edit(Request $request, Response $response, Factory $factory, $cuenta_id): Response { + $cuenta = $factory->find(Cuenta::class)->one($cuenta_id); + $output = [ + 'input' => $cuenta_id, + 'old' => $cuenta->toArray() + ]; + $input = json_decode($request->getBody()); + $cuenta->edit($input); + $output['cuenta'] = $cuenta->toArray(); + return $this->withJson($response, $output); + } + public function delete(Request $request, Response $response, Factory $factory, $cuenta_id): Response { + $cuenta = $factory->find(Cuenta::class)->one($cuenta_id); + $output = [ + 'input' => $cuenta_id, + 'cuenta' => $cuenta->toArray(), + 'eliminado' => $cuenta->delete() + ]; + return $this->withJson($response, $output); + } + public function entradas(Request $request, Response $response, Factory $factory, $cuenta_id): Response { + $cuenta = $factory->find(Cuenta::class)->one($cuenta_id); + $entradas = null; + if ($cuenta !== null) { + $entradas = $cuenta->entradas(); + if ($entradas !== null) { + array_walk($entradas, function(&$item) { + $item = $item->toArray(); + }); + } + } + $output = [ + 'input' => $cuenta_id, + 'cuenta' => $cuenta?->toArray(), + 'entradas' => $entradas + ]; + return $this->withJson($response, $output); + } +} diff --git a/api/common/Controller/Entradas.php b/api/common/Controller/Entradas.php new file mode 100644 index 0000000..6621a0f --- /dev/null +++ b/api/common/Controller/Entradas.php @@ -0,0 +1,66 @@ +find(Entrada::class)->array(); + $output = [ + 'entradas' => $entradas + ]; + return $this->withJson($response, $output); + } + public function show(Request $request, Response $response, Factory $factory, $entrada_id): Response { + $entrada = $factory->find(Entrada::class)->one($entrada_id); + $output = [ + 'input' => $entrada_id, + 'entrada' => $entrada?->toArray() + ]; + return $this->withJson($response, $output); + } + public function add(Request $request, Response $response, Factory $factory): Response { + $input = json_decode($request->getBody()); + $results = []; + if (is_array($input)) { + foreach ($input as $in) { + $entrada = Entrada::add($factory, $in); + $results []= ['entrada' => $entrada?->toArray(), 'agregado' => $entrada?->save()]; + } + } else { + $entrada = Entrada::add($factory, $input); + $results []= ['entrada' => $entrada?->toArray(), 'agregado' => $entrada?->save()]; + } + $output = [ + 'input' => $input, + 'entradas' => $results + ]; + return $this->withJson($response, $output); + } + public function edit(Request $request, Response $response, Factory $factory, $entrada_id): Response { + $entrada = $factory->find(Entrada::class)->one($entrada_id); + $output = [ + 'input' => $entrada_id, + 'old' => $entrada->toArray() + ]; + $input = json_decode($request->getBody()); + $entrada->edit($input); + $output['entrada'] = $entrada->toArray(); + return $this->withJson($response, $output); + } + public function delete(Request $request, Response $response, Factory $factory, $entrada_id): Response { + $entrada = $factory->find(Entrada::class)->one($entrada_id); + $output = [ + 'input' => $entrada_id, + 'entrada' => $entrada->toArray(), + 'eliminado' => $entrada->delete() + ]; + return $this->withJson($response, $output); + } +} diff --git a/api/common/Controller/Fuentes.php b/api/common/Controller/Fuentes.php new file mode 100644 index 0000000..5ccb223 --- /dev/null +++ b/api/common/Controller/Fuentes.php @@ -0,0 +1,84 @@ +find(Fuente::class)->array(); + $output = [ + 'fuentes' => $fuentes + ]; + return $this->withJson($response, $output); + } + public function show(Request $request, Response $response, Factory $factory, $fuente_id): Response { + $fuente = $factory->find(Fuente::class)->one($fuente_id); + $output = [ + 'input' => $fuente_id, + 'fuente' => $fuente?->toArray() + ]; + return $this->withJson($response, $output); + } + public function add(Request $request, Response $response, Factory $factory): Response { + $input = json_decode($request->getBody()); + $results = []; + if (is_array($input)) { + foreach ($input as $in) { + $fuente = Fuente::add($factory, $in); + $results []= ['fuente' => $fuente?->toArray(), 'agregado' => $fuente?->save()]; + } + } else { + $fuente = Fuente::add($factory, $input); + $results []= ['fuente' => $fuente?->toArray(), 'agregado' => $fuente?->save()]; + } + $output = [ + 'input' => $input, + 'fuentes' => $results + ]; + return $this->withJson($response, $output); + } + public function edit(Request $request, Response $response, Factory $factory, $fuente_id): Response { + $fuente = $factory->find(Fuente::class)->one($fuente_id); + $output = [ + 'input' => $fuente_id, + 'old' => $fuente->toArray() + ]; + $input = json_decode($request->getBody()); + $fuente->edit($input); + $output['fuente'] = $fuente->toArray(); + return $this->withJson($response, $output); + } + public function delete(Request $request, Response $response, Factory $factory, $fuente_id): Response { + $fuente = $factory->find(Fuente::class)->one($fuente_id); + $output = [ + 'input' => $fuente_id, + 'fuente' => $fuente->toArray(), + 'eliminado' => $fuente->delete() + ]; + return $this->withJson($response, $output); + } + public function entradas(Request $request, Response $response, Factory $factory, $fuente_id): Response { + $fuente = $factory->find(Fuente::class)->one($fuente_id); + $entradas = null; + if ($fuente !== null) { + $entradas = $fuente->entradas(); + if ($entradas !== null) { + array_walk($entradas, function(&$item) { + $item = $item->toArray(); + }); + } + } + $output = [ + 'input' => $fuente_id, + 'fuente' => $fuente?->toArray(), + 'entradas' => $entradas + ]; + return $this->withJson($response, $output); + } +} diff --git a/api/common/Controller/TiposFuentes.php b/api/common/Controller/TiposFuentes.php new file mode 100644 index 0000000..030eb3f --- /dev/null +++ b/api/common/Controller/TiposFuentes.php @@ -0,0 +1,89 @@ +find(TipoFuente::class)->array(); + if ($tipos_fuentes) { + usort($tipos_fuentes, function($a, $b) { + return strcmp($a['descripcion'], $b['descripcion']); + }); + } + $output = [ + 'tipos_fuentes' => $tipos_fuentes + ]; + return $this->withJson($response, $output); + } + public function show(Request $request, Response $response, Factory $factory, $tipo_fuente_id): Response { + $tipo_fuente = $factory->find(TipoFuente::class)->one($tipo_fuente_id); + $output = [ + 'input' => $tipo_fuente_id, + 'tipo_fuente' => $tipo_fuente?->toArray() + ]; + return $this->withJson($response, $output); + } + public function add(Request $request, Response $response, Factory $factory): Response { + $input = json_decode($request->getBody()); + $results = []; + if (is_array($input)) { + foreach ($input as $in) { + $tipo_fuente = TipoFuente::add($factory, $in); + $results []= ['tipo_fuente' => $tipo_fuente?->toArray(), 'agregado' => $tipo_fuente?->save()]; + } + } else { + $tipo_fuente = TipoFuente::add($factory, $input); + $results []= ['tipo_fuente' => $tipo_fuente?->toArray(), 'agregado' => $tipo_fuente?->save()]; + } + $output = [ + 'input' => $input, + 'tipo_fuentes' => $results + ]; + return $this->withJson($response, $output); + } + public function edit(Request $request, Response $response, Factory $factory, $tipo_fuente_id): Response { + $tipo_fuente = $factory->find(TipoFuente::class)->one($tipo_fuente_id); + $output = [ + 'input' => $tipo_fuente_id, + 'old' => $tipo_fuente->toArray() + ]; + $input = json_decode($request->getBody()); + $tipo_fuente->edit($input); + $output['tipo_fuente'] = $tipo_fuente->toArray(); + return $this->withJson($response, $output); + } + public function delete(Request $request, Response $response, Factory $factory, $tipo_fuente_id): Response { + $tipo_fuente = $factory->find(TipoFuente::class)->one($tipo_fuente_id); + $output = [ + 'input' => $tipo_fuente_id, + 'tipo_fuente' => $tipo_fuente->toArray(), + 'eliminado' => $tipo_fuente->delete() + ]; + return $this->withJson($response, $output); + } + public function fuentes(Request $request, Response $response, Factory $factory, $tipo_fuente_id): Response { + $tipo_fuente = $factory->find(TipoFuente::class)->one($tipo_fuente_id); + $fuentes = null; + if ($tipo_fuente !== null) { + $fuentes = $tipo_fuente->fuentes(); + if ($fuentes !== null) { + array_walk($fuentes, function(&$item) { + $item = $item->toArray(); + }); + } + } + $output = [ + 'input' => $tipo_fuente_id, + 'tipo_fuente' => $tipo_fuente?->toArray(), + 'fuentes' => $fuentes + ]; + return $this->withJson($response, $output); + } +} diff --git a/api/common/Controller/Workers.php b/api/common/Controller/Workers.php new file mode 100644 index 0000000..bebbd82 --- /dev/null +++ b/api/common/Controller/Workers.php @@ -0,0 +1,21 @@ +getBody(); + $result = $handler->register($post); + $output = [ + 'input' => $post, + 'result' => $result + ]; + return $this->withJson($response, $output); + } +} diff --git a/api/composer.json b/api/composer.json new file mode 100644 index 0000000..8d7b004 --- /dev/null +++ b/api/composer.json @@ -0,0 +1,44 @@ +{ + "name": "provm/contabilidad-api", + "description": "API para contabilidad", + "type": "project", + "require": { + "php-di/php-di": "^6.3", + "php-di/slim-bridge": "^3.1", + "nyholm/psr7": "^1.4", + "nyholm/psr7-server": "^1.0", + "zeuxisoo/slim-whoops": "^0.7.3", + "provm/controller": "^1.0", + "provm/models": "1.0.0-rc2", + "nesbot/carbon": "^2.50" + }, + "require-dev": { + "phpunit/phpunit": "^9.5", + "kint-php/kint": "^3.3" + }, + "authors": [ + { + "name": "Aldarien", + "email": "aldarien85@gmail.com" + } + ], + "autoload": { + "psr-4": { + "Contabilidad\\Common\\": "common", + "Contabilidad\\": "src" + } + }, + "repositories": [ + { + "type": "git", + "url": "http://git.provm.cl/ProVM/controller.git" + }, + { + "type": "git", + "url": "http://git.provm.cl/ProVM/models.git" + } + ], + "config": { + "secure-http": false + } +} diff --git a/api/nginx.conf b/api/nginx.conf new file mode 100644 index 0000000..7eed302 --- /dev/null +++ b/api/nginx.conf @@ -0,0 +1,22 @@ +server { + listen 80; + index index.php index.html index.htm; + error_log /var/log/nginx/error.log; + access_log /var/log/nginx/access.log; + root /app/public; + + location / { + try_files $uri $uri/ /index.php?$query_string; + } + + location ~ \.php$ { + try_files $uri =404; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass api:9000; + fastcgi_index index.php; + include fastcgi_params; + fastcgi_param REQUEST_URI $request_uri; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + } +} diff --git a/api/public/index.php b/api/public/index.php new file mode 100644 index 0000000..e6d2557 --- /dev/null +++ b/api/public/index.php @@ -0,0 +1,7 @@ +run(); diff --git a/api/resources/routes/bancos.php b/api/resources/routes/bancos.php new file mode 100644 index 0000000..ec73cc6 --- /dev/null +++ b/api/resources/routes/bancos.php @@ -0,0 +1,12 @@ +group('/bancos', function($app) { + $app->post('/add[/]', [Bancos::class, 'add']); + $app->get('[/]', Bancos::class); +}); +$app->group('/banco/{banco_id}', function($app) { + $app->put('/edit', [Bancos::class, 'edit']); + $app->delete('/delete', [Bancos::class, 'delete']); + $app->get('[/]', [Bancos::class, 'show']); +}); diff --git a/api/resources/routes/base.php b/api/resources/routes/base.php new file mode 100644 index 0000000..c1d1538 --- /dev/null +++ b/api/resources/routes/base.php @@ -0,0 +1,4 @@ +get('/', Base::class); diff --git a/api/resources/routes/categorias.php b/api/resources/routes/categorias.php new file mode 100644 index 0000000..361cfae --- /dev/null +++ b/api/resources/routes/categorias.php @@ -0,0 +1,13 @@ +group('/categorias', function($app) { + $app->post('/add[/]', [Categorias::class, 'add']); + $app->get('[/]', Categorias::class); +}); +$app->group('/categoria/{categoria_id}', function($app) { + $app->get('/cuentas', [Categorias::class, 'cuentas']); + $app->put('/edit', [Categorias::class, 'edit']); + $app->delete('/delete', [Categorias::class, 'delete']); + $app->get('[/]', [Categorias::class, 'show']); +}); diff --git a/api/resources/routes/cuentas.php b/api/resources/routes/cuentas.php new file mode 100644 index 0000000..b68375e --- /dev/null +++ b/api/resources/routes/cuentas.php @@ -0,0 +1,13 @@ +group('/cuentas', function($app) { + $app->post('/add[/]', [Cuentas::class, 'add']); + $app->get('[/]', Cuentas::class); +}); +$app->group('/cuenta/{cuenta_id}', function($app) { + $app->get('/entradas', [Cuentas::class, 'entradas']); + $app->put('/edit', [Cuentas::class, 'edit']); + $app->delete('/delete', [Cuentas::class, 'delete']); + $app->get('[/]', [Cuentas::class, 'show']); +}); diff --git a/api/resources/routes/entradas.php b/api/resources/routes/entradas.php new file mode 100644 index 0000000..aa41b03 --- /dev/null +++ b/api/resources/routes/entradas.php @@ -0,0 +1,12 @@ +group('/entradas', function($app) { + $app->post('/add[/]', [Entradas::class, 'add']); + $app->get('[/]', Entradas::class); +}); +$app->group('/entrada/{entrada_id}', function($app) { + $app->put('/edit', [Entradas::class, 'edit']); + $app->delete('/delete', [Entradas::class, 'delete']); + $app->get('[/]', [Entradas::class, 'show']); +}); diff --git a/api/resources/routes/fuentes.php b/api/resources/routes/fuentes.php new file mode 100644 index 0000000..20c2a94 --- /dev/null +++ b/api/resources/routes/fuentes.php @@ -0,0 +1,13 @@ +group('/fuentes', function($app) { + $app->post('/add[/]', [Fuentes::class, 'add']); + $app->get('[/]', Fuentes::class); +}); +$app->group('/fuente/{fuente_id}', function($app) { + $app->get('/entradas', [Fuentes::class, 'entradas']); + $app->put('/edit', [Fuentes::class, 'edit']); + $app->delete('/delete', [Fuentes::class, 'delete']); + $app->get('[/]', [Fuentes::class, 'show']); +}); diff --git a/api/resources/routes/tipos_fuentes.php b/api/resources/routes/tipos_fuentes.php new file mode 100644 index 0000000..5af20ae --- /dev/null +++ b/api/resources/routes/tipos_fuentes.php @@ -0,0 +1,13 @@ +group('/tipos_fuentes', function($app) { + $app->post('/add[/]', [TiposFuentes::class, 'add']); + $app->get('[/]', TiposFuentes::class); +}); +$app->group('/tipo_fuente/{tipo_fuente_id}', function($app) { + $app->get('/fuentes', [TiposFuentes::class, 'fuentes']); + $app->put('/edit', [TiposFuentes::class, 'edit']); + $app->delete('/delete', [TiposFuentes::class, 'delete']); + $app->get('[/]', [TiposFuentes::class, 'show']); +}); diff --git a/api/resources/routes/workers.php b/api/resources/routes/workers.php new file mode 100644 index 0000000..acf34a9 --- /dev/null +++ b/api/resources/routes/workers.php @@ -0,0 +1,6 @@ +group('/workers', function($app) { + $app->post('/register', [Workers::class, 'register']); +}); diff --git a/api/setup/app.php b/api/setup/app.php new file mode 100644 index 0000000..ba8e133 --- /dev/null +++ b/api/setup/app.php @@ -0,0 +1,46 @@ +isDir()) { + continue; + } + $builder->addDefinitions($file->getRealPath()); + } +} + +$container = $builder->build(); +$app = Bridge::create($container); + +$app->addRoutingMiddleware(); +$app->add(new WhoopsMiddleware()); + + +$folder = 'middlewares'; +if (file_exists($folder)) { + $files = new DirectoryIterator($folder); + foreach ($files as $file) { + if ($file->isDir() or $file->getExtension() != 'php') { + continue; + } + include_once $file->getRealPath(); + } +} + +include_once 'databases.php'; +include_once 'router.php'; diff --git a/api/setup/composer.php b/api/setup/composer.php new file mode 100644 index 0000000..c8fb135 --- /dev/null +++ b/api/setup/composer.php @@ -0,0 +1,6 @@ +getContainer()->get('databases'); + +foreach ($databases->databases as $name => $settings) { + if (!is_object($settings)) { + continue; + } + $auth = false; + $dsn = ''; + switch (strtolower($settings->type)) { + case 'mysql': + $data = [ + ['host', $settings->host->name], + ['dbname', $settings->name] + ]; + if (isset($settings->host->port)) { + $data []= ['port', $settings->host->port]; + } + array_walk($data, function(&$item) { + $item = implode('=', $item); + }); + $dsn = implode(':', [ + 'mysql', + implode(';', $data) + ]); + $auth = true; + break; + } + ORM::configure($dsn, null, $name); + if ($auth) { + ORM::configure('username', $settings->user->name, $name); + ORM::configure('password', $settings->user->password, $name); + } +} +if (isset($databases->short_names) and $databases->short_names) { + Model::$short_table_names = true; +} diff --git a/api/setup/router.php b/api/setup/router.php new file mode 100644 index 0000000..5fee462 --- /dev/null +++ b/api/setup/router.php @@ -0,0 +1,9 @@ +getContainer()->get('folders')->routes; +$files = new DirectoryIterator($folder); +foreach ($files as $file) { + if ($file->isDir() or $file->getExtension() != 'php') { + continue; + } + include_once $file->getRealPath(); +} diff --git a/api/setup/settings/01_env.php b/api/setup/settings/01_env.php new file mode 100644 index 0000000..161b2d4 --- /dev/null +++ b/api/setup/settings/01_env.php @@ -0,0 +1,4 @@ + $_ENV['DEBUG'] ?? false +]; diff --git a/api/setup/settings/02_common.php b/api/setup/settings/02_common.php new file mode 100644 index 0000000..8ef013b --- /dev/null +++ b/api/setup/settings/02_common.php @@ -0,0 +1,19 @@ + function(Container $c) { + $arr = [ + 'base' => dirname(__DIR__, 2) + ]; + $arr['resources'] = implode(DIRECTORY_SEPARATOR, [ + $arr['base'], + 'resources' + ]); + $arr['routes'] = implode(DIRECTORY_SEPARATOR, [ + $arr['resources'], + 'routes' + ]); + return (object) $arr; + } +]; diff --git a/api/setup/settings/03_database.php b/api/setup/settings/03_database.php new file mode 100644 index 0000000..4965d1a --- /dev/null +++ b/api/setup/settings/03_database.php @@ -0,0 +1,28 @@ + function() { + $arr = [ + ORM::DEFAULT_CONNECTION => [ + 'type' => 'mysql', + 'host' => [ + 'name' => $_ENV['MYSQL_HOST'] ?? 'db' + ], + 'user' => [ + 'name' => $_ENV['MYSQL_USER'], + 'password' => $_ENV['MYSQL_PASSWORD'] + ], + 'name' => $_ENV['MYSQL_DATABASE'] + ] + ]; + function toObj($arr) { + $obj = (object) $arr; + foreach ($arr as $k => $v) { + if (is_array($v)) { + $obj->{$k} = toObj($v); + } + } + return $obj; + } + return (object) ['databases' => toObj($arr), 'short_names' => true]; + } +]; diff --git a/api/src/Banco.php b/api/src/Banco.php new file mode 100644 index 0000000..971ce42 --- /dev/null +++ b/api/src/Banco.php @@ -0,0 +1,13 @@ +cuentas === null) { + $this->cuentas = $this->parentOf(Cuenta::class, [Model::CHILD_KEY => 'categoria_id']); + } + return $this->cuentas; + } +} diff --git a/api/src/Cuenta.php b/api/src/Cuenta.php new file mode 100644 index 0000000..bf14878 --- /dev/null +++ b/api/src/Cuenta.php @@ -0,0 +1,36 @@ +categoria === null) { + $this->categoria = $this->childOf(Categoria::class, [Model::SELF_KEY => 'categoria_id']); + } + return $this->categoria; + } + + protected $entradas; + public function entradas() { + if ($this->entradas === null) { + $this->entradas = $this->parentOf(Entrada::class, [Model::CHILD_KEY => 'cuenta_id']); + } + return $this->entradas; + } + + public function toArray(): array { + $arr = parent::toArray(); + $arr['categoria'] = $this->categoria()->toArray(); + return $arr; + } +} diff --git a/api/src/Entrada.php b/api/src/Entrada.php new file mode 100644 index 0000000..fa61ce7 --- /dev/null +++ b/api/src/Entrada.php @@ -0,0 +1,38 @@ +fuente === null) { + $this->fuente = $this->childOf(Fuente::class, [Model::SELF_KEY => 'fuente_id']); + } + return $this->fuente; + } + protected $cuenta; + public function cuenta() { + if ($this->cuenta === null) { + $this->cuenta = $this->childOf(Cuente::class, [Model::SELF_KEY => 'cuenta_id']); + } + return $this->cuenta; + } + public function fecha(\DateTime $fecha = null) { + if ($fecha === null) { + return Carbon::parse($this->fecha); + } + $this->fecha = $fecha->format('Y-m-d'); + } +} diff --git a/api/src/Fuente.php b/api/src/Fuente.php new file mode 100644 index 0000000..34ebcc1 --- /dev/null +++ b/api/src/Fuente.php @@ -0,0 +1,44 @@ +tipo === null) { + $this->tipo = $this->childOf(TipoFuente::class, [Model::SELF_KEY => 'tipo_id']); + } + return $this->tipo; + } + protected $banco; + public function banco() { + if ($this->banco === null) { + $this->banco = $this->childOf(Banco::class, [Model::SELF_KEY => 'banco_id']); + } + return $this->banco; + } + + protected $entradas; + public function entradas() { + if ($this->entradas === null) { + $this->entradas = $this->parentOf(Entrada::class, [Model::CHILD_KEY => 'fuente_id']); + } + return $this->entradas; + } + + public function toArray(): array { + $arr = parent::toArray(); + $arr['tipo'] = $this->tipo()->toArray(); + $arr['banco'] = $this->banco()->toArray(); + return $arr; + } +} diff --git a/api/src/TipoFuente.php b/api/src/TipoFuente.php new file mode 100644 index 0000000..8397cf8 --- /dev/null +++ b/api/src/TipoFuente.php @@ -0,0 +1,21 @@ +fuentes === null) { + $this->fuentes = $this->parentOf(Fuente::class, [Model::CHILD_KEY => 'tipo_id']); + } + return $this->fuentes; + } +}