autobus-api_old/AutobusApi.Application/Countries/Commands/CreateCountry/CreateCountryCommandHandler.cs

31 lines
769 B
C#

using AutobusApi.Application.Common.Interfaces;
using AutobusApi.Domain.Entities;
using MediatR;
namespace AutobusApi.Application.Countries.Commands.CreateCountry;
public class CreateCountryCommandHandler : IRequestHandler<CreateCountryCommand, int>
{
private readonly IApplicationDbContext _dbContext;
public CreateCountryCommandHandler(IApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<int> Handle(
CreateCountryCommand request,
CancellationToken cancellationToken)
{
var country = new Country();
country.Name = request.Name;
_dbContext.Countries.Add(country);
await _dbContext.SaveChangesAsync(cancellationToken);
return country.Id;
}
}