API
This commit is contained in:
14
api/common/Controller/Base.php
Normal file
14
api/common/Controller/Base.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Controller;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Incoviba\Common\Define\Controller\Json;
|
||||
|
||||
class Base {
|
||||
use Json;
|
||||
|
||||
public function __invoke(Request $request, Response $response): Response {
|
||||
return $this->withJson($response, []);
|
||||
}
|
||||
}
|
52
api/common/Controller/Operadores.php
Normal file
52
api/common/Controller/Operadores.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Controller;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use \Model;
|
||||
use Incoviba\Common\Define\Controller\Json;
|
||||
use Incoviba\Operador;
|
||||
use Incoviba\TipoAgente;
|
||||
|
||||
class Operadores {
|
||||
use Json;
|
||||
|
||||
public function __invoke(Request $request, Response $response): Response {
|
||||
$tipo = Model::factory(TipoAgente::class)->where('descripcion', 'operador')->find_one();
|
||||
$ids = array_map(function($item) {
|
||||
return $item->agente;
|
||||
}, $tipo->agente_tipos());
|
||||
$operadores = [];
|
||||
foreach ($ids as $id) {
|
||||
$operadores []= Model::factory(Operador::class)->find_one($id);
|
||||
}
|
||||
usort($operadores, function($a, $b) {
|
||||
return strcmp($a->abreviacion, $b->abreviacion);
|
||||
});
|
||||
array_walk($operadores, function(&$item) {
|
||||
$item = $item->as_array();
|
||||
});
|
||||
$output = compact('operadores');
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function show(Request $request, Response $response, $id_operador): Response {
|
||||
$operador = Model::factory(Operador::class)->find_one($id_operador);
|
||||
$output = ['operador' => $operador->as_array()];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function add(Request $request, Response $response): Response {
|
||||
$post = $request->getParsedBody();
|
||||
$output = Operador::add($post);
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function ventas(Request $request, Response $response, $id_operador): Response {
|
||||
$operador = Model::factory(Operador::class)->find_one($id_operador);
|
||||
$output = [
|
||||
'operador' => $operador->as_array(),
|
||||
'ventas' => array_map(function($item) {
|
||||
return $item->as_array();
|
||||
}, $operador->ventas())
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
38
api/common/Controller/Proyectos.php
Normal file
38
api/common/Controller/Proyectos.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Controller;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use \Model;
|
||||
use Incoviba\Common\Define\Controller\Json;
|
||||
use Incoviba\Proyecto;
|
||||
|
||||
class Proyectos {
|
||||
use Json;
|
||||
|
||||
public function __invoke(Request $request, Response $response): Response {
|
||||
$proyectos = Model::factory(Proyecto::class)->order_by_asc('descripcion')->find_many();
|
||||
array_walk($proyectos, function(&$item) {
|
||||
$item = $item->as_array();
|
||||
});
|
||||
$output = compact('proyectos');
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function show(Request $request, Response $response, $id_proyecto): Response {
|
||||
$proyecto = Model::factory(Proyecto::class)->find_one($id_proyecto);
|
||||
$output = ['proyecto' => $proyecto->as_array()];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function ventas(Request $request, Response $response, $id_proyecto): Response {
|
||||
$proyecto = Model::factory(Proyecto::class)->find_one($id_proyecto);
|
||||
$output = [
|
||||
'proyecto' => $proyecto->as_array(),
|
||||
'ventas' => array_map(function($item) {
|
||||
if ($item) {
|
||||
return $item->as_array();
|
||||
}
|
||||
}, $proyecto->ventas())
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
31
api/common/Controller/Ventas.php
Normal file
31
api/common/Controller/Ventas.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Controller;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use \Model;
|
||||
use Incoviba\Common\Define\Controller\Json;
|
||||
use Incoviba\Venta;
|
||||
|
||||
class Ventas {
|
||||
use Json;
|
||||
|
||||
public function __invoke(Request $request, Response $response): Response {
|
||||
$ventas = Model::factory(Venta::class)->find_array();
|
||||
$output = compact('ventas');
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function show(Request $request, Response $response, $id_venta): Response {
|
||||
$venta = Model::factory(Venta::class)->find_one($id_venta);
|
||||
$output = ['venta' => $venta->as_array()];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function operador(Request $request, Response $response, $id_venta): Response {
|
||||
$venta = Model::factory(Venta::class)->find_one($id_venta);
|
||||
$output = [
|
||||
'venta' => $venta->as_array(),
|
||||
'operador' => $venta->operador() ? $venta->operador()->as_array() : null
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
13
api/common/Define/Controller/Json.php
Normal file
13
api/common/Define/Controller/Json.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Define\Controller;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
|
||||
trait Json {
|
||||
public function withJson(Response $response, $data, int $status = 200): Response {
|
||||
$response->getBody()->write(json_encode($data));
|
||||
return $response
|
||||
->withHeader('Content-Type', 'application/json')
|
||||
->withStatus($status);
|
||||
}
|
||||
}
|
24
api/common/Middleware/Cors.php
Normal file
24
api/common/Middleware/Cors.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Middleware;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Slim\Routing\RouteContext;
|
||||
|
||||
class Cors {
|
||||
public function __invoke(Request $request, Handler $handler): Response {
|
||||
$routeContext = RouteContext::fromRequest($request);
|
||||
$routingResults = $routeContext->getRoutingResults();
|
||||
$methods = $routingResults->getAllowedMethods();
|
||||
$requestHeaders = $request->getHeaderLine('Access-Control-Request-Headers');
|
||||
|
||||
$response = $handler->handle($request);
|
||||
|
||||
$response = $response->withHeader('Access-Control-Allow-Origin', '*');
|
||||
$response = $response->withHeader('Access-Control-Allow-Methods', implode(',', $methods));
|
||||
$response = $response->withHeader('Access-Control-Allow-Headers', $requestHeaders);
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user