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