forked from ardalis/CleanArchitecture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddToDoItemHandler.cs
More file actions
36 lines (29 loc) · 1.07 KB
/
AddToDoItemHandler.cs
File metadata and controls
36 lines (29 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using NimblePros.SampleToDo.Core.ProjectAggregate;
using NimblePros.SampleToDo.Core.ProjectAggregate.Specifications;
namespace NimblePros.SampleToDo.UseCases.Projects.Commands.AddToDoItem;
public class AddToDoItemHandler : ICommandHandler<AddToDoItemCommand, Result<ToDoItemId>>
{
private readonly IRepository<Project> _repository;
public AddToDoItemHandler(IRepository<Project> repository)
{
_repository = repository;
}
public async ValueTask<Result<ToDoItemId>> Handle(AddToDoItemCommand request,
CancellationToken cancellationToken)
{
var spec = new ProjectByIdWithItemsSpec(request.ProjectId);
var entity = await _repository.FirstOrDefaultAsync(spec, cancellationToken);
if (entity == null)
{
return Result.NotFound();
}
var newItem = new ToDoItem(title: request.Title!, description: request.Description!);
if (request.ContributorId.HasValue)
{
newItem.AddContributor(request.ContributorId.Value);
}
entity.AddItem(newItem);
await _repository.UpdateAsync(entity);
return Result.Success(newItem.Id);
}
}