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