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 499854c..d67e65e 100644 --- a/backend-api/routes/api.php +++ b/backend-api/routes/api.php @@ -27,12 +27,14 @@ 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'); 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']); + } }