//UpdateLocation updates an existing location resource func (uc LocationController) UpdateLocation(w http.ResponseWriter, r *http.Request, p httprouter.Params) { var i InputAddress var o OutputAddress id := p.ByName("location_id") // fmt.Println(id) if !bson.IsObjectIdHex(id) { w.WriteHeader(404) return } oid := bson.ObjectIdHex(id) if err := uc.session.DB("go_273").C("Locations").FindId(oid).One(&o); err != nil { w.WriteHeader(404) return } json.NewDecoder(r.Body).Decode(&i) //Trying to get the lat lang!!!-------------------- googResCoor := getGoogLocation(i.Address + "+" + i.City + "+" + i.State + "+" + i.Zip) fmt.Println("resp is: ", googResCoor.Coordinate.Lat, googResCoor.Coordinate.Lang) o.Address = i.Address o.City = i.City o.State = i.State o.Zip = i.Zip o.Coordinate.Lat = googResCoor.Coordinate.Lat o.Coordinate.Lang = googResCoor.Coordinate.Lang // Write the user to mongo c := uc.session.DB("go_273").C("Locations") id2 := bson.M{"_id": oid} err := c.Update(id2, o) if err != nil { panic(err) } // Marshal provided interface into JSON structure uj, _ := json.Marshal(o) // Write content-type, statuscode, payload w.Header().Set("Content-Type", "application/json") w.WriteHeader(201) fmt.Fprintf(w, "%s", uj) }
// remove an existing location resource func (uc LocationController) RemoveLocation(w http.ResponseWriter, r *http.Request, p httprouter.Params) { id := p.ByName("location_id") if !bson.IsObjectIdHex(id) { w.WriteHeader(404) return } oid := bson.ObjectIdHex(id) // Remove user if err := uc.session.DB("go_273").C("Locations").RemoveId(oid); err != nil { w.WriteHeader(404) return } // Write status w.WriteHeader(200) }
// GetLocation retrieves one location resource func (uc LocationController) GetLocation(w http.ResponseWriter, r *http.Request, p httprouter.Params) { id := p.ByName("location_id") if !bson.IsObjectIdHex(id) { w.WriteHeader(404) return } oid := bson.ObjectIdHex(id) var o OutputAddress if err := uc.session.DB("go_273").C("Locations").FindId(oid).One(&o); err != nil { w.WriteHeader(404) return } // Marshal into JSON structure uj, _ := json.Marshal(o) w.Header().Set("Content-Type", "application/json") w.WriteHeader(200) fmt.Fprintf(w, "%s", uj) }