Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.88% |
46 / 49 |
|
75.00% |
6 / 8 |
CRAP | |
0.00% |
0 / 1 |
| UserController | |
93.88% |
46 / 49 |
|
75.00% |
6 / 8 |
16.06 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| show | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| index | |
83.33% |
10 / 12 |
|
0.00% |
0 / 1 |
5.12 | |||
| store | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| update | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
2.00 | |||
| 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 Illuminate\Support\Facades\Gate; |
| 8 | use Core\Models\Papel; |
| 9 | |
| 10 | use App\Http\Resources\UserResource; |
| 11 | |
| 12 | use Core\Services\{ |
| 13 | Usuario\CadastroUsuarioService, |
| 14 | Usuario\EdicaoUsuarioService, |
| 15 | }; |
| 16 | |
| 17 | use Core\Repositories\{ |
| 18 | IUsuariosRepository, |
| 19 | }; |
| 20 | |
| 21 | class UserController extends Controller |
| 22 | { |
| 23 | public function __construct( |
| 24 | private CadastroUsuarioService $cadastroService, |
| 25 | private EdicaoUsuarioService $edicaoService, |
| 26 | private IUsuariosRepository $usuariosRepository, |
| 27 | ) {} |
| 28 | |
| 29 | /** |
| 30 | * @OA\Get( |
| 31 | * tags={"usuário"}, |
| 32 | * path="/api/user/{id}", |
| 33 | * description="Exibição de 1 usuário", |
| 34 | * security={{"JWT":{}}}, |
| 35 | * @OA\Parameter( |
| 36 | * name="id", |
| 37 | * in="path", |
| 38 | * description="Id usuário", |
| 39 | * required=true, |
| 40 | * @OA\Schema(type="integer", example=1), |
| 41 | * ), |
| 42 | * @OA\Response(response="2XX", description="OK"), |
| 43 | * ) |
| 44 | */ |
| 45 | public function show(int $id, Request $r) |
| 46 | { |
| 47 | $u = $this->usuariosRepository->findById($id); |
| 48 | |
| 49 | if(is_null($u)) { |
| 50 | abort(404); |
| 51 | } |
| 52 | |
| 53 | return new UserResource($u); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @OA\Get( |
| 58 | * tags={"usuário"}, |
| 59 | * path="/api/users", |
| 60 | * description="Listagem de usuários", |
| 61 | * security={{"JWT":{}}}, |
| 62 | * @OA\Parameter( |
| 63 | * name="name", |
| 64 | * in="query", |
| 65 | * description="Nome parcial do usuário", |
| 66 | * @OA\Schema(type="string", example="Allan"), |
| 67 | * ), |
| 68 | * @OA\Parameter( |
| 69 | * name="email", |
| 70 | * in="query", |
| 71 | * description="Email parcial do usuário", |
| 72 | * @OA\Schema(type="string", example="example@foo.com"), |
| 73 | * ), |
| 74 | * @OA\Parameter( |
| 75 | * name="cpf", |
| 76 | * in="query", |
| 77 | * description="CPF do usuário", |
| 78 | * @OA\Schema(type="string", example="37128197060"), |
| 79 | * ), |
| 80 | * @OA\Parameter( |
| 81 | * name="role", |
| 82 | * in="query", |
| 83 | * description="Papel do usuário", |
| 84 | * @OA\Schema( |
| 85 | * type="string", |
| 86 | * enum={"membro", "colaborador", "admin"} |
| 87 | * ), |
| 88 | * ), |
| 89 | * @OA\Parameter( |
| 90 | * name="page", |
| 91 | * in="query", |
| 92 | * description="Paginação", |
| 93 | * @OA\Schema(type="integer", example=1), |
| 94 | * ), |
| 95 | * @OA\Response(response="2XX", description="OK"), |
| 96 | * ) |
| 97 | */ |
| 98 | public function index(Request $r) |
| 99 | { |
| 100 | $page = $r->page ?? 1; |
| 101 | |
| 102 | $condition = []; |
| 103 | |
| 104 | if(!is_null($r->name)) { |
| 105 | $condition['nome'] = $r->name; |
| 106 | } |
| 107 | |
| 108 | if(!is_null($r->cpf)) { |
| 109 | $condition['cpf'] = $r->cpf; |
| 110 | } |
| 111 | |
| 112 | if(!is_null($r->email)) { |
| 113 | $condition['email'] = $r->email; |
| 114 | } |
| 115 | |
| 116 | if(!is_null($r->role)) { |
| 117 | $condition['papel'] = $r->role; |
| 118 | } |
| 119 | |
| 120 | $u = $this->usuariosRepository->findBy($condition, $page); |
| 121 | |
| 122 | return UserResource::collection($u); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @OA\Post( |
| 127 | * tags={"usuário"}, |
| 128 | * path="/api/users", |
| 129 | * description="Cadastro de usuário", |
| 130 | * security={{"JWT":{}}}, |
| 131 | * @OA\RequestBody( |
| 132 | * @OA\MediaType(mediaType="application/json;charset=UTF-8", |
| 133 | * @OA\Schema( |
| 134 | * required={"name", "cpf", "password", "email"}, |
| 135 | * @OA\Property( |
| 136 | * property="name", |
| 137 | * type="string", |
| 138 | * example="Diego", |
| 139 | * ), |
| 140 | * @OA\Property( |
| 141 | * property="cpf", |
| 142 | * type="string", |
| 143 | * example="37128197060", |
| 144 | * ), |
| 145 | * @OA\Property( |
| 146 | * property="password", |
| 147 | * type="string", |
| 148 | * example="19800507", |
| 149 | * ), |
| 150 | * @OA\Property( |
| 151 | * property="email", |
| 152 | * type="string", |
| 153 | * example="example@foo.com", |
| 154 | * ), |
| 155 | * @OA\Property( |
| 156 | * property="role", |
| 157 | * type="string", |
| 158 | * example="membro", |
| 159 | * ), |
| 160 | * ), |
| 161 | * ), |
| 162 | * ), |
| 163 | * @OA\Response(response="2XX", description="OK"), |
| 164 | * ) |
| 165 | */ |
| 166 | public function store(Request $r) |
| 167 | { |
| 168 | $this->validate($r, [ |
| 169 | 'name' => 'required', |
| 170 | 'cpf' => 'required', |
| 171 | 'email' => 'required|email', |
| 172 | 'password' => 'required', |
| 173 | ]); |
| 174 | |
| 175 | $role = ''; |
| 176 | |
| 177 | if(Gate::check('papel', Papel::$ADMIN)) { |
| 178 | $role = $r->role ?? ''; |
| 179 | } |
| 180 | |
| 181 | $u = $this->cadastroService->execute( |
| 182 | nome: $r->name, |
| 183 | cpf: $r->cpf, |
| 184 | senha: $r->password, |
| 185 | email: $r->email, |
| 186 | papel: $role, |
| 187 | ); |
| 188 | |
| 189 | return new UserResource($u); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * @OA\Put( |
| 194 | * tags={"usuário"}, |
| 195 | * path="/api/users/{id}", |
| 196 | * description="Edição de usuário", |
| 197 | * security={{"JWT":{}}}, |
| 198 | * @OA\Parameter( |
| 199 | * name="id", |
| 200 | * in="path", |
| 201 | * description="Id usuário", |
| 202 | * required=true, |
| 203 | * @OA\Schema(type="integer", example=1) |
| 204 | * ), |
| 205 | * @OA\RequestBody( |
| 206 | * @OA\MediaType(mediaType="application/json;charset=UTF-8", |
| 207 | * @OA\Schema( |
| 208 | * required={"name", "cpf", "password", "email"}, |
| 209 | * @OA\Property( |
| 210 | * property="name", |
| 211 | * type="string", |
| 212 | * example="Diego", |
| 213 | * ), |
| 214 | * @OA\Property( |
| 215 | * property="cpf", |
| 216 | * type="string", |
| 217 | * example="37128197060", |
| 218 | * ), |
| 219 | * @OA\Property( |
| 220 | * property="password", |
| 221 | * type="string", |
| 222 | * example="19800507", |
| 223 | * ), |
| 224 | * @OA\Property( |
| 225 | * property="email", |
| 226 | * type="string", |
| 227 | * example="example@foo.com", |
| 228 | * ), |
| 229 | * @OA\Property( |
| 230 | * property="role", |
| 231 | * type="string", |
| 232 | * example="membro", |
| 233 | * ), |
| 234 | * ), |
| 235 | * ), |
| 236 | * ), |
| 237 | * @OA\Response(response="2XX", description="OK"), |
| 238 | * ) |
| 239 | */ |
| 240 | public function update(int $id, Request $r) |
| 241 | { |
| 242 | $u = $this->findOrFail($id); |
| 243 | |
| 244 | $this->validate($r, [ |
| 245 | 'name' => 'required', |
| 246 | 'cpf' => 'required', |
| 247 | 'email' => 'required|email', |
| 248 | ]); |
| 249 | |
| 250 | $role = ''; |
| 251 | |
| 252 | if(Gate::check('papel', Papel::$ADMIN)) { |
| 253 | $role = $r->role ?? ''; |
| 254 | } |
| 255 | |
| 256 | $u = $this->edicaoService->execute( |
| 257 | id: $id, |
| 258 | nome: $r->name, |
| 259 | cpf: $r->cpf, |
| 260 | email: $r->email, |
| 261 | papel: $role, |
| 262 | ); |
| 263 | |
| 264 | return new UserResource($u); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * @OA\Delete( |
| 269 | * tags={"usuário"}, |
| 270 | * path="/api/users/{id}", |
| 271 | * description="Deleção de usuário", |
| 272 | * security={{"JWT":{}}}, |
| 273 | * @OA\Parameter( |
| 274 | * name="id", |
| 275 | * in="path", |
| 276 | * description="Id autor", |
| 277 | * required=true, |
| 278 | * @OA\Schema(type="integer", example=1), |
| 279 | * ), |
| 280 | * @OA\Response(response="2XX", description="OK"), |
| 281 | * ) |
| 282 | */ |
| 283 | public function destroy(int $id) |
| 284 | { |
| 285 | $u = $this->findOrFail($id); |
| 286 | $u->inativar(); |
| 287 | $this->usuariosRepository->save($u); |
| 288 | |
| 289 | return response()->json('', 204); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * @OA\Post( |
| 294 | * tags={"usuário"}, |
| 295 | * path="/api/users/{id}/activate", |
| 296 | * description="Reativação de usuário", |
| 297 | * security={{"JWT":{}}}, |
| 298 | * @OA\Parameter( |
| 299 | * name="id", |
| 300 | * in="path", |
| 301 | * description="Id usuário", |
| 302 | * required=true, |
| 303 | * @OA\Schema(type="integer", example=1), |
| 304 | * ), |
| 305 | * @OA\Response(response="2XX", description="OK"), |
| 306 | * ) |
| 307 | */ |
| 308 | public function activate(int $id) |
| 309 | { |
| 310 | $u = $this->findOrFail($id); |
| 311 | $u->ativar(); |
| 312 | $this->usuariosRepository->save($u); |
| 313 | |
| 314 | return response()->json('', 204); |
| 315 | } |
| 316 | |
| 317 | private function findOrFail(int $id) |
| 318 | { |
| 319 | $u = $this->usuariosRepository->findById($id); |
| 320 | |
| 321 | if(is_null($u)) { |
| 322 | abort(404); |
| 323 | } |
| 324 | |
| 325 | return $u; |
| 326 | } |
| 327 | } |