autobus-api_old/AutobusApi.Application/Countries/Commands/UpdateCountry/UpdateCountryCommandHandler.cs

33 lines
878 B
C#

using AutobusApi.Application.Common.Exceptions;
using AutobusApi.Application.Common.Interfaces;
using MediatR;
namespace AutobusApi.Application.Countries.Commands.UpdateCountry;
public class UpdateCountryCommandHandler : IRequestHandler<UpdateCountryCommand>
{
private readonly IApplicationDbContext _dbContext;
public UpdateCountryCommandHandler(IApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task Handle(
UpdateCountryCommand request,
CancellationToken cancellationToken)
{
var country = await _dbContext.Countries
.FindAsync(new object[] { request.Id }, cancellationToken);
if (country == null)
{
throw new NotFoundException();
}
country.Name = request.Name;
await _dbContext.SaveChangesAsync(cancellationToken);
}
}