public RouteHeader(Route route) { RouteId = route.RouteId; UserId = route.UserId; IsOffer = route.IsOffer; Name = route.Name; }
public ActionResult CreateUpdate(int id = 0) { Route route = db.Route.Find(id); //Retrieve route if (route == null) route = new Route(); //Create a new route if none retrieved IEnumerable<RouteWayPoint> routeWayPoint = from rwp in db.RouteWayPoint where rwp.RouteId == id select rwp; RouteView routeView = new RouteView(route); //, routeWayPoint, Json(routeWayPoint)); return View(routeView); }
public ActionResult Create(Route route) { if (ModelState.IsValid) { db.Route.Add(route); db.SaveChanges(); return RedirectToAction("Index"); } return View(route); }
/// Search matching route for the specified route and send mail to users /// Search database for matching route /// Send mail to the user (skip route collected from covoiturage.fr) private void searchMatchingRoute(Route route) { IEnumerable<RouteSearch> routeMatchList = searchMatchingRoute(route.RouteId); //Send mail to user for all matches found foreach ( RouteSearch matchRoute in routeMatchList ) { if ( matchRoute.Id > 999 ) //Send mail only if this is not a route from covoiturage.fr GetSendMatchingRouteMail(route.RouteId, matchRoute.Id); } }
/// Use the route provided to create new routes using data from covoiturage.fr /// Convert the route start/end lat/lng to city /// Query covoiturage.fr with this start.end city and take first non club route /// Save this route for UserId=0 private void getRouteFromCovoiturageFr(Route route) { CityGeoCode startCity = getLocalityFromGoogle(route.StartLatitude ?? 0, route.StartLongitude ?? 0); CityGeoCode endCity = getLocalityFromGoogle(route.EndLatitude ?? 0, route.EndLongitude ?? 0); //We got some city as start/end String routeLink = ""; if (startCity != null && endCity != null) routeLink = getLinkFromCovoiturageFr(startCity.city, endCity.city); if (routeLink != "") //Create a new covoiturage route { try { //Get last ID var userRoute = from r in db.Route where r.UserId == 0 select r; int id; if (userRoute.Any()) id = userRoute.Max(r => r.RouteId); else id = 0; if (id == 999) id = 0; //Currently we are limited to 999 route per user id += 1; //increment RouteId //Set new route data Route newRoute = new Route(); newRoute.RouteId = id; newRoute.Name = startCity.city + " - " + endCity.city; newRoute.IsOffer = true; newRoute.UserId = 0; newRoute.StartLatitude = startCity.lat; //Later should the city lat newRoute.StartLongitude = startCity.lng; newRoute.EndLatitude = endCity.lat; newRoute.EndLongitude = endCity.lng; newRoute.CovoiturageLink = routeLink; newRoute.Distance = 0; //Need to get a Google route to know it //Add the route in dabase db.Route.Add(newRoute); db.SaveChanges(); } catch (Exception e) { } } }
/// PUT api/ApiRoute/5 - Not used currently public HttpResponseMessage PutRoute(int id, Route route) { if (ModelState.IsValid && id == route.RouteId) { db.Entry(route).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return Request.CreateResponse(HttpStatusCode.NotFound); } return Request.CreateResponse(HttpStatusCode.OK); } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }
// POST api/ApiRoute /// Create or update route: local db -> central db /// If the route is not found in database it will be created, otherwise it is updated public HttpResponseMessage PostRoute(Route route) { if (ModelState.IsValid) { Route dbRoute = ( from r in db.Route where r.RouteId == route.RouteId select r).FirstOrDefault(); if (dbRoute == null) { db.Route.Add(route); getRouteFromCovoiturageFr(route); searchMatchingRoute(route); } else //Update existing route { dbRoute.Name = route.Name; dbRoute.IsOffer = route.IsOffer; //We do not update distance when it is zero. Current db value might be right and new value might be wrong (issue to access Google API) if (route.Distance > 0) dbRoute.Distance = route.Distance; dbRoute.StartLatitude = route.StartLatitude; dbRoute.StartLongitude = route.StartLongitude; dbRoute.EndLatitude = route.EndLatitude; dbRoute.EndLongitude = route.EndLongitude; } db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, route); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = route.RouteId })); return response; } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }
//TMP API to call getRouteFromCovoiturageFr public void getRouteCovoiturage(decimal lat, decimal lng) { Route route = new Route(); route.StartLatitude = lat; //47.90320650 route.StartLongitude = lng; //1.90921890 route.EndLatitude = 50.629250M; //Lille route.EndLongitude = 3.0572560M; getRouteFromCovoiturageFr(route); }
public int Update(int id, bool isOffer, string name, int distance) { Route route = null; if (id == 0) //Create new route { route = new Route(); //Retrieve next RouteId //var maxId = from r in db.Route select new { maxId = r.Max(s => s.RouteId) }; var maxId = (from r in db.Route where r.UserId == userId select (int?)r.RouteId).Max(); route.RouteId = ( maxId ?? userId * 1000 ) + 1; } else route = db.Route.Find(id); //Retrieve route route.UserId = userId; route.Name = name; route.IsOffer = isOffer; route.Distance = distance; if (id == 0) db.Route.Add(route); //db.SaveChanges(); try { db.SaveChanges(); } catch (OptimisticConcurrencyException) { //db.Refresh(RefreshMode.ClientWins, db.Route); db.SaveChanges(); } return route.RouteId; }
public ActionResult Edit(Route route) { if (ModelState.IsValid) { db.Entry(route).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(route); }
//Create or update route public ActionResult CreateUpdateOld(int id = 0) { //Simple route with 2 points //Route route = db.Route.Find(id); //if (route == null ) route = new Route(); //return View(route); Route route = db.Route.Find(id); //Retrieve route if (route == null) route = new Route(); //Create a new route if none retrieved IEnumerable<RouteWayPoint> routeWayPoint = from rwp in db.RouteWayPoint where rwp.RouteId == id select rwp; JsonResult a = Json(routeWayPoint); //if (routeWayPoint == null) routeWayPoint = new RouteWayPoint(); //Create new way points if none retrieved RouteView routeView = new RouteView(route); //, routeWayPoint, Json(routeWayPoint)); return View(routeView); }
//, IEnumerable<RouteWayPoint> routeWayPoint, JsonResult jsonRoute) public RouteView(Route route) { RouteId = route.RouteId; UserId = route.UserId; IsOffer = route.IsOffer; Name = route.Name; //RouteWayPoint = routeWayPoint; //JsonRoute = jsonRoute; }