2022-12-20
This commit is contained in:
@ -6,6 +6,7 @@ use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use function Safe\{json_decode, file_get_contents};
|
||||
use ProVM\Alias\Controller\Json;
|
||||
use ProVM\Implement\Path;
|
||||
|
||||
class Base
|
||||
{
|
||||
@ -13,12 +14,8 @@ class Base
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, ContainerInterface $container): ResponseInterface
|
||||
{
|
||||
$folder = $container->get('folders')->documentation;
|
||||
$filename = implode(DIRECTORY_SEPARATOR, [
|
||||
$folder,
|
||||
'base.json'
|
||||
]);
|
||||
$documentation = json_decode(file_get_contents($filename));
|
||||
$filename = $container->get('documentation');
|
||||
$documentation = json_decode(trim(file_get_contents($filename)));
|
||||
return $this->withJson($response, $documentation);
|
||||
}
|
||||
}
|
||||
|
16
api/common/Controller/Categorias.php
Normal file
16
api/common/Controller/Categorias.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
namespace Common\Controller;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use ProVM\Alias\API\Controller;
|
||||
use Contabilidad\Repository\Categoria;
|
||||
|
||||
class Categorias extends Controller
|
||||
{
|
||||
public function setup(ContainerInterface $container): void
|
||||
{
|
||||
$this->setSingular('categoria')
|
||||
->setPlural('categorias')
|
||||
->setRepository($container->get(Categoria::class));
|
||||
}
|
||||
}
|
16
api/common/Controller/Conecciones.php
Normal file
16
api/common/Controller/Conecciones.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
namespace Common\Controller;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use ProVM\Alias\API\Controller;
|
||||
use Contabilidad\Repository\Coneccion;
|
||||
|
||||
class Conecciones extends Controller
|
||||
{
|
||||
public function setup(ContainerInterface $container): void
|
||||
{
|
||||
$this->setSingular('coneccion')
|
||||
->setPlural('conecciones')
|
||||
->setRepository($container->get(Coneccion::class));
|
||||
}
|
||||
}
|
@ -1,125 +1,16 @@
|
||||
<?php
|
||||
namespace Common\Controller;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use PDOException;
|
||||
use function Safe\{json_decode,error_log};
|
||||
use ProVM\Alias\Controller\Json;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use ProVM\Alias\API\Controller;
|
||||
use Contabilidad\Repository\Cuenta;
|
||||
|
||||
class Cuentas
|
||||
class Cuentas extends Controller
|
||||
{
|
||||
use Json;
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Cuenta $repository): ResponseInterface
|
||||
public function setup(ContainerInterface $container): void
|
||||
{
|
||||
$cuentas = [];
|
||||
try {
|
||||
$cuentas = $repository->fetchAll();
|
||||
} catch (PDOException $e) {
|
||||
error_log($e);
|
||||
}
|
||||
return $this->withJson($response, compact('cuentas'));
|
||||
}
|
||||
public function get(ServerRequestInterface $request, ResponseInterface $response, Cuenta $repository, $cuenta_id): ResponseInterface
|
||||
{
|
||||
$cuenta = null;
|
||||
try {
|
||||
$cuenta = $repository->fetchById($cuenta_id);
|
||||
} catch (PDOException $e) {
|
||||
error_log($e);
|
||||
}
|
||||
return $this->withJson($response, compact('cuenta'));
|
||||
}
|
||||
public function add(ServerRequestInterface $request, ResponseInterface $response, Cuenta $repository): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody();
|
||||
$contents = $body->getContents();
|
||||
$json = json_decode($contents);
|
||||
if (!is_array($json)) {
|
||||
$json = [$json];
|
||||
}
|
||||
$output = [
|
||||
'input' => $json,
|
||||
'cuentas' => []
|
||||
];
|
||||
foreach ($json as $data) {
|
||||
$cuenta = $repository->create((array) $data);
|
||||
$status = true;
|
||||
$exists = true;
|
||||
if ($cuenta->isNew()) {
|
||||
$exists = false;
|
||||
try {
|
||||
$repository->save($cuenta);
|
||||
} catch (PDOException $e) {
|
||||
error_log($e);
|
||||
$status = false;
|
||||
}
|
||||
}
|
||||
$output['cuentas'] []= [
|
||||
'cuenta' => $cuenta,
|
||||
'exists' => $exists,
|
||||
'added' => $status
|
||||
];
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function edit(ServerRequestInterface $request, ResponseInterface $response, Cuenta $repository): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody();
|
||||
$contents = $body->getContents();
|
||||
$json = json_decode($contents);
|
||||
if (!is_array($json)) {
|
||||
$json = [$json];
|
||||
}
|
||||
$output = [
|
||||
'input' => $json,
|
||||
'cuentas' => []
|
||||
];
|
||||
foreach ($json as $data) {
|
||||
$cuenta = $repository->fetchById($data->id);
|
||||
$old = clone $cuenta;
|
||||
try {
|
||||
$cuenta->edit((array) $data);
|
||||
$status = $cuenta->isDirty();
|
||||
if ($status) {
|
||||
$repository->save($cuenta);
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
error_log($e);
|
||||
$status = false;
|
||||
}
|
||||
$output['cuentas'] []= [
|
||||
'antes' => $old,
|
||||
'cuenta' => $cuenta,
|
||||
'edited' => $status
|
||||
];
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function editOne(ServerRequestInterface $request, ResponseInterface $response, Cuenta $repository, $cuenta_id): ResponseInterface
|
||||
{
|
||||
$cuenta = $repository->fetchById($cuenta_id);
|
||||
$body = $request->getBody();
|
||||
$contents = $body->getContents();
|
||||
$json = json_decode($contents, JSON_OBJECT_AS_ARRAY);
|
||||
$output = [
|
||||
'input' => $json,
|
||||
'old' => clone $cuenta
|
||||
];
|
||||
try {
|
||||
$cuenta->edit((array) $json);
|
||||
$status = $cuenta->isDirty();
|
||||
if ($status) {
|
||||
$repository->save($cuenta);
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
error_log($e);
|
||||
$status = false;
|
||||
}
|
||||
$output['cuenta'] = $cuenta;
|
||||
$output['edited'] = $status;
|
||||
return $this->withJson($response, $output);
|
||||
$this->setSingular('cuenta')
|
||||
->setPlural('cuentas')
|
||||
->setRepository($container->get(Cuenta::class));
|
||||
}
|
||||
}
|
||||
|
17
api/common/Controller/Estado/Conecciones.php
Normal file
17
api/common/Controller/Estado/Conecciones.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace Common\Controller\Estado;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use ProVM\Alias\API\Controller;
|
||||
use Contabilidad\Repository\Estado\Coneccion;
|
||||
|
||||
class Conecciones extends Controller
|
||||
{
|
||||
|
||||
public function setup(ContainerInterface $container): void
|
||||
{
|
||||
$this->setSingular('estado_coneccion')
|
||||
->setPlural('estados_conecciones')
|
||||
->setRepository($container->get(Coneccion::class));
|
||||
}
|
||||
}
|
16
api/common/Controller/Monedas.php
Normal file
16
api/common/Controller/Monedas.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
namespace Common\Controller;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use ProVM\Alias\API\Controller;
|
||||
use Contabilidad\Repository\Moneda;
|
||||
|
||||
class Monedas extends Controller
|
||||
{
|
||||
public function setup(ContainerInterface $container): void
|
||||
{
|
||||
$this->setSingular('moneda');
|
||||
$this->setPlural('monedas');
|
||||
$this->setRepository($container->get(Moneda::class));
|
||||
}
|
||||
}
|
16
api/common/Controller/Transacciones.php
Normal file
16
api/common/Controller/Transacciones.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
namespace Common\Controller;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use ProVM\Alias\API\Controller;
|
||||
use Contabilidad\Repository\Transaccion;
|
||||
|
||||
class Transacciones extends Controller
|
||||
{
|
||||
public function setup(ContainerInterface $container): void
|
||||
{
|
||||
$this->setSingular('transaccion');
|
||||
$this->setPlural('transacciones');
|
||||
$this->setRepository($container->get(Transaccion::class));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user