From 8ea13c3efd73dddb27bcb065998a8bc6f193b6b9 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Sat, 1 Mar 2025 13:27:01 -0300 Subject: [PATCH] API Contratos --- .../API/Proyectos/Brokers/Contracts.php | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 app/src/Controller/API/Proyectos/Brokers/Contracts.php diff --git a/app/src/Controller/API/Proyectos/Brokers/Contracts.php b/app/src/Controller/API/Proyectos/Brokers/Contracts.php new file mode 100644 index 0000000..248fa11 --- /dev/null +++ b/app/src/Controller/API/Proyectos/Brokers/Contracts.php @@ -0,0 +1,101 @@ +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); + } +}