// Update removes an existing user resource func (uc UserController) UpdateLocations(w http.ResponseWriter, r *http.Request, p httprouter.Params) { // Stub an example user id := p.ByName("id") // Verify id is ObjectId, otherwise bail if !bson.IsObjectIdHex(id) { w.WriteHeader(404) return } // Grab id oid := bson.ObjectIdHex(id) l := models.Location{} // Populate the user data json.NewDecoder(r.Body).Decode(&l) str := getURL(l.Address, l.City, l.State) getLocation(&l, str) l.Id = oid // Write the user to mongo if err := uc.session.DB("cmpe273").C("assignment2").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").C("assignment2").FindId(oid).One(&l); err != nil { w.WriteHeader(404) return } // Marshal provided interface into JSON structure lj, _ := json.Marshal(l) // Write content-type, statuscode, payload w.Header().Set("Content-Type", "application/json") w.WriteHeader(201) fmt.Fprintf(w, "%s", lj) }
// CreateUser creates a new user resource func (uc UserController) CreateLocations(w http.ResponseWriter, r *http.Request, p httprouter.Params) { // Stub an user to be populated from the body l := models.Location{} // Populate the user data json.NewDecoder(r.Body).Decode(&l) str := getURL(l.Address, l.City, l.State) getLocation(&l, str) // Add an Id l.Id = bson.NewObjectId() // Write the user to mongo uc.session.DB("cmpe273").C("assignment2").Insert(l) // Marshal provided interface into JSON structure lj, _ := json.Marshal(l) // Write content-type, statuscode, payload w.Header().Set("Content-Type", "application/json") w.WriteHeader(201) fmt.Fprintf(w, "%s", lj) }