public async Task <IHttpActionResult> CreateTrip(TripReadModel model)
        {
            var waypoints = model.WayPoints.Select(w => w.Place).ToList();

            waypoints.Insert(0, model.StartPoint);

            waypoints.Add(model.EndPoint);

            DistanseMatrixResponseModel distanseMatrixModel = await _distanceMatrixQuery.GetDistanceMatrix(waypoints);

            if (!distanseMatrixModel.Validate())
            {
                throw new ApplicationException("Invalid response from distanse matrix API");
            }

            double[,] matrix = distanseMatrixModel.ToArray();

            var route = _routeService.CalculateRoute(matrix, 0, waypoints.Count - 1, model.Algorithm);
            var trip  = model.ToEntity();

            _context.Trips.Add(trip);
            await _context.SaveChangesAsync();

            return(Ok(route));
        }
示例#2
0
        /// <summary>
        ///     A DistanseMatrixResponseModel extension method that validates the given model.
        /// </summary>
        ///
        /// <remarks>   Vladyslav, 24.05.2016. </remarks>
        ///
        /// <param name="model">    The model to act on. </param>
        ///
        /// <returns>   true if validation succeeds, false if it fails. </returns>

        internal static bool Validate(this DistanseMatrixResponseModel model)
        {
            if (model.Status != ValidationConstants.Ok)
            {
                return(false);
            }

            if (model.Rows.Any(row => row.Elements.Any(el => el.Status != ValidationConstants.Ok)))
            {
                return(false);
            }

            return(true);
        }
示例#3
0
        /// <summary>
        ///     A DistanseMatrixResponseModel extension method that convert this object into an array
        ///     representation.
        /// </summary>
        ///
        /// <remarks>   Vladyslav, 25.05.2016. </remarks>
        ///
        /// <param name="model">    The model to act on. </param>
        ///
        /// <returns>   An array that represents the data in this object. </returns>

        public static double[,] ToArray(this DistanseMatrixResponseModel model)
        {
            var matrix = new double[model.Rows.Count, model.Rows.Count];

            for (int i = 0; i < model.Rows.Count; i++)
            {
                var row = model.Rows[i];
                for (int j = 0; j < row.Elements.Count; j++)
                {
                    var element = row.Elements[j];
                    matrix[i, j] = element.Distance.Value;
                }
            }

            return(matrix);
        }
示例#4
0
        /// <summary>   Gets distance matrix. </summary>
        ///
        /// <remarks>   Vladyslav, 24.05.2016. </remarks>
        ///
        /// <exception cref="ApplicationException"> Thrown when an response from Google has unsuccessfull status code. </exception>
        ///
        /// <param name="waypoints">    The waypoints. </param>
        ///
        /// <returns>   The distance matrix. </returns>

        public async Task <DistanseMatrixResponseModel> GetDistanceMatrix(IEnumerable <GooglePlaceModel> waypoints)
        {
            string requestWaypoints = waypoints.PrepareWaypointsRequestString();
            string key = ConfigurationManager.AppSettings["API_KEY"];
            string url = string.Format(DistanseMatrixUrl, requestWaypoints, requestWaypoints, key);

            using (var message = new HttpRequestMessage(HttpMethod.Get, url))
                using (var response = await _httpClient.SendAsync(message))
                {
                    if (!response.IsSuccessStatusCode)
                    {
                        throw new ApplicationException("Google Distance Matrix Query Failed");
                    }

                    string json = await response.Content.ReadAsStringAsync();

                    DistanseMatrixResponseModel model = Deserialise(json);
                    return(model);
                }
        }