Api/Brizco.Repository/Handlers/Sections/CreateSectionCommandHandler.cs

40 lines
1.5 KiB
C#
Raw Normal View History

namespace Brizco.Repository.Handlers.Sections;
2023-11-14 16:21:49 +03:30
public class CreateSectionCommandHandler : IRequestHandler<CreateSectionCommand, SectionSDto>
{
private readonly IRepositoryWrapper _repositoryWrapper;
private readonly ICurrentUserService _currentUserService;
public CreateSectionCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
{
_repositoryWrapper = repositoryWrapper;
_currentUserService = currentUserService;
}
public async Task<SectionSDto> Handle(CreateSectionCommand 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 = Section
2023-11-14 16:21:49 +03:30
.Create(request.Title,
request.Description,
complexId);
_repositoryWrapper.SetRepository<Section>().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;
}
}
}