93 lines
3.1 KiB
PHP
93 lines
3.1 KiB
PHP
<?php
|
|
namespace Incoviba\API\Common\Controller;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
use Incoviba\API\Common\Define\Controller\Json;
|
|
use Incoviba\API\Common\Factory\Model as Factory;
|
|
use Incoviba\Venta\Venta;
|
|
|
|
class Ventas {
|
|
use Json;
|
|
|
|
public function __invoke(Request $request, Response $response, Factory $factory): Response {
|
|
$ventas = $factory->find(Venta::class)->array();
|
|
$url = '' . $request->getUri();
|
|
array_walk($ventas, function (&$item) use ($url) {
|
|
$item['link'] = [
|
|
'rel' => 'venta',
|
|
'title' => 'Venta',
|
|
'href' => str_replace('/ventas', "/venta/{$item['id']}", $url)
|
|
];
|
|
});
|
|
return $this->withJson($response, compact('ventas'));
|
|
}
|
|
public function show(Request $request, Response $response, Factory $factory, $venta_id): Response {
|
|
$venta = $factory->find(Venta::class)->one($venta_id);
|
|
$output = [
|
|
'input' => $venta_id,
|
|
'venta' => $venta->toArray(),
|
|
'link' => [
|
|
'rel' => 'ventas',
|
|
'title' => 'Ventas',
|
|
'href' => str_replace("/venta/{$venta_id}", '/ventas', $request->getUri())
|
|
]
|
|
];
|
|
$output['links'] = [
|
|
[
|
|
'rel' => 'propietario',
|
|
'title' => $venta->propietario()->nombreCompleto(),
|
|
'href' => str_replace("/venta/{$venta_id}", "/propietario/{$venta->propietario}", $request->getUri())
|
|
],
|
|
[
|
|
'rel' => 'propiedad',
|
|
'title' => 'Propiedad',
|
|
'href' => str_replace("/venta/{$venta_id}", "/propiedad/{$venta->propiedad}", $request->getUri())
|
|
]
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function add(Request $request, Response $response, Factory $factory): Response {
|
|
$post = $request->getParsedBody();
|
|
$output = [
|
|
'input' => $post
|
|
];
|
|
if (in_array('ventas', $post)) {
|
|
$output['ventas'] = [];
|
|
foreach ($post['ventas'] as $input) {
|
|
$venta = Venta::add($factory, $input);
|
|
$venta []= [
|
|
'venta' => $venta->toArray(),
|
|
'created' => $venta->is_new() ? $venta->save() : false
|
|
];
|
|
}
|
|
} elseif (in_array('venta', $post)) {
|
|
$venta = Venta::add($factory, $post);
|
|
$output['venta'] = $venta;
|
|
$output['created'] = $venta->is_new() ? $venta->save() : false;
|
|
}
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function edit(Request $request, Response $response, Factory $factory, $venta_id): Response {
|
|
$post = $request->getParsedBody();
|
|
$input = compact('venta_id', 'post');
|
|
$venta = $factory->find(Venta::class)->one($venta_id);
|
|
$output = [
|
|
'input' => $input,
|
|
'venta' => $venta->toArray()
|
|
];
|
|
$output['edited'] = $venta->edit($post);
|
|
$output['changes'] = $venta->toArray();
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function delete(Request $request, Response $response, Factory $factory, $venta_id): Response {
|
|
$venta = $factory->find(Venta::class)->one($venta_id);
|
|
$output = [
|
|
'input' => $venta_id,
|
|
'venta' => $venta->toArray()
|
|
];
|
|
$output['deleted'] = $venta->delete();
|
|
return $this->withJson($response, $output);
|
|
}
|
|
}
|