41 lines
1.7 KiB
C#
41 lines
1.7 KiB
C#
namespace Brizco.Repository.Handlers.Shifts;
|
|
|
|
public class CreateShiftCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
|
|
: IRequestHandler<CreateShiftCommand, ShiftSDto>
|
|
{
|
|
public async Task<ShiftSDto> Handle(CreateShiftCommand request, CancellationToken cancellationToken)
|
|
{
|
|
|
|
if (currentUserService.ComplexId == null)
|
|
throw new AppException("ComplexId is null", ApiResultStatusCode.NotFound);
|
|
if (!Guid.TryParse(currentUserService.ComplexId, out Guid complexId))
|
|
throw new AppException("ComplexId is wrong", ApiResultStatusCode.NotFound);
|
|
|
|
try
|
|
{
|
|
await repositoryWrapper.BeginTransaction(cancellationToken);
|
|
var shift = Shift
|
|
.Create(request.Title,
|
|
request.Description,
|
|
request.StartAt,
|
|
request.EndAt,
|
|
complexId);
|
|
if (request.DayOfWeeks.Count == 0)
|
|
throw new AppException("روزهای شیفت را انتخاب کنید");
|
|
request.DayOfWeeks.ForEach(d => shift.SetDay(d));
|
|
if (request.Routines.Count == 0)
|
|
throw new AppException("روتین شیفت را انتخاب کنید");
|
|
request.Routines.ForEach(r=>shift.AddRoutine(r));
|
|
|
|
repositoryWrapper.SetRepository<Shift>().Add(shift);
|
|
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
|
await repositoryWrapper.CommitAsync(cancellationToken);
|
|
return shift.AdaptToSDto();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
await repositoryWrapper.RollBackAsync(cancellationToken);
|
|
throw;
|
|
}
|
|
}
|
|
} |