Skip to content

Commit 56413a2

Browse files
Refactor: refatorando controller parte 3. Adicionando services.
Regra: Controller pode no máximo chamar uma service.
1 parent 6257498 commit 56413a2

2 files changed

Lines changed: 29 additions & 19 deletions

File tree

Controllers/TaskManagerController.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,16 @@ public IActionResult ListAllTasks()
5656

5757
[HttpGet("ListTaskByUser/{userId}")]
5858
public IActionResult ListTarefaByUser(
59-
[FromRoute] int userId,
60-
[FromServices] AppDbContext context)
59+
[FromRoute] int userId)
6160
{
62-
var tasks = context.Tasks
63-
.Where(x => x.UserId == userId)
64-
.Select(task => new
65-
{
66-
TaskId = task.Id,
67-
TaskTitle = task.Title,
68-
TaskDescription = task.Description,
69-
Done = task.Done,
70-
CreatedAt = task.CreatedAt,
71-
CategoryName = context.CategorieTasks
72-
.Where(category => category.Id == task.CategorieTaskId)
73-
.Select(category => category.Name)
74-
.FirstOrDefault()
75-
})
76-
.ToList();
77-
78-
return Ok(tasks);
61+
try
62+
{
63+
var tasksByUser = taskManagerServices.GetTasksByUser(userId);
64+
return Ok(tasksByUser);
65+
}catch(System.Exception)
66+
{
67+
return BadRequest();
68+
}
7969
}
8070

8171
[HttpGet("/GetById/{id:int}")]

Services/TaskManagerServices.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,25 @@ public IActionResult GetAllTasks()
7070
.ToList();
7171
return new OkObjectResult(tasks);
7272
}
73+
74+
public IActionResult GetTasksByUser (int userId)
75+
{
76+
var tasks = context.Tasks
77+
.Where(x => x.UserId == userId)
78+
.Select(task => new
79+
{
80+
TaskId = task.Id,
81+
TaskTitle = task.Title,
82+
TaskDescription = task.Description,
83+
Done = task.Done,
84+
CreatedAt = task.CreatedAt,
85+
CategoryName = context.CategorieTasks
86+
.Where(category => category.Id == task.CategorieTaskId)
87+
.Select(category => category.Name)
88+
.FirstOrDefault()
89+
})
90+
.ToList();
91+
return new OkObjectResult(tasks);
92+
}
7393
}
7494
}

0 commit comments

Comments
 (0)