Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
21 / 21 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| LoanController | |
100.00% |
21 / 21 |
|
100.00% |
6 / 6 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| show | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| index | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| store | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| devolution | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| findOrFail | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers; |
| 4 | |
| 5 | use Illuminate\Http\Request; |
| 6 | |
| 7 | use App\Http\Resources\LoanResource; |
| 8 | |
| 9 | use Core\Services\{ |
| 10 | Emprestimo\EmprestimoService, |
| 11 | Emprestimo\DevolucaoService, |
| 12 | }; |
| 13 | |
| 14 | use Core\Repositories\{ |
| 15 | IEmprestimosRepository, |
| 16 | }; |
| 17 | |
| 18 | class LoanController extends Controller |
| 19 | { |
| 20 | public function __construct( |
| 21 | private EmprestimoService $emprestimoService, |
| 22 | private DevolucaoService $devolucaoService, |
| 23 | private IEmprestimosRepository $emprestimosRepository, |
| 24 | ) {} |
| 25 | |
| 26 | /** |
| 27 | * @OA\Get( |
| 28 | * tags={"empréstimo"}, |
| 29 | * path="/api/loans/{id}", |
| 30 | * description="Exibição de 1 empréstimo", |
| 31 | * security={{"JWT":{}}}, |
| 32 | * @OA\Parameter( |
| 33 | * name="id", |
| 34 | * in="path", |
| 35 | * description="Id empréstimo", |
| 36 | * required=true, |
| 37 | * @OA\Schema(type="integer", example=1), |
| 38 | * ), |
| 39 | * @OA\Response(response="2XX", description="OK"), |
| 40 | * ) |
| 41 | */ |
| 42 | public function show(int $id, Request $r) |
| 43 | { |
| 44 | $e = $this->findOrFail($id); |
| 45 | |
| 46 | return new LoanResource($e); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @OA\Get( |
| 51 | * tags={"empréstimo"}, |
| 52 | * path="/api/loans", |
| 53 | * description="Listagem de empréstimos", |
| 54 | * security={{"JWT":{}}}, |
| 55 | * @OA\Parameter( |
| 56 | * name="bookId", |
| 57 | * in="query", |
| 58 | * description="Livro", |
| 59 | * @OA\Schema(type="integer", example=1), |
| 60 | * ), |
| 61 | * @OA\Parameter( |
| 62 | * name="memberId", |
| 63 | * in="query", |
| 64 | * description="Usuário do membro", |
| 65 | * @OA\Schema(type="integer", example=1), |
| 66 | * ), |
| 67 | * @OA\Parameter( |
| 68 | * name="collaboratorId", |
| 69 | * in="query", |
| 70 | * description="Usuário do colaborador", |
| 71 | * @OA\Schema(type="integer", example=2), |
| 72 | * ), |
| 73 | * @OA\Response(response="2XX", description="OK"), |
| 74 | * ) |
| 75 | */ |
| 76 | public function index(Request $r) |
| 77 | { |
| 78 | $page = $r->page ?? 1; |
| 79 | |
| 80 | $condition = []; |
| 81 | |
| 82 | $condition = $this->getArrayOfRequestedValues($r, [ |
| 83 | 'bookId' => 'livro', |
| 84 | 'memberId' => 'usuarioMembro', |
| 85 | 'collaboratorId' => 'usuarioColaborador' |
| 86 | ]); |
| 87 | |
| 88 | $e = $this->emprestimosRepository->findBy($condition, $page); |
| 89 | |
| 90 | return LoanResource::collection($e); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @OA\Post( |
| 95 | * tags={"empréstimo"}, |
| 96 | * path="/api/loans", |
| 97 | * description="Cadastro de empréstimo", |
| 98 | * security={{"JWT":{}}}, |
| 99 | * @OA\RequestBody( |
| 100 | * @OA\MediaType(mediaType="application/json;charset=UTF-8", |
| 101 | * @OA\Schema( |
| 102 | * required={"bookId", "memberId", "collaboratorId"}, |
| 103 | * @OA\Property( |
| 104 | * property="bookId", |
| 105 | * type="integer", |
| 106 | * example=1, |
| 107 | * ), |
| 108 | * @OA\Property( |
| 109 | * property="memberId", |
| 110 | * type="integer", |
| 111 | * example=1, |
| 112 | * ), |
| 113 | * @OA\Property( |
| 114 | * property="collaboratorId", |
| 115 | * type="integer", |
| 116 | * example=2, |
| 117 | * ), |
| 118 | * ), |
| 119 | * ), |
| 120 | * ), |
| 121 | * @OA\Response(response="2XX", description="OK"), |
| 122 | * ) |
| 123 | */ |
| 124 | public function store(Request $r) |
| 125 | { |
| 126 | $this->validate($r, [ |
| 127 | 'bookId' => 'required', |
| 128 | 'memberId' => 'required', |
| 129 | 'collaboratorId' => 'required', |
| 130 | ]); |
| 131 | |
| 132 | $e = $this->emprestimoService->execute( |
| 133 | idLivro: $r->bookId, |
| 134 | idMembro: $r->memberId, |
| 135 | idColaborador: $r->collaboratorId, |
| 136 | ); |
| 137 | |
| 138 | return new LoanResource($e); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @OA\Patch( |
| 143 | * tags={"empréstimo"}, |
| 144 | * path="/api/loans/{id}/devolution", |
| 145 | * description="Devolução de empréstimo", |
| 146 | * security={{"JWT":{}}}, |
| 147 | * @OA\Parameter( |
| 148 | * name="id", |
| 149 | * in="path", |
| 150 | * description="Id empréstimo", |
| 151 | * required=true, |
| 152 | * @OA\Schema(type="integer", example=1) |
| 153 | * ), |
| 154 | * @OA\Response(response="2XX", description="OK"), |
| 155 | * ) |
| 156 | */ |
| 157 | public function devolution(int $id) |
| 158 | { |
| 159 | $this->findOrFail($id); |
| 160 | |
| 161 | $e = $this->devolucaoService->execute( |
| 162 | idEmprestimo: $id, |
| 163 | ); |
| 164 | |
| 165 | return new LoanResource($e); |
| 166 | } |
| 167 | |
| 168 | private function findOrFail(int $id) |
| 169 | { |
| 170 | $e = $this->emprestimosRepository->findById($id); |
| 171 | |
| 172 | if(is_null($e)) { |
| 173 | abort(404); |
| 174 | } |
| 175 | |
| 176 | return $e; |
| 177 | } |
| 178 | } |
| 179 |