Api/Brizco.Repository/Handlers/Shifts/CreateShiftCommandHandler.cs

48 lines
2.0 KiB
C#
Raw Normal View History

namespace Brizco.Repository.Handlers.Shifts;
2023-09-15 12:37:02 +03:30
public class CreateShiftCommandHandler : IRequestHandler<CreateShiftCommand, ShiftSDto>
2023-09-15 12:37:02 +03:30
{
private readonly IRepositoryWrapper _repositoryWrapper;
private readonly ICurrentUserService _currentUserService;
2023-11-14 16:21:49 +03:30
public CreateShiftCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
2023-09-15 12:37:02 +03:30
{
_repositoryWrapper = repositoryWrapper;
_currentUserService = currentUserService;
2023-09-15 12:37:02 +03:30
}
public async Task<ShiftSDto> Handle(CreateShiftCommand request, CancellationToken cancellationToken)
2023-09-15 12:37:02 +03:30
{
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("روزهای شیفت را انتخاب کنید");
2023-11-13 14:42:49 +03:30
request.DayOfWeeks.ForEach(d => shift.SetDay(d));
if (request.Routines.Count == 0)
throw new AppException("روتین شیفت را انتخاب کنید");
request.Routines.ForEach(r=>shift.AddRoutine(r));
2023-11-13 14:42:49 +03:30
_repositoryWrapper.SetRepository<Shift>().Add(shift);
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
await _repositoryWrapper.CommitAsync(cancellationToken);
return shift.AdaptToSDto();
}
2023-11-14 16:21:49 +03:30
catch (Exception)
{
await _repositoryWrapper.RollBackAsync(cancellationToken);
throw;
}
2023-09-15 12:37:02 +03:30
}
}