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