2024-07-08 17:45:17 +03:30
|
|
|
|
namespace Brizco.Repository.Handlers.Shifts;
|
2023-09-15 12:37:02 +03:30
|
|
|
|
|
2024-08-09 21:42:04 +03:30
|
|
|
|
public class CreateShiftCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
|
|
|
|
|
|
: IRequestHandler<CreateShiftCommand, ShiftSDto>
|
2023-09-15 12:37:02 +03:30
|
|
|
|
{
|
2023-10-26 01:24:04 +03:30
|
|
|
|
public async Task<ShiftSDto> Handle(CreateShiftCommand request, CancellationToken cancellationToken)
|
2023-09-15 12:37:02 +03:30
|
|
|
|
{
|
2023-09-18 15:41:41 +03:30
|
|
|
|
|
2024-08-09 21:42:04 +03:30
|
|
|
|
if (currentUserService.ComplexId == null)
|
2023-09-18 15:41:41 +03:30
|
|
|
|
throw new AppException("ComplexId is null", ApiResultStatusCode.NotFound);
|
2024-08-09 21:42:04 +03:30
|
|
|
|
if (!Guid.TryParse(currentUserService.ComplexId, out Guid complexId))
|
2023-09-18 15:41:41 +03:30
|
|
|
|
throw new AppException("ComplexId is wrong", ApiResultStatusCode.NotFound);
|
|
|
|
|
|
|
2023-09-20 11:54:12 +03:30
|
|
|
|
try
|
|
|
|
|
|
{
|
2024-08-09 21:42:04 +03:30
|
|
|
|
await repositoryWrapper.BeginTransaction(cancellationToken);
|
2024-07-08 17:45:17 +03:30
|
|
|
|
var shift = Shift
|
2023-09-20 11:54:12 +03:30
|
|
|
|
.Create(request.Title,
|
|
|
|
|
|
request.Description,
|
|
|
|
|
|
request.StartAt,
|
|
|
|
|
|
request.EndAt,
|
|
|
|
|
|
complexId);
|
2023-11-18 22:23:23 +03:30
|
|
|
|
if (request.DayOfWeeks.Count == 0)
|
|
|
|
|
|
throw new AppException("روزهای شیفت را انتخاب کنید");
|
2023-11-13 14:42:49 +03:30
|
|
|
|
request.DayOfWeeks.ForEach(d => shift.SetDay(d));
|
2023-11-18 22:23:23 +03:30
|
|
|
|
if (request.Routines.Count == 0)
|
|
|
|
|
|
throw new AppException("روتین شیفت را انتخاب کنید");
|
|
|
|
|
|
request.Routines.ForEach(r=>shift.AddRoutine(r));
|
2023-11-13 14:42:49 +03:30
|
|
|
|
|
2024-08-09 21:42:04 +03:30
|
|
|
|
repositoryWrapper.SetRepository<Shift>().Add(shift);
|
|
|
|
|
|
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
await repositoryWrapper.CommitAsync(cancellationToken);
|
2023-10-26 01:24:04 +03:30
|
|
|
|
return shift.AdaptToSDto();
|
2023-09-20 11:54:12 +03:30
|
|
|
|
}
|
2023-11-14 16:21:49 +03:30
|
|
|
|
catch (Exception)
|
2023-09-20 11:54:12 +03:30
|
|
|
|
{
|
2024-08-09 21:42:04 +03:30
|
|
|
|
await repositoryWrapper.RollBackAsync(cancellationToken);
|
2023-09-20 11:54:12 +03:30
|
|
|
|
throw;
|
|
|
|
|
|
}
|
2023-09-15 12:37:02 +03:30
|
|
|
|
}
|
|
|
|
|
|
}
|