Skip to content

Commit 1157db5

Browse files
Refactor services with DAL
1 parent efa6e37 commit 1157db5

7 files changed

Lines changed: 122 additions & 24 deletions

File tree

Dal/CategorieTaskDal.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Todo.Data;
2+
using Todo.Domain;
3+
4+
namespace Todo.Dal
5+
{
6+
public class CategorieTaskDal
7+
{
8+
private readonly AppDbContext _context;
9+
10+
public CategorieTaskDal(AppDbContext context)
11+
{
12+
_context = context;
13+
}
14+
15+
public void AddCategorieTask(CategorieTaskEntity entity)
16+
{
17+
_context.CategorieTasks.Add(entity);
18+
_context.SaveChanges();
19+
}
20+
21+
public void UpdateCategorieTask(CategorieTaskEntity entity)
22+
{
23+
_context.CategorieTasks.Update(entity);
24+
_context.SaveChanges();
25+
}
26+
}
27+
}

Dal/TaskDal.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Todo.Data;
2+
using Todo.Domain;
3+
4+
namespace Todo.Dal
5+
{
6+
public class TaskDal
7+
{
8+
private readonly AppDbContext _context;
9+
10+
public TaskDal(AppDbContext context)
11+
{
12+
_context = context;
13+
}
14+
15+
public int InsertTask(TaskEntity task)
16+
{
17+
_context.Tasks.Add(task);
18+
_context.SaveChanges();
19+
return task.Id;
20+
}
21+
22+
public TaskEntity UpdateTask(TaskEntity task)
23+
{
24+
_context.Tasks.Update(task);
25+
_context.SaveChanges();
26+
return task;
27+
}
28+
29+
public void DeleteTask(TaskEntity task)
30+
{
31+
_context.Tasks.Remove(task);
32+
_context.SaveChanges();
33+
}
34+
35+
public TaskEntity? ToggleDone(int id)
36+
{
37+
var task = _context.Tasks.FirstOrDefault(t => t.Id == id);
38+
if (task == null)
39+
return null;
40+
task.Done = !task.Done;
41+
_context.Tasks.Update(task);
42+
_context.SaveChanges();
43+
return task;
44+
}
45+
}
46+
}

Dal/UserDal.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Todo.Data;
2+
using Todo.Domain;
3+
4+
namespace Todo.Dal
5+
{
6+
public class UserDal
7+
{
8+
private readonly AppDbContext _context;
9+
10+
public UserDal(AppDbContext context)
11+
{
12+
_context = context;
13+
}
14+
15+
public void AddUser(UserEntity user)
16+
{
17+
_context.Users.Add(user);
18+
_context.SaveChanges();
19+
}
20+
}
21+
}

Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.OpenApi.Models;
99
using Todo.Services;
1010
using Todo;
11+
using Todo.Dal;
1112

1213
var builder = WebApplication.CreateBuilder(args);
1314

@@ -38,6 +39,9 @@
3839
builder.Services.AddScoped<TaskManagerServices>();
3940
builder.Services.AddScoped<LoginService>();
4041
builder.Services.AddScoped<CategorieTaskService>();
42+
builder.Services.AddScoped<Todo.Dal.TaskDal>();
43+
builder.Services.AddScoped<Todo.Dal.CategorieTaskDal>();
44+
builder.Services.AddScoped<Todo.Dal.UserDal>();
4145

4246

4347
var key = Encoding.ASCII.GetBytes(Settings.Secret);

Services/CategorieTaskService.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
using Todo.Data;
33
using Todo.Domain;
44
using Todo.Models;
5+
using Todo.Dal;
56

67
namespace Todo.Services
78
{
89
public class CategorieTaskService:ControllerBase
910
{
1011
public readonly AppDbContext _context;
12+
private readonly CategorieTaskDal _dal;
1113

12-
public CategorieTaskService(AppDbContext context)
14+
public CategorieTaskService(AppDbContext context, CategorieTaskDal dal)
1315
{
1416
_context = context;
17+
_dal = dal;
1518
}
1619

1720
public IActionResult Get()
@@ -37,8 +40,7 @@ public IActionResult CreateCategorieTask([FromBody] CategorieTaskModel model)
3740
Description = model.Description
3841
};
3942

40-
_context.CategorieTasks.Add(categorieTask);
41-
_context.SaveChanges();
43+
_dal.AddCategorieTask(categorieTask);
4244
return Ok("Categoria de tarefa criada com sucesso.");
4345
}
4446
catch (Exception ex)
@@ -60,8 +62,7 @@ public IActionResult UpdateCategorieTask([FromBody] CategorieTaskModel model)
6062
categorieToUpdate.Name = model.Name;
6163
categorieToUpdate.Description = model.Description;
6264

63-
_context.CategorieTasks.Update(categorieToUpdate);
64-
_context.SaveChanges();
65+
_dal.UpdateCategorieTask(categorieToUpdate);
6566
return Ok("Categoria de tarefa atualizada com sucesso.");
6667
}
6768
catch (Exception ex)

Services/LoginService.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Todo.Data;
77
using Todo.Domain;
88
using Todo.Models;
9+
using Todo.Dal;
910

1011

1112
namespace Todo.Services
@@ -14,11 +15,13 @@ namespace Todo.Services
1415
public class LoginService: ControllerBase
1516
{
1617
private readonly AppDbContext _context;
18+
private readonly UserDal _dal;
1719

18-
public LoginService(AppDbContext context)
20+
public LoginService(AppDbContext context, UserDal dal)
1921
{
2022
_context = context;
21-
}
23+
_dal = dal;
24+
}
2225

2326
public IActionResult ListUsers ()
2427
{
@@ -30,8 +33,7 @@ public IActionResult CreateAccount([FromBody] UserEntity model)
3033
{
3134
if (model != null)
3235
{
33-
_context.Users.Add(model);
34-
_context.SaveChanges();
36+
_dal.AddUser(model);
3537
return new ObjectResult("Conta de usuário criada com sucesso.");
3638
}
3739
else

Services/TaskManagerServices.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
using Todo.Data;
33
using Todo.Domain;
44
using Todo.Models;
5+
using Todo.Dal;
56

67
namespace Todo.Services
78
{
89
public class TaskManagerServices: ControllerBase
910
{
1011
private readonly AppDbContext context;
12+
private readonly TaskDal _taskDal;
1113

12-
public TaskManagerServices(AppDbContext dbContext)
14+
public TaskManagerServices(AppDbContext dbContext, TaskDal taskDal)
1315
{
1416
context = dbContext;
17+
_taskDal = taskDal;
1518
}
1619

1720
public IActionResult GetTasksToDo()
@@ -132,10 +135,9 @@ public IActionResult InsertTask (TaskModel model, int userId)
132135
UserId = userId
133136
};
134137

135-
context.Tasks.Add(newTask);
136-
context.SaveChanges();
138+
var newId = _taskDal.InsertTask(newTask);
137139

138-
return Ok(new { taskId = newTask.Id });
140+
return Ok(new { taskId = newId });
139141
}
140142
catch (Exception ex)
141143
{
@@ -155,11 +157,11 @@ public IActionResult EditTask (TaskModel model, int id)
155157
return new NotFoundResult();
156158
}
157159

160+
158161
taskToEdit.Title = model.Title;
159162
taskToEdit.Description = model.Description;
160163

161-
context.Tasks.Update(taskToEdit);
162-
context.SaveChanges();
164+
_taskDal.UpdateTask(taskToEdit);
163165

164166
return Ok(taskToEdit);
165167
}
@@ -169,8 +171,7 @@ public IActionResult DeleteTask (int id)
169171

170172
if (taskToDelete == null) return new NotFoundResult();
171173

172-
context.Tasks.Remove(taskToDelete);
173-
context.SaveChanges();
174+
_taskDal.DeleteTask(taskToDelete);
174175

175176
return Ok();
176177
}
@@ -180,12 +181,9 @@ public IActionResult DoneTask (int id)
180181

181182
if (task == null) return new BadRequestResult();
182183

183-
task.Done = !task.Done;
184-
185-
context.Tasks.Update(task);
186-
context.SaveChanges();
184+
var updated = _taskDal.ToggleDone(id);
187185

188-
return Ok(task);
186+
return updated == null ? BadRequest() : Ok(updated);
189187
}
190188
public IActionResult AsignTask (TaskModel model)
191189
{
@@ -205,8 +203,7 @@ public IActionResult AsignTask (TaskModel model)
205203
UserId = model.UserId
206204
};
207205

208-
context.Tasks.Add(taskToAsign);
209-
context.SaveChanges();
206+
_taskDal.InsertTask(taskToAsign);
210207

211208
return Ok(taskToAsign);
212209
}

0 commit comments

Comments
 (0)