示例#1
0
文件: user.go 项目: oeeckhoutte/stack
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})
}
示例#2
0
文件: user.go 项目: icobani/stack
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})
}