func (lc LocationController) UpdateLocation(w http.ResponseWriter, r *http.Request) { params := r.URL.Query() uid := params.Get(":location_id") oid := bson.ObjectIdHex(uid) loc := models.Location{} oldloc := models.Location{} decoder := json.NewDecoder(r.Body) err := decoder.Decode(&loc) if err != nil { fmt.Errorf("Error in decoding the Input JSON: %v", err) } err = lc.session.DB("test_mongo_db").C("test").FindId(oid).One(&oldloc) if err != nil { fmt.Printf("got an error finding a doc %v\n") } var address string address = loc.Address + loc.City + loc.State + loc.Zip lat, lng := GetCoordinates(address) loc.Coordinate.Lat = lat loc.Coordinate.Lng = lng loc.ID = oid if loc.Name == "" { loc.Name = oldloc.Name } err = lc.session.DB("test_mongo_db").C("test").UpdateId(oid, loc) if err != nil { fmt.Printf("got an error updating the doc %v\n") } err = lc.session.DB("test_mongo_db").C("test").FindId(oid).One(&loc) if err != nil { fmt.Printf("got an error finding a doc %v\n") } w.Header().Set("Content-Type", "application/json; charset=UTF-8") json.NewEncoder(w).Encode(loc) }
func (lc LocationController) AddLocation(w http.ResponseWriter, r *http.Request) { loc := models.Location{} decoder := json.NewDecoder(r.Body) err := decoder.Decode(&loc) if err != nil { fmt.Errorf("Error in decoding the Input JSON: %v", err) } var address string address = loc.Address + loc.City + loc.State + loc.Zip lat, lng := GetCoordinates(address) loc.ID = bson.NewObjectId() loc.Coordinate.Lat = lat loc.Coordinate.Lng = lng lc.session.DB("test_mongo_db").C("test").Insert(loc) w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusCreated) json.NewEncoder(w).Encode(loc) }