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

27 lines
1.1 KiB
C#

namespace Brizco.Repository.Handlers.Sections;
public class DeleteSectionCommandHandler(IRepositoryWrapper repositoryWrapper)
: IRequestHandler<DeleteSectionCommand, bool>
{
public async Task<bool> Handle(DeleteSectionCommand request, CancellationToken cancellationToken)
{
var section = await repositoryWrapper.SetRepository<Section>()
.TableNoTracking
.FirstOrDefaultAsync(s => s.Id == request.Id, cancellationToken);
if (section == null)
throw new AppException("Section not found", ApiResultStatusCode.NotFound);
var positions = await repositoryWrapper.SetRepository<Position>()
.TableNoTracking
.Where(p => p.SectionId == section.Id)
.CountAsync(cancellationToken);
if (positions > 0)
throw new AppException("این سکشن پوزیشن فعال دارد ، نخست پوزیشن های سکشن را حذف کرده یا منتقل کنید");
repositoryWrapper.SetRepository<Section>()
.Delete(section);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
return true;
}
}