public async Task<IHttpActionResult> PutPointOfInterest(int id, PointOfInterest pointOfInterest) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != pointOfInterest.PointOfInterestId) { return BadRequest(); } db.Entry(pointOfInterest).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PointOfInterestExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public async Task<IHttpActionResult> PostPointOfInterest(PointOfInterest pointOfInterest) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.PointOfInterests.Add(pointOfInterest); await db.SaveChangesAsync(); return CreatedAtRoute("DefaultApi", new { id = pointOfInterest.PointOfInterestId }, pointOfInterest); }
// !!!! HELPER FUNCTIONS !!!! public List<PointOfInterest> getPlaces(Location start, int timeInMinutes) { //create query //build ("https://maps.googleapis.com/maps/api/place/nearbysearch/output?" + parameters) //OR (less data) string URI = "https://maps.googleapis.com/maps/api/place/radarsearch/json?"; //add parameters //key URI += "key=" + (new Secrets()).GoogleAPIServerKey + "&"; //location URI += "location=" + start.latitude.ToString() + "," + start.longitude.ToString() + "&"; //radius; estimate 1 meter per second walking speed URI += "radius=" + (timeInMinutes * 60 / 2).ToString() + "&"; //types; start with "park" and possibly add more later //see https://developers.google.com/places/supported_types for list of types URI += "types=park"; //create a new, empty list of Points Of Interest List<PointOfInterest> candidatePoIs = new List<PointOfInterest>(); //request processing var googleRadarObject = JToken.Parse(callAPIgetJSon(URI)); var status = googleRadarObject.Children<JProperty>().FirstOrDefault(x => x.Name == "status").Value; if (status.ToString() == "OK") { var resultsArray = googleRadarObject.Children<JProperty>().FirstOrDefault(x => x.Name == "results").Value; //iterate over the json and parse into PointsOfInterest, placing each in the list foreach (var item in resultsArray) { //extract the google place id string GooglePlaceId = item.Children<JProperty>().FirstOrDefault(x => x.Name == "place_id").Value.ToString(); //extract the lattitude from JSon structuret decimal lat = decimal.Parse( item.Children<JProperty>().FirstOrDefault(x => x.Name == "geometry").Value. Children<JProperty>().FirstOrDefault(x => x.Name == "location").Value. Children<JProperty>().FirstOrDefault(x => x.Name == "lat").Value.ToString()); //extract the longitude from JSon structure decimal lng = decimal.Parse( item.Children<JProperty>().FirstOrDefault(x => x.Name == "geometry").Value. Children<JProperty>().FirstOrDefault(x => x.Name == "location").Value. Children<JProperty>().FirstOrDefault(x => x.Name == "lng").Value.ToString()); //create new PoI with above properties PointOfInterest point = new PointOfInterest(lat, lng, GooglePlaceId); //add it to the list of candidates candidatePoIs.Add(point); } } //return processed list return candidatePoIs; }