Skip to content

Commit 880bc2c

Browse files
authored
Implemented the first async methods for the CQRS todos controller, update method still needs to be done. Add implementation for the TodoItemRepo with ne CQRS Methods (github#18)
1 parent 0c0de52 commit 880bc2c

File tree

8 files changed

+51
-17
lines changed

8 files changed

+51
-17
lines changed
Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using System.Text.Json;
21
using Microsoft.AspNetCore.Mvc;
3-
using TodoistClone.Application.Services.TodoService;
2+
using TodoistClone.Application.Services.TodoService.Commands;
3+
using TodoistClone.Application.Services.TodoService.Commands.DTOs;
4+
using TodoistClone.Application.Services.TodoService.Commands.DTOs.Delete;
5+
using TodoistClone.Application.Services.TodoService.Queries;
46
using TodoistClone.Contracts.TodoContract;
57

68
namespace TodoistClone.Api.Controllers
@@ -9,44 +11,60 @@ namespace TodoistClone.Api.Controllers
911
[Route("todos")]
1012
public class TodosController : ControllerBase
1113
{
12-
private readonly ITodoService _todoService;
14+
private readonly ITodoQueryService _todoQueryService;
15+
private readonly ITodoCommandService _todoCommandService;
1316

14-
public TodosController(ITodoService todoService)
17+
public TodosController( ITodoQueryService todoQueryService, ITodoCommandService todoCommandService)
1518
{
16-
_todoService = todoService;
19+
_todoQueryService = todoQueryService;
20+
_todoCommandService = todoCommandService;
1721
}
1822

1923
[HttpGet("getById")]
20-
public IActionResult GetById([FromHeader] string id)
24+
public async Task<IActionResult> GetById([FromHeader] string id)
2125
{
2226

2327
if (id is null)
2428
{
2529
return BadRequest();
2630
}
27-
var todoResult = _todoService.GetById(Guid.Parse(id));
31+
var todoResult = await _todoQueryService.GetById(Guid.Parse(id));
2832

2933
var response = new TodoGetResponse(
30-
todoResult.Todo.Id,
31-
todoResult.Todo.Title,
32-
todoResult.Todo.Description,
33-
todoResult.Todo.Done);
34+
todoResult.Id,
35+
todoResult.Title,
36+
todoResult.Description,
37+
todoResult.Done);
3438

3539
return Ok(response);
3640
}
3741

3842

3943
[HttpPost("add")]
40-
public IActionResult Add(TodoPostRequest request)
44+
public async Task<IActionResult> Add(TodoPostRequest request)
4145
{
42-
var todoResult = _todoService.Add(
46+
var todoResult = await _todoCommandService.Create(
47+
new TodoItemCreateRequest(
4348
request.Title,
4449
request.Description,
45-
request.Done);
50+
request.Done
51+
));
4652

4753
var response = new TodoPostResponse(todoResult.Id);
4854

4955
return Ok(response);
5056
}
57+
58+
[HttpDelete("delete")]
59+
public async Task<IActionResult> Delete(string id) {
60+
if (id is null) {
61+
return BadRequest();
62+
}
63+
var todoResult = await _todoCommandService.Delete(new TodoItemDeleteRequest(Guid.Parse(id)));
64+
65+
var response = new TodoDeleteRespone(todoResult.Done);
66+
67+
return Ok(response);
68+
}
5169
}
5270
}

src/TodoistClone.Application/DependencyInjection.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
using Microsoft.Extensions.DependencyInjection;
2+
using TodoistClone.Application.Common.Interfaces.Persistence;
23
using TodoistClone.Application.Services.Authentication.Commands;
34
using TodoistClone.Application.Services.Authentication.Queries;
4-
using TodoistClone.Application.Services.TodoService;
5+
using TodoistClone.Application.Services.TodoService.Commands;
6+
using TodoistClone.Application.Services.TodoService.Commands.DTOs;
7+
using TodoistClone.Application.Services.TodoService.Queries;
8+
using TodoistClone.Domain.Entities;
59

610
namespace TodoistClone.Application;
711

@@ -12,7 +16,8 @@ public static IServiceCollection AddApplication(this IServiceCollection services
1216
{
1317
services.AddScoped<IAuthenticationCommandService, AuthenticationCommandService>();
1418
services.AddScoped<IAuthenticationQueryService, AuthenticationQueryService>();
15-
services.AddScoped<ITodoService, TodoService>();
19+
services.AddScoped<ITodoCommandService, TodoCommandService>();
20+
services.AddScoped<ITodoQueryService, TodoQueryService>();
1621
return services;
1722
}
1823

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace TodoistClone.Application.Services.TodoService.Commands.DTOs;
22

3-
public record TodoItemCreateRequest(string Title,
3+
public record TodoItemCreateRequest(
4+
string Title,
45
string Description,
56
bool Done);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace TodoistClone.Contracts.TodoContract;
2+
3+
public record TodoDeleteRequest(
4+
Guid Id
5+
);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace TodoistClone.Contracts.TodoContract;
2+
3+
public record TodoDeleteRespone(
4+
bool Result
5+
);

0 commit comments

Comments
 (0)