autobus-api_old/AutobusApi.Application/Cities/Commands/UpdateCity/UpdateCityCommandHandler.cs

34 lines
890 B
C#

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