Api/Brizco.Repository/Handlers/Section/UpdateSectionCommandHandler.cs

39 lines
1.6 KiB
C#

namespace Brizco.Repository.Handlers.Section;
public class UpdateSectionCommandHandler : IRequestHandler<UpdateSectionCommand, bool>
{
private readonly IRepositoryWrapper _repositoryWrapper;
private readonly ICurrentUserService _currentUserService;
public UpdateSectionCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
{
_repositoryWrapper = repositoryWrapper;
_currentUserService = currentUserService;
}
public async Task<bool> Handle(UpdateSectionCommand request, CancellationToken cancellationToken)
{
var shift = await _repositoryWrapper.SetRepository<Domain.Entities.Complex.Section>()
.TableNoTracking.FirstOrDefaultAsync(s => s.Id == request.Id, cancellationToken);
if (shift == null)
throw new AppException("Section not found", ApiResultStatusCode.NotFound);
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);
var newSection = Domain.Entities.Complex.Section.Create(request.Title,
request.Description,
complexId);
newSection.Id = request.Id;
_repositoryWrapper.SetRepository<Domain.Entities.Complex.Section>()
.Update(newSection);
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
return true;
}
}