Skip to content

Commit 937403a

Browse files
Refactor: refatorando controller parte 1. Adicionando services.
Regra: Controller pode no máximo chamar uma service.
1 parent 7cf5583 commit 937403a

2 files changed

Lines changed: 52 additions & 18 deletions

File tree

Controllers/TaskManagerController.cs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
11
using Microsoft.AspNetCore.Authorization;
22
using Microsoft.AspNetCore.Mvc;
33
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.IdentityModel.Tokens;
45
using Todo.Data;
6+
using Todo.Migrations;
57
using Todo.Models;
8+
using Todo.Services;
69

710
namespace Todo.Controllers
811
{
912
[ApiController]
1013
public class TaskManagerController : ControllerBase
1114
{
15+
private readonly TaskManagerServices taskManagerServices;
16+
1217
[HttpGet("/TasksToDo")]
13-
public IActionResult Get([FromServices] AppDbContext context)
18+
public IActionResult Get()
1419
{
15-
16-
var tasksDone = context.Tasks
17-
.Where(x => x.Done == false)
18-
.Select(task => new
19-
{
20-
TaskId = task.Id,
21-
TaskTitle = task.Title,
22-
TaskDescription = task.Description,
23-
Done = task.Done,
24-
CreatedAt = task.CreatedAt,
25-
CategoryName = context.CategorieTasks
26-
.Where(category => category.Id == task.CategorieTaskId)
27-
.Select(category => category.Name)
28-
.FirstOrDefault()
29-
})
30-
.ToList();
31-
return Ok(tasksDone);
20+
try
21+
{
22+
var tasks = taskManagerServices.GetTasksToDo();
23+
return Ok(tasks);
24+
}
25+
catch (System.Exception)
26+
{
27+
return BadRequest();
28+
}
3229
}
3330

3431
[HttpGet("/ListTaskDone")]

Services/TaskManagerServices.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Todo.Data;
3+
4+
namespace Todo.Services
5+
{
6+
public class TaskManagerServices
7+
{
8+
private readonly AppDbContext context;
9+
10+
public TaskManagerServices(AppDbContext dbContext)
11+
{
12+
context = dbContext;
13+
}
14+
15+
public IActionResult GetTasksToDo()
16+
{
17+
var tasks = context.Tasks
18+
.Where(x => x.Done == false)
19+
.Select(task => new
20+
{
21+
TaskId = task.Id,
22+
TaskTitle = task.Title,
23+
TaskDescription = task.Description,
24+
Done = task.Done,
25+
CreatedAt = task.CreatedAt,
26+
CategoryName = context.CategorieTasks
27+
.Where(category => category.Id == task.CategorieTaskId)
28+
.Select(category => category.Name)
29+
.FirstOrDefault()
30+
})
31+
.ToList();
32+
return new OkObjectResult(tasks);
33+
}
34+
35+
36+
}
37+
}

0 commit comments

Comments
 (0)