public async Task <RouteDto> Handle(AddRouteReq request, CancellationToken cancellationToken) { var route = new Domain.Route() { Name = request.Name, Status = request.Status, }; await _context.Routes.AddAsync(route); await _context.SaveChangesAsync(cancellationToken); if (route == null) { throw new BadRequestException("Có lỗi xảy ra"); } route.OriginId = request.OriginId == 0 ? route.Id : request.OriginId; await _context.SaveChangesAsync(cancellationToken); if (request.PointIds == null || request.PointIds.Length <= 0) { return(_mapper.Map <RouteDto>(route)); } var existPointIds = request.PointIds != null && request.PointIds.Length > 0; try { if (existPointIds) { var i = 0; foreach (var pointId in request.PointIds) { var routePoint = new RoutePoint() { PointId = pointId, RouteId = route.Id, Position = i++ }; await _context.RoutePoints.AddAsync(routePoint); } await _context.SaveChangesAsync(cancellationToken); } } catch (Exception) { throw new BadRequestException("Có lỗi xảy ra"); } return(_mapper.Map <RouteDto>(route)); }
public static List <Domain.Point> FromDTORoutesToGraph(IEnumerable <Route> routes) { List <Domain.Point> domainPoints = new List <Domain.Point>(); List <Infrastructure.Persistence.DTO.Point> points = routes.Select(x => x.DestinationPoint).Distinct().ToList(); points.AddRange(routes.Select(x => x.OriginalPoint).Distinct()); domainPoints = points.Select(x => new Domain.Point(x.Id, x.Name)).Distinct().ToList(); foreach (var item in routes) { var originPoint = domainPoints.Where(x => x.Id == item.OriginalPoint.Id).FirstOrDefault(); var destinPoint = domainPoints.Where(x => x.Id == item.DestinationPoint.Id).FirstOrDefault(); Domain.Route route = new Domain.Route(destinPoint, item.Cost, item.Time); originPoint.DestinationList.Add(route); } return(domainPoints); }