autobus-api_old/AutobusApi.Application/Addresses/Commands/UpdateAddress/UpdateAddressCommandHandler.cs

39 lines
1.2 KiB
C#

using AutobusApi.Application.Common.Exceptions;
using AutobusApi.Application.Common.Interfaces;
using AutobusApi.Domain.Enums;
using MediatR;
namespace AutobusApi.Application.Addresses.Commands.UpdateAddress;
public class UpdateAddressCommandHandler : IRequestHandler<UpdateAddressCommand>
{
private readonly IApplicationDbContext _dbContext;
public UpdateAddressCommandHandler(IApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task Handle(
UpdateAddressCommand request,
CancellationToken cancellationToken)
{
var address = await _dbContext.Addresses
.FindAsync(new object[] { request.Id }, cancellationToken);
if (address == null)
{
throw new NotFoundException();
}
address.Name = request.Name;
address.CityId = request.CityId;
// TODO: Fix abstraction problems
// address.Location.Latitude = request.Latitude;
// address.Location.Longitude = request.Longitude;
address.VehicleType = Enum.Parse<VehicleType>(request.VehicleType);
await _dbContext.SaveChangesAsync(cancellationToken);
}
}