Skip to content

Commit

Permalink
Merge pull request #20 from RamonSouzaDev/backend
Browse files Browse the repository at this point in the history
feature backend: adicionando parameters para rota
  • Loading branch information
RamonSouzaDev authored Nov 16, 2023
2 parents 1cb0438 + 58260e3 commit 556bb68
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
10 changes: 4 additions & 6 deletions backend-api/app/Http/Controllers/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 4 additions & 2 deletions backend-api/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
39 changes: 39 additions & 0 deletions backend-api/tests/Feature/TaskControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

0 comments on commit 556bb68

Please sign in to comment.