func SearchByIdHandler(w http.ResponseWriter, r *http.Request) { sessionCopy := database.GetSession() defer sessionCopy.Close() i64, _ := strconv.ParseInt(GetMyKey(r), 10, 64) i := int32(i64) fmt.Println(i) fmt.Println("Gorilla context Key " + GetMyKey(r)) user := User{} err := sessionCopy.DB("MyDB").C("MyCollection").Find(bson.M{"id": i}).One(&user) if err != nil { fmt.Println("no user with that id") data, error := json.Marshal("error no user can be found with an id of " + GetMyKey(r)) checkErrorAndPanic(error) w.Header().Set("Content-Type", "application/json") w.Write(data) } else { fmt.Println("User", user) data, error := json.Marshal(user) checkErrorAndPanic(error) w.Header().Set("Content-Type", "application/json") w.Write(data) } }
func (uc UserController) UserCreate(w http.ResponseWriter, r *http.Request) { sessionCopy := database.GetSession() defer sessionCopy.Close() //not working through schema /* var user User err := r.ParseForm() if err != nil { log.Println("Form could not be parsed") } err2 := decoder.Decode(&user, r.PostForm) if err2 != nil { log.Println("Got error decoding form: ", err2) } fmt.Println("User:"******"gorilla context not available here outside of request key empty" + i) body, err := ioutil.ReadAll(r.Body) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) } log.Println(string(body)) var u User err = json.Unmarshal([]byte(string(body)), &u) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) } log.Println("Name" + u.Name) users := sessionCopy.DB("MyDB").C("MyCollection") err = users.Insert(User{ID: u.ID, Name: u.Name, Email: u.Email}) if err != nil { fmt.Println(err) http.Error(w, err.Error(), http.StatusBadRequest) } w.WriteHeader(http.StatusCreated) fmt.Println("created") }
//expose REST API func (uc UserController) GetUsersHandler(w http.ResponseWriter, r *http.Request) { sessionCopy := database.GetSession() defer sessionCopy.Close() users := sessionCopy.DB("MyDB").C("MyCollection") var results []User err := users.Find(bson.M{}).All(&results) if err != nil { panic(err) } for _, res := range results { fmt.Printf("User: %s|%s\n", res.Name, res.Email) } data, error := json.Marshal(results) checkErrorAndPanic(error) w.Header().Set("Content-Type", "application/json") w.Write(data) }