Api/Berizco.Repository/Handlers/ShiftPlan/CreateShiftPlanCommandHandl...

30 lines
1.3 KiB
C#

namespace Brizco.Repository.Handlers.ShiftPlan;
public class CreateShiftPlanCommandHandler : IRequestHandler<CreateShiftPlanCommand, ShiftPlanLDto>
{
private readonly IRepositoryWrapper _repositoryWrapper;
public CreateShiftPlanCommandHandler(IRepositoryWrapper repositoryWrapper)
{
_repositoryWrapper = repositoryWrapper;
}
public async Task<ShiftPlanLDto> Handle(CreateShiftPlanCommand request, CancellationToken cancellationToken)
{
var shift = await _repositoryWrapper.SetRepository<Domain.Entities.Shift.Shift>()
.TableNoTracking
.FirstOrDefaultAsync(s => s.Id == request.ShiftId, cancellationToken);
if (shift == null)
throw new AppException("Shift not found", ApiResultStatusCode.NotFound);
var shiftPlan = shift.AddPlan(request.StartAt, request.EndAt);
if (request.UserIds.Count == 0)
throw new AppException("شیفت بندی مورد نظر باید حداقل متشکل از یک فرد باشد", ApiResultStatusCode.BadRequest);
request.UserIds.ForEach(i=>shiftPlan.AddUser(i));
_repositoryWrapper.SetRepository<Domain.Entities.Shift.ShiftPlan>().Add(shiftPlan);
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
return shiftPlan.AdaptToLDto();
}
}