Exemple #1
0
func GetUserHandler(w http.ResponseWriter, r *http.Request) {
	id, err := bsonutils.ObjectId(mux.Vars(r)["id"])

	if err != nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	var user model.User

	if err := ctx.M(r).DB("").C("users").FindId(id).One(&user); err != nil {
		if err == mgo.ErrNotFound {
			w.WriteHeader(http.StatusNotFound)
			return
		}

		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	httpres.Json(w, http.StatusOK, userResponse{User: user})
}
Exemple #2
0
func GetUserHandler(w http.ResponseWriter, r *http.Request) {
	id, ok := bsonutils.ObjectId(mux.Vars(r)["id"])

	if !ok {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	var user model.User

	s := conf.M.Copy()
	defer s.Close()

	if err := s.DB("").C("users").FindId(id).One(&user); err != nil {
		if err == mgo.ErrNotFound {
			w.WriteHeader(http.StatusNotFound)
			return
		}
		log.Println(err)
		return
	}

	httpres.Json(w, http.StatusOK, UserResponse{user})
}