Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
34 / 34 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
| BookController | |
100.00% |
34 / 34 |
|
100.00% |
8 / 8 |
10 | |
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% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| store | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| update | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| destroy | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| activate | |
100.00% |
4 / 4 |
|
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 Core\Services\{ |
| 8 | Emprestimo\CadastroLivroService, |
| 9 | }; |
| 10 | |
| 11 | use Core\Repositories\{ |
| 12 | ILivrosRepository, |
| 13 | }; |
| 14 | |
| 15 | use App\Http\Resources\BookResource; |
| 16 | |
| 17 | class BookController extends Controller |
| 18 | { |
| 19 | public function __construct( |
| 20 | private CadastroLivroService $cadastroService, |
| 21 | private ILivrosRepository $livrosRepository, |
| 22 | ) {} |
| 23 | |
| 24 | /** |
| 25 | * @OA\Get( |
| 26 | * tags={"livro"}, |
| 27 | * path="/api/books/{id}", |
| 28 | * description="Exibição de 1 livro", |
| 29 | * security={{"JWT":{}}}, |
| 30 | * @OA\Parameter( |
| 31 | * name="id", |
| 32 | * in="path", |
| 33 | * description="Id livro", |
| 34 | * required=true, |
| 35 | * @OA\Schema(type="integer", example=1), |
| 36 | * ), |
| 37 | * @OA\Response(response="2XX", description="OK"), |
| 38 | * ) |
| 39 | */ |
| 40 | public function show(int $id, Request $r) |
| 41 | { |
| 42 | $l = $this->findOrFail($id); |
| 43 | |
| 44 | return new BookResource($l); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @OA\Get( |
| 49 | * tags={"livro"}, |
| 50 | * path="/api/books", |
| 51 | * description="Listagem de livros", |
| 52 | * security={{"JWT":{}}}, |
| 53 | * @OA\Parameter( |
| 54 | * name="title", |
| 55 | * in="query", |
| 56 | * description="Título parcial do livro", |
| 57 | * @OA\Schema(type="string", example="Allan"), |
| 58 | * ), |
| 59 | * @OA\Parameter( |
| 60 | * name="page", |
| 61 | * in="query", |
| 62 | * description="Paginação", |
| 63 | * @OA\Schema(type="integer", example=1), |
| 64 | * ), |
| 65 | * @OA\Response(response="2XX", description="OK"), |
| 66 | * ) |
| 67 | */ |
| 68 | public function index(Request $r) |
| 69 | { |
| 70 | $page = $r->page ?? 1; |
| 71 | |
| 72 | $condition = []; |
| 73 | |
| 74 | if(!is_null($r->title)) { |
| 75 | $condition['titulo'] = $r->title; |
| 76 | } |
| 77 | |
| 78 | $l = $this->livrosRepository->findBy($condition, $page); |
| 79 | |
| 80 | return BookResource::collection($l); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @OA\Post( |
| 85 | * tags={"livro"}, |
| 86 | * path="/api/books", |
| 87 | * description="Cadastro de livro", |
| 88 | * security={{"JWT":{}}}, |
| 89 | * @OA\RequestBody( |
| 90 | * @OA\MediaType(mediaType="application/json;charset=UTF-8", |
| 91 | * @OA\Schema( |
| 92 | * required={"title", "author_id", "amount"}, |
| 93 | * @OA\Property( |
| 94 | * property="title", |
| 95 | * type="string", |
| 96 | * example="Coração delator", |
| 97 | * ), |
| 98 | * @OA\Property( |
| 99 | * property="author_id", |
| 100 | * type="integer", |
| 101 | * example=1, |
| 102 | * ), |
| 103 | * @OA\Property( |
| 104 | * property="amount", |
| 105 | * type="integer", |
| 106 | * example=3, |
| 107 | * ), |
| 108 | * ), |
| 109 | * ), |
| 110 | * ), |
| 111 | * @OA\Response(response="2XX", description="OK"), |
| 112 | * ) |
| 113 | */ |
| 114 | public function store(Request $r) |
| 115 | { |
| 116 | $this->validate($r, [ |
| 117 | 'title' => 'required', |
| 118 | 'author_id' => 'required|integer', |
| 119 | 'amount' => 'required|integer', |
| 120 | ]); |
| 121 | |
| 122 | $l = $this->cadastroService->execute( |
| 123 | titulo: $r->title, |
| 124 | idAutor: $r->author_id, |
| 125 | quantidade: $r->amount, |
| 126 | ); |
| 127 | |
| 128 | return new BookResource($l); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @OA\Put( |
| 133 | * tags={"livro"}, |
| 134 | * path="/api/books/{id}", |
| 135 | * description="Edição de livro", |
| 136 | * security={{"JWT":{}}}, |
| 137 | * @OA\Parameter( |
| 138 | * name="id", |
| 139 | * in="path", |
| 140 | * description="Id livro", |
| 141 | * required=true, |
| 142 | * @OA\Schema(type="integer", example=1) |
| 143 | * ), |
| 144 | * @OA\RequestBody( |
| 145 | * @OA\MediaType(mediaType="application/json;charset=UTF-8", |
| 146 | * @OA\Schema( |
| 147 | * required={"title", "author_id", "amount"}, |
| 148 | * @OA\Property( |
| 149 | * property="title", |
| 150 | * type="string", |
| 151 | * example="Coração delator", |
| 152 | * ), |
| 153 | * @OA\Property( |
| 154 | * property="author_id", |
| 155 | * type="integer", |
| 156 | * example=1, |
| 157 | * ), |
| 158 | * @OA\Property( |
| 159 | * property="amount", |
| 160 | * type="integer", |
| 161 | * example=3, |
| 162 | * ), |
| 163 | * ), |
| 164 | * ), |
| 165 | * ), |
| 166 | * @OA\Response(response="2XX", description="OK"), |
| 167 | * ) |
| 168 | */ |
| 169 | public function update(int $id, Request $r) |
| 170 | { |
| 171 | $this->findOrFail($id); |
| 172 | |
| 173 | $this->validate($r, [ |
| 174 | 'title' => 'required', |
| 175 | 'author_id' => 'required|integer', |
| 176 | 'amount' => 'required|integer', |
| 177 | ]); |
| 178 | |
| 179 | $l = $this->cadastroService->execute( |
| 180 | id: $id, |
| 181 | titulo: $r->title, |
| 182 | idAutor: $r->author_id, |
| 183 | quantidade: $r->amount, |
| 184 | ); |
| 185 | |
| 186 | return new BookResource($l); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @OA\Delete( |
| 191 | * tags={"livro"}, |
| 192 | * path="/api/books/{id}", |
| 193 | * description="Deleção de livro", |
| 194 | * security={{"JWT":{}}}, |
| 195 | * @OA\Parameter( |
| 196 | * name="id", |
| 197 | * in="path", |
| 198 | * description="Id livro", |
| 199 | * required=true, |
| 200 | * @OA\Schema(type="integer", example=1), |
| 201 | * ), |
| 202 | * @OA\Response(response="2XX", description="OK"), |
| 203 | * ) |
| 204 | */ |
| 205 | public function destroy(int $id) |
| 206 | { |
| 207 | $l = $this->findOrFail($id); |
| 208 | $l->inativar(); |
| 209 | $this->livrosRepository->save($l); |
| 210 | |
| 211 | return response()->json('', 204); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * @OA\Post( |
| 216 | * tags={"livro"}, |
| 217 | * path="/api/books/{id}/activate", |
| 218 | * description="Reativação de livro", |
| 219 | * security={{"JWT":{}}}, |
| 220 | * @OA\Parameter( |
| 221 | * name="id", |
| 222 | * in="path", |
| 223 | * description="Id livro", |
| 224 | * required=true, |
| 225 | * @OA\Schema(type="integer", example=1), |
| 226 | * ), |
| 227 | * @OA\Response(response="2XX", description="OK"), |
| 228 | * ) |
| 229 | */ |
| 230 | public function activate(int $id) |
| 231 | { |
| 232 | $l = $this->findOrFail($id); |
| 233 | $l->ativar(); |
| 234 | $this->livrosRepository->save($l); |
| 235 | |
| 236 | return response()->json('', 204); |
| 237 | } |
| 238 | |
| 239 | private function findOrFail(int $id) |
| 240 | { |
| 241 | $l = $this->livrosRepository->findById($id); |
| 242 | |
| 243 | if(is_null($l)) { |
| 244 | abort(404); |
| 245 | } |
| 246 | |
| 247 | return $l; |
| 248 | } |
| 249 | } |
| 250 |