func UpdateLocation(w http.ResponseWriter, r *http.Request, p httprouter.Params) { id := p.ByName("id") if !bson.IsObjectIdHex(id) { w.WriteHeader(404) return } var locationInfo structure.Request json.NewDecoder(r.Body).Decode(&locationInfo) response := structure.Response{} response.Id = bson.ObjectIdHex(id) response.Name = locationInfo.Name response.Address = locationInfo.Address response.City = locationInfo.City response.State = locationInfo.State response.Zip = locationInfo.Zip requestURL := createURL(response.Address, response.City, response.State) getLocation(&response, requestURL) if err := session.DB("cmpe273_project").C("hello").Update(bson.M{"_id": response.Id}, bson.M{"$set": bson.M{"address": response.Address, "city": response.City, "state": response.State, "zip": response.Zip, "coordinate.lat": response.Coordinate.Lat, "coordinate.lng": response.Coordinate.Lng}}); err != nil { w.WriteHeader(404) return } if err := session.DB("cmpe273_project").C("hello").FindId(response.Id).One(&response); err != nil { w.WriteHeader(404) return } location, _ := json.Marshal(response) w.Header().Set("Content-Type", "application/json") w.WriteHeader(201) fmt.Fprintf(w, "%s", location) }
func CreateLocation(rw http.ResponseWriter, req *http.Request, p httprouter.Params) { var locationInfo structure.Request json.NewDecoder(req.Body).Decode(&locationInfo) response := structure.Response{} response.Id = bson.NewObjectId() response.Name = locationInfo.Name response.Address = locationInfo.Address response.City = locationInfo.City response.State = locationInfo.State response.Zip = locationInfo.Zip requestURL := createURL(response.Address, response.City, response.State) getLocation(&response, requestURL) session.DB("cmpe273_project").C("hello").Insert(response) location, _ := json.Marshal(response) rw.Header().Set("Content-Type", "application/json") rw.WriteHeader(201) fmt.Fprintf(rw, "%s", location) }