namespace Brizco.Repository.Handlers.Shifts; public class CreateShiftCommandHandler : IRequestHandler { private readonly IRepositoryWrapper _repositoryWrapper; private readonly ICurrentUserService _currentUserService; public CreateShiftCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService) { _repositoryWrapper = repositoryWrapper; _currentUserService = currentUserService; } public async Task 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().Add(shift); await _repositoryWrapper.SaveChangesAsync(cancellationToken); await _repositoryWrapper.CommitAsync(cancellationToken); return shift.AdaptToSDto(); } catch (Exception) { await _repositoryWrapper.RollBackAsync(cancellationToken); throw; } } }