autobus-api_old/AutobusApi.Application/Cities/Commands/CreateCity/CreateCityCommandHandler.cs

32 lines
775 B
C#

using AutobusApi.Application.Common.Interfaces;
using AutobusApi.Domain.Entities;
using MediatR;
namespace AutobusApi.Application.Cities.Commands.CreateCity;
public class CreateCityCommandHandler : IRequestHandler<CreateCityCommand, int>
{
private readonly IApplicationDbContext _dbContext;
public CreateCityCommandHandler(IApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<int> Handle(
CreateCityCommand request,
CancellationToken cancellationToken)
{
var city = new City();
city.Name = request.Name;
city.RegionId = request.RegionId;
_dbContext.Cities.Add(city);
await _dbContext.SaveChangesAsync(cancellationToken);
return city.Id;
}
}