59 lines
1.8 KiB
C#
59 lines
1.8 KiB
C#
using Indotalent.Infrastructure.Database;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Indotalent.Features.Utilities.Todo.Cqrs;
|
|
|
|
public class UpdateTodoRequest
|
|
{
|
|
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 DateTimeOffset? CreatedAt { get; set; }
|
|
public string? CreatedBy { get; set; }
|
|
public DateTimeOffset? UpdatedAt { get; set; }
|
|
public string? UpdatedBy { get; set; }
|
|
}
|
|
|
|
public class UpdateTodoResponse
|
|
{
|
|
public string? Id { get; set; }
|
|
public bool Success { get; set; }
|
|
}
|
|
|
|
public record UpdateTodoCommand(UpdateTodoRequest Data) : IRequest<UpdateTodoResponse>;
|
|
|
|
public class UpdateTodoHandler : IRequestHandler<UpdateTodoCommand, UpdateTodoResponse>
|
|
{
|
|
private readonly AppDbContext _context;
|
|
|
|
public UpdateTodoHandler(AppDbContext context) => _context = context;
|
|
|
|
public async Task<UpdateTodoResponse> Handle(UpdateTodoCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var entity = await _context.Todo
|
|
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
|
|
|
if (entity == null)
|
|
{
|
|
return new UpdateTodoResponse { 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 UpdateTodoResponse
|
|
{
|
|
Id = entity.Id,
|
|
Success = true
|
|
};
|
|
}
|
|
} |