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

41 lines
1.7 KiB
C#
Raw Permalink Normal View History

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
{
public async Task<ShiftSDto> Handle(CreateShiftCommand request, CancellationToken cancellationToken)
2023-09-15 12:37:02 +03:30
{
2024-08-09 21:42:04 +03:30
if (currentUserService.ComplexId == null)
throw new AppException("ComplexId is null", ApiResultStatusCode.NotFound);
2024-08-09 21:42:04 +03:30
if (!Guid.TryParse(currentUserService.ComplexId, out Guid complexId))
throw new AppException("ComplexId is wrong", ApiResultStatusCode.NotFound);
try
{
2024-08-09 21:42:04 +03:30
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
2024-08-09 21:42:04 +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)
{
2024-08-09 21:42:04 +03:30
await repositoryWrapper.RollBackAsync(cancellationToken);
throw;
}
2023-09-15 12:37:02 +03:30
}
}