API 1.0.0-rc
This commit is contained in:
71
api/common/Controller/Bancos.php
Normal file
71
api/common/Controller/Bancos.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
namespace Contabilidad\Common\Controller;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use ProVM\Common\Define\Controller\Json;
|
||||
use ProVM\Common\Factory\Model as Factory;
|
||||
use Contabilidad\Banco;
|
||||
|
||||
class Bancos {
|
||||
use Json;
|
||||
|
||||
public function __invoke(Request $request, Response $response, Factory $factory): Response {
|
||||
$bancos = $factory->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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user