2023-09-15 12:37:02 +03:30
|
|
|
|
namespace Brizco.Repository.Handlers.Shift;
|
|
|
|
|
|
|
2023-09-18 09:45:02 +03:30
|
|
|
|
public class CreateShiftCommandHandler : IRequestHandler<CreateShiftCommand, Domain.Entities.Shift.Shift>
|
2023-09-15 12:37:02 +03:30
|
|
|
|
{
|
|
|
|
|
|
private readonly IRepositoryWrapper _repositoryWrapper;
|
2023-09-18 15:41:41 +03:30
|
|
|
|
private readonly ICurrentUserService _currentUserService;
|
|
|
|
|
|
|
|
|
|
|
|
public CreateShiftCommandHandler(IRepositoryWrapper repositoryWrapper,ICurrentUserService currentUserService)
|
2023-09-15 12:37:02 +03:30
|
|
|
|
{
|
|
|
|
|
|
_repositoryWrapper = repositoryWrapper;
|
2023-09-18 15:41:41 +03:30
|
|
|
|
_currentUserService = currentUserService;
|
2023-09-15 12:37:02 +03:30
|
|
|
|
}
|
|
|
|
|
|
public async Task<Domain.Entities.Shift.Shift> Handle(CreateShiftCommand request, CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
2023-09-18 15:41:41 +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);
|
|
|
|
|
|
|
2023-09-15 12:37:02 +03:30
|
|
|
|
var shift = Domain.Entities.Shift.Shift
|
|
|
|
|
|
.Create(request.Title,
|
|
|
|
|
|
request.Description,
|
|
|
|
|
|
request.StartAt,
|
2023-09-18 15:41:41 +03:30
|
|
|
|
request.EndAt,
|
|
|
|
|
|
complexId);
|
2023-09-15 12:37:02 +03:30
|
|
|
|
|
2023-09-18 09:45:02 +03:30
|
|
|
|
request.DayOfWeeks.ForEach(d => shift.SetDay(d));
|
2023-09-15 12:37:02 +03:30
|
|
|
|
|
|
|
|
|
|
_repositoryWrapper.SetRepository<Domain.Entities.Shift.Shift>().Add(shift);
|
|
|
|
|
|
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
return shift;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|