autobus-api_old/AutobusApi.Application/Companies/Commands/UpdateCompany/UpdateCompanyCommandHandler.cs

36 lines
1.0 KiB
C#

using AutobusApi.Application.Common.Exceptions;
using AutobusApi.Application.Common.Interfaces;
using MediatR;
namespace AutobusApi.Application.Companies.Commands.UpdateCompany;
public class UpdateCompanyCommandHandler : IRequestHandler<UpdateCompanyCommand>
{
private readonly IApplicationDbContext _dbContext;
public UpdateCompanyCommandHandler(IApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task Handle(
UpdateCompanyCommand request,
CancellationToken cancellationToken)
{
var company = await _dbContext.Companies
.FindAsync(new object[] { request.Id }, cancellationToken);
if (company == null)
{
throw new NotFoundException();
}
company.Name = request.Name;
company.LegalAddress = request.LegalAddress;
company.ContactEmail = request.ContactEmail;
company.ContactPhoneNumber = request.ContactPhoneNumber;
await _dbContext.SaveChangesAsync(cancellationToken);
}
}