Api/Brizco.Repository/Handlers/Positions/CreatePositionCommandHandle...

36 lines
1.4 KiB
C#

namespace Brizco.Repository.Handlers.Positions;
public class CreatePositionCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
: IRequestHandler<CreatePositionCommand, PositionSDto>
{
public async Task<PositionSDto> Handle(CreatePositionCommand 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 = Position
.Create(request.Title,
request.Description,
complexId,
request.SectionId);
request.Permissions.ForEach(f=>entity.AddPermission(f));
repositoryWrapper.SetRepository<Position>().Add(entity);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
await repositoryWrapper.CommitAsync(cancellationToken);
return entity.AdaptToSDto();
}
catch (Exception)
{
await repositoryWrapper.RollBackAsync(cancellationToken);
throw;
}
}
}