From 0d7c513bb8382e1246da9d8794e91cf57151fd46 Mon Sep 17 00:00:00 2001 From: Ramon Mendes Date: Thu, 16 Nov 2023 14:42:05 -0300 Subject: [PATCH 1/2] feature backend: adicionando parameters para rota --- backend-api/routes/api.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backend-api/routes/api.php b/backend-api/routes/api.php index 499854c..461d0cc 100644 --- a/backend-api/routes/api.php +++ b/backend-api/routes/api.php @@ -27,7 +27,9 @@ Route::post('/logout', 'App\Http\Controllers\Auth\LoginController@logout'); Route::group(['prefix' => '/tasks'], function () { - Route::apiResource('', TaskController::class); + Route::apiResource('', TaskController::class)->parameters([ + '' => 'task', + ]); Route::put('/{task}/complete', 'App\Http\Controllers\TaskController@markAsCompleted'); Route::put('/{task}/incompleted', 'App\Http\Controllers\TaskController@markAsIncompleted'); Route::post('/export-excel', 'App\Http\Controllers\TaskController@exportExcel')->name('tasks.export-excel'); From 58260e30ba5a165b8b38fd20dbcc0437ec634441 Mon Sep 17 00:00:00 2001 From: Ramon Mendes Date: Thu, 16 Nov 2023 15:11:19 -0300 Subject: [PATCH 2/2] feature backend: adicionando testes para delete de multiplas tarefas --- .../app/Http/Controllers/TaskController.php | 10 ++--- backend-api/routes/api.php | 2 +- .../tests/Feature/TaskControllerTest.php | 39 +++++++++++++++++++ 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/backend-api/app/Http/Controllers/TaskController.php b/backend-api/app/Http/Controllers/TaskController.php index c2ea0cd..0dd625a 100644 --- a/backend-api/app/Http/Controllers/TaskController.php +++ b/backend-api/app/Http/Controllers/TaskController.php @@ -114,19 +114,17 @@ public function exportExcel(): BinaryFileResponse * @param Request $request * @return JsonResponse */ - public function deleteMultiple(Request $request): JsonResponse + public function deleteMultipleTask(Request $request): JsonResponse { $tasks = $request->all(); - if (!is_array($tasks) || empty($tasks)) { + if (!$tasks) { return response()->json(['message' => 'Nenhuma tarefa foi informada'], 400); } - + $taskIds = array_column($tasks, 'id'); - foreach ($tasks as $task) { - Task::whereIn('id', $taskIds)->delete(); - } + Task::whereIn('id', $taskIds)->delete(); return response()->json(['message' => 'Tarefas excluídas com sucesso'], 200); } diff --git a/backend-api/routes/api.php b/backend-api/routes/api.php index 461d0cc..d67e65e 100644 --- a/backend-api/routes/api.php +++ b/backend-api/routes/api.php @@ -35,6 +35,6 @@ Route::post('/export-excel', 'App\Http\Controllers\TaskController@exportExcel')->name('tasks.export-excel'); Route::post('/mark-all-as-completed', 'App\Http\Controllers\TaskController@markAllAsCompleted'); Route::post('/mark-all-as-incompleted', 'App\Http\Controllers\TaskController@markAllAsIncompleted'); - Route::post('/delete-multiple', 'App\Http\Controllers\TaskController@deleteMultiple'); + Route::post('/delete-multiple', 'App\Http\Controllers\TaskController@deleteMultipleTask'); }); }); diff --git a/backend-api/tests/Feature/TaskControllerTest.php b/backend-api/tests/Feature/TaskControllerTest.php index 7c8a584..9f8dcfc 100644 --- a/backend-api/tests/Feature/TaskControllerTest.php +++ b/backend-api/tests/Feature/TaskControllerTest.php @@ -213,5 +213,44 @@ public function testExportExcel() $response->assertDownload(); } + /** + * Testa a exclusão de várias tarefas. + * + * @return void + * @covers TaskController@deleteMultipleTask + */ + public function testDeleteMultipleTask() + { + $user = User::factory()->create(); + $this->actingAs($user, 'api'); + + + $tasks = Task::factory(20)->create(); + + $taskIds = $tasks->pluck('id')->toArray(); + + $response = $this->post("/api/tasks/delete-multiple", ['tasks' => $taskIds]); + + foreach ($taskIds as $taskId) { + $this->assertDatabaseMissing('tasks', [['id' => $taskId]]); + } + + $response->assertStatus(200); + } + + /** + * Testa a exclusão de várias tarefas sem informar tarefas. + * + * @return void + * @covers TaskController@deleteMultipleTask + */ + public function testDeleteMultipleTaskWithoutTasks() + { + $user = User::factory()->create(); + $this->actingAs($user, 'api'); + + $response = $this->post("/api/tasks/delete-multiple", []); + $response->assertStatus(400)->assertJson(['message' => 'Nenhuma tarefa foi informada']); + } }