using AutobusApi.Application.Common.Exceptions; using AutobusApi.Application.Common.Interfaces; using MediatR; using Microsoft.EntityFrameworkCore; namespace AutobusApi.Application.Regions.Commands.DeleteRegion; public class DeleteRegionCommandHandler : IRequestHandler { private readonly IApplicationDbContext _dbContext; public DeleteRegionCommandHandler(IApplicationDbContext dbContext) { _dbContext = dbContext; } public async Task Handle( DeleteRegionCommand request, CancellationToken cancellationToken) { var region = await _dbContext.Regions .SingleOrDefaultAsync(c => c.Id == request.Id, cancellationToken); if (region == null) { throw new NotFoundException(); } _dbContext.Regions.Remove(region); await _dbContext.SaveChangesAsync(cancellationToken); } }