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

31 lines
1.3 KiB
C#
Raw Permalink Normal View History

namespace Brizco.Repository.Handlers.Routines;
2023-11-14 16:21:49 +03:30
2024-08-09 21:42:04 +03:30
public class CreateRoutineCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
: IRequestHandler<CreateRoutineCommand, RoutineSDto>
2023-11-14 16:21:49 +03:30
{
public async Task<RoutineSDto> Handle(CreateRoutineCommand request, CancellationToken cancellationToken)
{
2024-08-09 21:42:04 +03:30
if (currentUserService.ComplexId == null)
2023-11-14 16:21:49 +03:30
throw new AppException("ComplexId is null", ApiResultStatusCode.NotFound);
2024-08-09 21:42:04 +03:30
if (!Guid.TryParse(currentUserService.ComplexId, out Guid complexId))
2023-11-14 16:21:49 +03:30
throw new AppException("ComplexId is wrong", ApiResultStatusCode.NotFound);
try
{
2024-08-09 21:42:04 +03:30
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-08-09 21:42:04 +03:30
repositoryWrapper.SetRepository<Domain.Entities.Routines.Routine>().Add(entity);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
await repositoryWrapper.CommitAsync(cancellationToken);
2023-11-14 16:21:49 +03:30
return entity.AdaptToSDto();
}
catch (Exception)
{
2024-08-09 21:42:04 +03:30
await repositoryWrapper.RollBackAsync(cancellationToken);
2023-11-14 16:21:49 +03:30
throw;
}
}
}