autobus-api_old/AutobusApi.Application/Addresses/Commands/CreateAddress/CreateAddressCommandHandler.cs

37 lines
1.0 KiB
C#

using AutobusApi.Application.Common.Interfaces;
using AutobusApi.Domain.Entities;
using AutobusApi.Domain.Enums;
using MediatR;
namespace AutobusApi.Application.Addresses.Commands.CreateAddress;
public class CreateAddressCommandHandler : IRequestHandler<CreateAddressCommand, int>
{
private readonly IApplicationDbContext _dbContext;
public CreateAddressCommandHandler(IApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<int> Handle(
CreateAddressCommand request,
CancellationToken cancellationToken)
{
var address = new Address();
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);
_dbContext.Addresses.Add(address);
await _dbContext.SaveChangesAsync(cancellationToken);
return address.Id;
}
}