55 lines
1.7 KiB
C#
55 lines
1.7 KiB
C#
using Indotalent.Infrastructure.Database;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Indotalent.Features.Utilities.Todo.Cqrs;
|
|
|
|
public class UpdateTodoItemRequest
|
|
{
|
|
public string? Id { get; set; }
|
|
public string? Name { get; set; }
|
|
public string? Description { get; set; }
|
|
public DateTime? StartTime { get; set; }
|
|
public DateTime? EndTime { get; set; }
|
|
public bool IsCompleted { get; set; }
|
|
}
|
|
|
|
public class UpdateTodoItemResponse
|
|
{
|
|
public string? Id { get; set; }
|
|
public bool Success { get; set; }
|
|
}
|
|
|
|
public record UpdateTodoItemCommand(UpdateTodoItemRequest Data) : IRequest<UpdateTodoItemResponse>;
|
|
|
|
public class UpdateTodoItemHandler : IRequestHandler<UpdateTodoItemCommand, UpdateTodoItemResponse>
|
|
{
|
|
private readonly AppDbContext _context;
|
|
|
|
public UpdateTodoItemHandler(AppDbContext context) => _context = context;
|
|
|
|
public async Task<UpdateTodoItemResponse> Handle(UpdateTodoItemCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var entity = await _context.TodoItem
|
|
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
|
|
|
if (entity == null)
|
|
{
|
|
return new UpdateTodoItemResponse { Id = request.Data.Id, Success = false };
|
|
}
|
|
|
|
entity.Name = request.Data.Name;
|
|
entity.Description = request.Data.Description;
|
|
entity.StartTime = request.Data.StartTime;
|
|
entity.EndTime = request.Data.EndTime;
|
|
entity.IsCompleted = request.Data.IsCompleted;
|
|
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
return new UpdateTodoItemResponse
|
|
{
|
|
Id = entity.Id,
|
|
Success = true
|
|
};
|
|
}
|
|
} |