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

31 lines
1.2 KiB
C#
Raw 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 UpdateSectionCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
: IRequestHandler<UpdateSectionCommand, bool>
2023-11-14 16:21:49 +03:30
{
public async Task<bool> Handle(UpdateSectionCommand request, CancellationToken cancellationToken)
{
2024-08-09 21:42:04 +03:30
var shift = await repositoryWrapper.SetRepository<Section>()
2023-11-14 16:21:49 +03:30
.TableNoTracking.FirstOrDefaultAsync(s => s.Id == request.Id, cancellationToken);
if (shift == null)
throw new AppException("Section not found", ApiResultStatusCode.NotFound);
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);
var newSection = Section.Create(request.Title,
2023-11-14 16:21:49 +03:30
request.Description,
complexId);
newSection.Id = request.Id;
2024-08-09 21:42:04 +03:30
repositoryWrapper.SetRepository<Section>()
2023-11-14 16:21:49 +03:30
.Update(newSection);
2024-08-09 21:42:04 +03:30
await repositoryWrapper.SaveChangesAsync(cancellationToken);
2023-11-14 16:21:49 +03:30
return true;
}
}