Api/Brizco.Repository/Handlers/Routines/CreateRoutineCommandHandler.cs

38 lines
1.5 KiB
C#
Raw Normal View History

namespace Brizco.Repository.Handlers.Routines;
2023-11-14 16:21:49 +03:30
public class CreateRoutineCommandHandler : IRequestHandler<CreateRoutineCommand, RoutineSDto>
{
private readonly IRepositoryWrapper _repositoryWrapper;
private readonly ICurrentUserService _currentUserService;
public CreateRoutineCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
{
_repositoryWrapper = repositoryWrapper;
_currentUserService = currentUserService;
}
public async Task<RoutineSDto> Handle(CreateRoutineCommand 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);
2024-07-06 21:20:10 +03:30
var entity = Domain.Entities.Routines.Routine
2023-11-19 22:06:49 +03:30
.Create(request.Name,request.Description,complexId);
2023-11-14 16:21:49 +03:30
2024-07-06 21:20:10 +03:30
_repositoryWrapper.SetRepository<Domain.Entities.Routines.Routine>().Add(entity);
2023-11-14 16:21:49 +03:30
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
await _repositoryWrapper.CommitAsync(cancellationToken);
return entity.AdaptToSDto();
}
catch (Exception)
{
await _repositoryWrapper.RollBackAsync(cancellationToken);
throw;
}
}
}