namespace Brizco.Repository.Handlers.Routines; public class CreateRoutineCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService) : IRequestHandler { public async Task 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); var entity = Domain.Entities.Routines.Routine .Create(request.Name,request.Description,complexId); repositoryWrapper.SetRepository().Add(entity); await repositoryWrapper.SaveChangesAsync(cancellationToken); await repositoryWrapper.CommitAsync(cancellationToken); return entity.AdaptToSDto(); } catch (Exception) { await repositoryWrapper.RollBackAsync(cancellationToken); throw; } } }