autobus-api_old/AutobusApi.Application/Buses/Commands/CreateBus/CreateBusCommandHandler.cs

39 lines
1.1 KiB
C#

using AutobusApi.Application.Common.Interfaces;
using AutobusApi.Domain.Entities;
using MediatR;
namespace AutobusApi.Application.Buses.Commands.CreateBus;
public class CreateBusCommandHandler : IRequestHandler<CreateBusCommand, int>
{
private readonly IApplicationDbContext _dbContext;
public CreateBusCommandHandler(IApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<int> Handle(
CreateBusCommand request,
CancellationToken cancellationToken)
{
var bus = new Bus();
bus.CompanyId = request.CompanyId;
bus.Number = request.Number;
bus.Model = request.Model;
bus.Capacity = request.Capacity;
bus.HasClimateControl = request.HasClimateControl;
bus.HasWiFi = request.HasWiFi;
bus.HasMultimedia = request.HasMultimedia;
bus.HasWC = request.HasWC;
bus.HasOutlets = request.HasOutlets;
_dbContext.Vehicles.Add(bus);
await _dbContext.SaveChangesAsync(cancellationToken);
return bus.Id;
}
}