autobus-api_old/AutobusApi.Application/Regions/Commands/UpdateRegion/UpdateRegionCommandHandler.cs

34 lines
912 B
C#

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