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

33 lines
1.3 KiB
C#

namespace Brizco.Repository.Handlers.Sections;
public class CreateSectionCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
: IRequestHandler<CreateSectionCommand, SectionSDto>
{
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
.Create(request.Title,
request.Description,
complexId);
repositoryWrapper.SetRepository<Section>().Add(entity);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
await repositoryWrapper.CommitAsync(cancellationToken);
return entity.AdaptToSDto();
}
catch (Exception)
{
await repositoryWrapper.RollBackAsync(cancellationToken);
throw;
}
}
}