Beispiel #1
0
func (c *appContext) usersHandler(w http.ResponseWriter, r *http.Request) {
	repo := models.UserRepo{c.db.C("users")}
	users, err := repo.All()
	if err != nil {
		panic(err)
	}

	w.Header().Set("Content-Type", "application/vnd.api+json")
	json.NewEncoder(w).Encode(users)
}
Beispiel #2
0
func (c *appContext) userHandler(w http.ResponseWriter, r *http.Request) {
	params := context.Get(r, "params").(httprouter.Params)
	repo := models.UserRepo{c.db.C("users")}
	user, err := repo.Find(params.ByName("id"))
	if err != nil {
		panic(err)
	}

	w.Header().Set("Content-Type", "application/vnd.api+json")
	json.NewEncoder(w).Encode(user)
}
Beispiel #3
0
func (c *appContext) deleteuserHandler(w http.ResponseWriter, r *http.Request) {
	params := context.Get(r, "params").(httprouter.Params)
	repo := models.UserRepo{c.db.C("users")}
	err := repo.Delete(params.ByName("id"))
	if err != nil {
		panic(err)
	}

	w.WriteHeader(204)
	w.Write([]byte("\n"))
}
Beispiel #4
0
func (c *appContext) createuserHandler(w http.ResponseWriter, r *http.Request) {
	body := context.Get(r, "body").(*models.UserResource)
	repo := models.UserRepo{c.db.C("users")}
	err := repo.Create(&body.Data)
	if err != nil {
		panic(err)
	}

	w.Header().Set("Content-Type", "application/vnd.api+json")
	w.WriteHeader(201)
	json.NewEncoder(w).Encode(body)
}
Beispiel #5
0
func (c *appContext) updateuserHandler(w http.ResponseWriter, r *http.Request) {
	params := context.Get(r, "params").(httprouter.Params)
	body := context.Get(r, "body").(*models.UserResource)
	body.Data.Id = bson.ObjectIdHex(params.ByName("id"))
	repo := models.UserRepo{c.db.C("users")}
	err := repo.Update(&body.Data)
	if err != nil {
		panic(err)
	}

	w.WriteHeader(204)
	w.Write([]byte("\n"))
}