2024-07-08 17:45:17 +03:30
|
|
|
|
namespace Brizco.Repository.Handlers.Sections;
|
2023-11-14 16:21:49 +03:30
|
|
|
|
|
2024-08-09 21:42:04 +03:30
|
|
|
|
public class DeleteSectionCommandHandler(IRepositoryWrapper repositoryWrapper)
|
|
|
|
|
|
: IRequestHandler<DeleteSectionCommand, bool>
|
2023-11-14 16:21:49 +03:30
|
|
|
|
{
|
|
|
|
|
|
public async Task<bool> Handle(DeleteSectionCommand request, CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
2024-08-09 21:42:04 +03:30
|
|
|
|
var section = await repositoryWrapper.SetRepository<Section>()
|
2023-11-14 16:21:49 +03:30
|
|
|
|
.TableNoTracking
|
|
|
|
|
|
.FirstOrDefaultAsync(s => s.Id == request.Id, cancellationToken);
|
|
|
|
|
|
if (section == null)
|
2023-11-18 22:23:23 +03:30
|
|
|
|
throw new AppException("Section not found", ApiResultStatusCode.NotFound);
|
|
|
|
|
|
|
2024-08-09 21:42:04 +03:30
|
|
|
|
var positions = await repositoryWrapper.SetRepository<Position>()
|
2023-11-18 22:23:23 +03:30
|
|
|
|
.TableNoTracking
|
|
|
|
|
|
.Where(p => p.SectionId == section.Id)
|
|
|
|
|
|
.CountAsync(cancellationToken);
|
|
|
|
|
|
if (positions > 0)
|
|
|
|
|
|
throw new AppException("این سکشن پوزیشن فعال دارد ، نخست پوزیشن های سکشن را حذف کرده یا منتقل کنید");
|
|
|
|
|
|
|
2024-08-09 21:42:04 +03:30
|
|
|
|
repositoryWrapper.SetRepository<Section>()
|
2023-11-14 16:21:49 +03:30
|
|
|
|
.Delete(section);
|
|
|
|
|
|
|
2024-08-09 21:42:04 +03:30
|
|
|
|
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
2023-11-14 16:21:49 +03:30
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|