36 lines
898 B
C#
36 lines
898 B
C#
using Hutopy.Application.Common.Interfaces;
|
|
using Hutopy.Domain.Entities;
|
|
using Hutopy.Domain.Events;
|
|
|
|
namespace Hutopy.Application.TodoItems.Commands.CreateTodoItem;
|
|
|
|
public record CreateTodoItemCommand : IRequest<int>
|
|
{
|
|
public int ListId { get; init; }
|
|
|
|
public string? Title { get; init; }
|
|
}
|
|
|
|
public class CreateTodoItemCommandHandler(
|
|
IApplicationDbContext context)
|
|
: IRequestHandler<CreateTodoItemCommand, int>
|
|
{
|
|
public async Task<int> Handle(CreateTodoItemCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var entity = new TodoItem
|
|
{
|
|
ListId = request.ListId,
|
|
Title = request.Title,
|
|
Done = false
|
|
};
|
|
|
|
entity.AddDomainEvent(new TodoItemCreatedEvent(entity));
|
|
|
|
context.TodoItems.Add(entity);
|
|
|
|
await context.SaveChangesAsync(cancellationToken);
|
|
|
|
return entity.Id;
|
|
}
|
|
}
|