36 lines
904 B
C#
36 lines
904 B
C#
using AutobusApi.Application.Common.Interfaces;
|
|
using AutobusApi.Domain.Entities;
|
|
using MediatR;
|
|
|
|
namespace AutobusApi.Application.Routes.Commands.CreateRoute;
|
|
|
|
public class CreateRouteCommandHandler : IRequestHandler<CreateRouteCommand, int>
|
|
{
|
|
private readonly IApplicationDbContext _dbContext;
|
|
|
|
public CreateRouteCommandHandler(IApplicationDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
public async Task<int> Handle(
|
|
CreateRouteCommand request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var route = new Route();
|
|
|
|
route.RouteAddresses = request.Addresses.Select(a =>
|
|
new RouteAddress
|
|
{
|
|
Order = a.Order,
|
|
AddressId = a.Id
|
|
}).ToList();
|
|
|
|
_dbContext.Routes.Add(route);
|
|
|
|
await _dbContext.SaveChangesAsync(cancellationToken);
|
|
|
|
return route.Id;
|
|
}
|
|
}
|