Api/Berizco.Repository/Handlers/Shift/UpdateShiftCommandHandler.cs

27 lines
1.1 KiB
C#

namespace Brizco.Repository.Handlers.Shift;
public class UpdateShiftCommandHandler : IRequestHandler<UpdateShiftCommand, bool>
{
private readonly IRepositoryWrapper _repositoryWrapper;
public UpdateShiftCommandHandler(IRepositoryWrapper repositoryWrapper)
{
_repositoryWrapper = repositoryWrapper;
}
public async Task<bool> Handle(UpdateShiftCommand request, CancellationToken cancellationToken)
{
var shift = await _repositoryWrapper.SetRepository<Domain.Entities.Shift.Shift>()
.TableNoTracking.FirstOrDefaultAsync(s => s.Id == request.id);
if (shift == null)
throw new AppException("Shift not found", ApiResultStatusCode.NotFound);
var newShift = Domain.Entities.Shift.Shift.Create(request.Title, request.Description, request.StartAt, request.EndAt);
newShift.Id = request.id;
_repositoryWrapper.SetRepository<Domain.Entities.Shift.Shift>()
.Update(newShift);
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
return true;
}
}