44 lines
1.7 KiB
C#
44 lines
1.7 KiB
C#
namespace Brizco.Repository.Handlers.Shift;
|
|
|
|
public class CreateShiftCommandHandler : IRequestHandler<CreateShiftCommand, Domain.Entities.Shift.Shift>
|
|
{
|
|
private readonly IRepositoryWrapper _repositoryWrapper;
|
|
private readonly ICurrentUserService _currentUserService;
|
|
|
|
public CreateShiftCommandHandler(IRepositoryWrapper repositoryWrapper,ICurrentUserService currentUserService)
|
|
{
|
|
_repositoryWrapper = repositoryWrapper;
|
|
_currentUserService = currentUserService;
|
|
}
|
|
public async Task<Domain.Entities.Shift.Shift> 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 = Domain.Entities.Shift.Shift
|
|
.Create(request.Title,
|
|
request.Description,
|
|
request.StartAt,
|
|
request.EndAt,
|
|
complexId);
|
|
|
|
request.DayOfWeeks.ForEach(d => shift.SetDay(d));
|
|
|
|
_repositoryWrapper.SetRepository<Domain.Entities.Shift.Shift>().Add(shift);
|
|
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
|
|
await _repositoryWrapper.CommitAsync(cancellationToken);
|
|
return shift;
|
|
}
|
|
catch (Exception )
|
|
{
|
|
await _repositoryWrapper.RollBackAsync(cancellationToken);
|
|
throw;
|
|
}
|
|
}
|
|
} |