Esempio n. 1
0
func (uc UserController) UpdateLocations(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	id := p.ByName("id")

	if !bson.IsObjectIdHex(id) {
		w.WriteHeader(404)
		return
	}

	oid := bson.ObjectIdHex(id)
	l := structure.Location{}
	json.NewDecoder(r.Body).Decode(&l)
	str := getURL(l.Address, l.City, l.State)
	getLocation(&l, str)
	l.Id = oid
	if err := uc.session.DB("cmpe273_project").C("hello").Update(bson.M{"_id": l.Id}, bson.M{"$set": bson.M{"address": l.Address, "city": l.City, "state": l.State, "zip": l.Zip, "coordinate.lat": l.Coordinate.Lat, "coordinate.lng": l.Coordinate.Lng}}); err != nil {
		w.WriteHeader(404)
		return
	}
	if err := uc.session.DB("cmpe273_project").C("hello").FindId(oid).One(&l); err != nil {
		w.WriteHeader(404)
		return
	}
	lj, _ := json.Marshal(l)

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(201)
	fmt.Fprintf(w, "%s", lj)
}
Esempio n. 2
0
func (userCon UserController) CreateLocations(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	location := structure.Location{}

	json.NewDecoder(r.Body).Decode(&location)
	str := getURL(location.Address, location.City, location.State)
	getLocation(&location, str)

	location.Id = bson.NewObjectId()
	userCon.session.DB("cmpe273_project").C("hello").Insert(location)

	result, _ := json.Marshal(location)

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(201)
	fmt.Fprintf(w, "%s", result)
}