34 lines
931 B
C#
34 lines
931 B
C#
using AutobusApi.Application.Common.Exceptions;
|
|
using AutobusApi.Application.Common.Interfaces;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace AutobusApi.Application.Companies.Commands.DeleteCompany;
|
|
|
|
public class DeleteCompanyCommandHandler : IRequestHandler<DeleteCompanyCommand>
|
|
{
|
|
private readonly IApplicationDbContext _dbContext;
|
|
|
|
public DeleteCompanyCommandHandler(IApplicationDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
public async Task Handle(
|
|
DeleteCompanyCommand request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var company = await _dbContext.Companies
|
|
.SingleOrDefaultAsync(c => c.Id == request.Id, cancellationToken);
|
|
|
|
if (company == null)
|
|
{
|
|
throw new NotFoundException();
|
|
}
|
|
|
|
_dbContext.Companies.Remove(company);
|
|
|
|
await _dbContext.SaveChangesAsync(cancellationToken);
|
|
}
|
|
}
|