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

33 lines
1.3 KiB
C#
Raw Permalink Normal View History

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