API Contratos
This commit is contained in:
101
app/src/Controller/API/Proyectos/Brokers/Contracts.php
Normal file
101
app/src/Controller/API/Proyectos/Brokers/Contracts.php
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
<?php
|
||||||
|
namespace Incoviba\Controller\API\Proyectos\Brokers;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
use Incoviba\Controller\API\withJson;
|
||||||
|
use Incoviba\Service;
|
||||||
|
use Incoviba\Exception\ServiceAction;
|
||||||
|
|
||||||
|
class Contracts
|
||||||
|
{
|
||||||
|
use withJson;
|
||||||
|
|
||||||
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker\Contract $contractService): ResponseInterface
|
||||||
|
{
|
||||||
|
$contracts = $contractService->getAll();
|
||||||
|
|
||||||
|
return $this->withJson($response, compact('contracts'));
|
||||||
|
}
|
||||||
|
public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker\Contract $contractService, int $contract_id): ResponseInterface
|
||||||
|
{
|
||||||
|
$output = [
|
||||||
|
'contract_id' => $contract_id,
|
||||||
|
'contract' => null
|
||||||
|
];
|
||||||
|
try {
|
||||||
|
$output['contract'] = $contractService->getById($contract_id);
|
||||||
|
} catch (ServiceAction\Read $exception) {
|
||||||
|
return $this->withError($response, $exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
|
public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker\Contract $contractService): ResponseInterface
|
||||||
|
{
|
||||||
|
$input = $request->getParsedBody();
|
||||||
|
$output = [
|
||||||
|
'input' => $input,
|
||||||
|
'contracts' => [],
|
||||||
|
'success' => false,
|
||||||
|
'partial' => false,
|
||||||
|
'errors' => [],
|
||||||
|
];
|
||||||
|
foreach ($input['contracts'] as $jsonData) {
|
||||||
|
try {
|
||||||
|
$contractData = json_decode($jsonData, true);
|
||||||
|
if (is_array($jsonData)) {
|
||||||
|
$contractData = $jsonData;
|
||||||
|
}
|
||||||
|
$contract = $contractService->add($contractData);
|
||||||
|
$output['contracts'] []= [
|
||||||
|
'contract' => $contract,
|
||||||
|
'success' => true
|
||||||
|
];
|
||||||
|
$output['partial'] = true;
|
||||||
|
} catch (ServiceAction\Create $exception) {
|
||||||
|
$output['errors'] []= $this->parseError($exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (count($output['contracts']) == count($input['contracts'])) {
|
||||||
|
$output['success'] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
|
public function edit(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker\Contract $contractService, int $contract_id): ResponseInterface
|
||||||
|
{
|
||||||
|
$input = $request->getParsedBody();
|
||||||
|
$output = [
|
||||||
|
'contract_id' => $contract_id,
|
||||||
|
'input' => $input,
|
||||||
|
'contract' => null,
|
||||||
|
'success' => false
|
||||||
|
];
|
||||||
|
try {
|
||||||
|
$contract = $contractService->getById($contract_id);
|
||||||
|
$output['contract'] = $contractService->edit($contract, $input);
|
||||||
|
$output['success'] = true;
|
||||||
|
} catch (ServiceAction\Read | ServiceAction\Update $exception) {
|
||||||
|
return $this->withError($response, $exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
|
public function delete(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker\Contract $contractService, int $contract_id): ResponseInterface
|
||||||
|
{
|
||||||
|
$output = [
|
||||||
|
'contract_id' => $contract_id,
|
||||||
|
'contract' => null,
|
||||||
|
'success' => false
|
||||||
|
];
|
||||||
|
try {
|
||||||
|
$output['contract'] = $contractService->delete($contract_id);
|
||||||
|
$output['success'] = true;
|
||||||
|
} catch (ServiceAction\Delete $exception) {
|
||||||
|
return $this->withError($response, $exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user