public ActionResult SearchRoute(Shippment model) { var shippment = new Shippment(); var Model = new Shippment(); var categories = DbHelper.GetAllCategoriesFromDb(); Model.Categories = GetCategoryListItems(categories); var cities = DbHelper.GetAllCities(); Model.CitiesFrom = GetCityListItems(cities); Model.CitiesTo = GetCityListItems(cities); if (ModelState.IsValid) { shippment.Category = model.Category; shippment.Weight = model.Weight; shippment.CityFrom = model.CityFrom; shippment.CityTo = model.CityTo; CalculatedRoute calculatedRoute = null; CalculatedRoute calculatedRouteCheap = null; try { calculatedRoute = RouteCalculator.Calculate(model.Category, model.Weight, DbHelper.GetCityByName(model.CityFrom), DbHelper.GetCityByName(model.CityTo), true); calculatedRouteCheap = RouteCalculator.Calculate(model.Category, model.Weight, DbHelper.GetCityByName(model.CityFrom), DbHelper.GetCityByName(model.CityTo), false); } catch (Exception e) { Console.WriteLine(e.ToString()); } shippment.searchedSections = calculatedRoute; shippment.searchedSectionsCheap = calculatedRouteCheap; shippment.Categories = GetCategoryListItems(categories); shippment.CitiesFrom = GetCityListItems(cities); shippment.CitiesTo = GetCityListItems(cities); if (shippment.searchedSections != null && shippment.searchedSections.Route != null) { for (int i = 0; i < shippment.searchedSections.Route.Count; i++) { TempData["price" + i] = shippment.searchedSections.Route[i].Price.ToString(); TempData["duration" + i] = shippment.searchedSections.Route[i].Duration.ToString(); TempData["from" + i] = shippment.searchedSections.Route[i].From.Name; TempData["to" + i] = shippment.searchedSections.Route[i].To.Name; } TempData["price"] = shippment.searchedSections.Price; TempData["duration"] = shippment.searchedSections.Duration; } return(View("index", shippment)); } return(View("index", Model)); }
// GET api/<controller>/5 public IHttpActionResult Get(string fromName, string toName, string parcelType, int weight, int filter) { fromName = fromName.ToUpper(); toName = toName.ToUpper(); if (!DbHelper.GetAllCities().Select(x => x.Name).Contains(fromName) || !DbHelper.GetAllCities().Select(x => x.Name).Contains(toName)) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "One of the specified cities was not found")); } if (!DbHelper.GetAllCategoriesFromDb().Select(x => x.Name).Contains(parcelType)) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The specified parcel type was not found")); } if (!(filter == 0 || filter == 1)) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The specified filter was not found")); } if (weight > 100) { return(Json(new {})); } try { var result = RouteCalculator.Calculate( DbHelper.GetCategoryByName(parcelType), weight, DbHelper.GetCityByName(fromName), DbHelper.GetCityByName(toName), filter == 1 ); //var result = RouteCalculator.CalculateInternalRoute( // DbHelper.GetCategoryByName(parcelType), // weight, // fromName, // toName, // filter == 1 //); return(Json(new { valid = "true", duration = result.Duration.ToString(), price = result.Price.ToString(), fromName = fromName, toName = toName })); } catch (InvalidDataException e) { Console.Write(e.Message); throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The route could not be calculated")); } }
public IHttpActionResult RetrievePlaceData([FromBody] ClientRouteCalculatorRequest rcr) { try { List <Route> resultList = RouteCalculator.Calculate(rcr); return(Ok(resultList)); } catch (Exception ex) { return(Content(HttpStatusCode.BadRequest, ex)); //return BadRequest(ex.Message); } }