autobus-api_old/AutobusApi.Application/Buses/Commands/UpdateBus/UpdateBusCommandHandler.cs

41 lines
1.2 KiB
C#

using AutobusApi.Application.Common.Exceptions;
using AutobusApi.Application.Common.Interfaces;
using MediatR;
namespace AutobusApi.Application.Buses.Commands.UpdateBus;
public class UpdateBusCommandHandler : IRequestHandler<UpdateBusCommand>
{
private readonly IApplicationDbContext _dbContext;
public UpdateBusCommandHandler(IApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task Handle(
UpdateBusCommand request,
CancellationToken cancellationToken)
{
var bus = await _dbContext.Buses
.FindAsync(new object[] { request.Id }, cancellationToken);
if (bus == null)
{
throw new NotFoundException();
}
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;
await _dbContext.SaveChangesAsync(cancellationToken);
}
}