autobus-api_old/AutobusApi.Application/Regions/Commands/CreateRegion/CreateRegionCommandHandler.cs

32 lines
801 B
C#

using AutobusApi.Application.Common.Interfaces;
using AutobusApi.Domain.Entities;
using MediatR;
namespace AutobusApi.Application.Regions.Commands.CreateRegion;
public class CreateRegionCommandHandler : IRequestHandler<CreateRegionCommand, int>
{
private readonly IApplicationDbContext _dbContext;
public CreateRegionCommandHandler(IApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<int> Handle(
CreateRegionCommand request,
CancellationToken cancellationToken)
{
var region = new Region();
region.Name = request.Name;
region.CountryId = request.CountryId;
_dbContext.Regions.Add(region);
await _dbContext.SaveChangesAsync(cancellationToken);
return region.Id;
}
}