Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
AuthorController
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
7 / 7
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 show
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 index
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 store
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 update
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 destroy
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 findOrFail
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace App\Http\Controllers;
4
5use Illuminate\Http\Request;
6
7use App\Http\Resources\AuthorResource;
8
9use Core\Services\{
10    Emprestimo\CadastroAutorService,
11};
12
13use Core\Repositories\{
14    IAutoresRepository,
15};
16
17class AuthorController extends Controller
18{
19    public function __construct(
20        private CadastroAutorService $cadastroService,
21        private IAutoresRepository $autoresRepository,
22    ) {}
23
24    /**
25     * @OA\Get(
26     *     tags={"autor"},
27     *     path="/api/authors/{id}",
28     *     description="Exibição de 1 autor",
29     *     security={{"JWT":{}}},
30     *     @OA\Parameter(
31     *         name="id",
32     *         in="path",
33     *         description="Id autor",
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        $a = $this->findOrFail($id);
43
44        return new AuthorResource($a);
45    }
46
47    /**
48     * @OA\Get(
49     *     tags={"autor"},
50     *     path="/api/authors",
51     *     description="Listagem de autores",
52     *     security={{"JWT":{}}},
53     *     @OA\Parameter(
54     *         name="name",
55     *         in="query",
56     *         description="Nome parcial do autor",
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->name)) {
75            $condition['nome'] = $r->name;
76        }
77
78        $a = $this->autoresRepository->findBy($condition, $page);
79
80        return AuthorResource::collection($a);
81    }
82
83    /**
84     * @OA\Post(
85     *     tags={"autor"},
86     *     path="/api/authors",
87     *     description="Cadastro de autor",
88     *     security={{"JWT":{}}},
89     *     @OA\RequestBody(
90     *         @OA\MediaType(mediaType="application/json;charset=UTF-8",
91     *             @OA\Schema(
92     *                 required={"name"},
93     *                 @OA\Property(
94     *                      property="name",
95     *                      type="string",
96     *                      example="Edgar Allan Poe",
97     *                 ),
98     *             ),
99     *         ),
100     *     ),
101     *     @OA\Response(response="2XX", description="OK"),
102     * )
103     */
104    public function store(Request $r)
105    {
106        $this->validate($r, [
107            'name' => 'required',
108        ]);
109
110        $a = $this->cadastroService->execute(
111            nome: $r->name,
112        );
113
114        return new AuthorResource($a);
115    }
116
117    /**
118     * @OA\Put(
119     *     tags={"autor"},
120     *     path="/api/authors/{id}",
121     *     description="Edição de autor",
122     *     security={{"JWT":{}}},
123     *     @OA\Parameter(
124     *         name="id",
125     *         in="path",
126     *         description="Id autor",
127     *         required=true,
128     *         @OA\Schema(type="integer", example=1)
129     *     ),
130     *     @OA\RequestBody(
131     *         @OA\MediaType(mediaType="application/json;charset=UTF-8",
132     *             @OA\Schema(
133     *                 required={"name"},
134     *                 @OA\Property(
135     *                      property="name",
136     *                      type="string",
137     *                      example="Edgar Allan Poe",
138     *                 ),
139     *             ),
140     *         ),
141     *     ),
142     *     @OA\Response(response="2XX", description="OK"),
143     * )
144     */
145    public function update(int $id, Request $r)
146    {
147        $this->findOrFail($id);
148
149        $this->validate($r, [
150            'name' => 'required',
151        ]);
152
153        $a = $this->cadastroService->execute(
154            id:     $id,
155            nome:   $r->name,
156        );
157
158        return new AuthorResource($a);
159    }
160
161    /**
162     * @OA\Delete(
163     *     tags={"autor"},
164     *     path="/api/authors/{id}",
165     *     description="Deleção de autor",
166     *     security={{"JWT":{}}},
167     *     @OA\Parameter(
168     *         name="id",
169     *         in="path",
170     *         description="Id autor",
171     *         required=true,
172     *         @OA\Schema(type="integer", example=1),
173     *     ),
174     *     @OA\Response(response="2XX", description="OK"),
175     * )
176     */
177    public function destroy(int $id)
178    {
179        $this->findOrFail($id);
180
181        $this->autoresRepository->delete($id);
182
183        return response()->json('', 204);
184    }
185
186    private function findOrFail(int $id)
187    {
188        $a = $this->autoresRepository->findById($id);
189
190        if(is_null($a)) {
191            abort(404);
192        }
193
194        return $a;
195    }
196}