Exemple #1
0
func (c *appContext) tasksHandler(w http.ResponseWriter, r *http.Request) {
	repo := models.TaskRepo{c.db.C("tasks")}
	tasks, err := repo.All()
	if err != nil {
		panic(err)
	}

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

	w.WriteHeader(204)
	w.Write([]byte("\n"))
}
Exemple #3
0
func (c *appContext) taskHandler(w http.ResponseWriter, r *http.Request) {
	params := context.Get(r, "params").(httprouter.Params)
	repo := models.TaskRepo{c.db.C("tasks")}
	task, err := repo.Find(params.ByName("id"))
	if err != nil {
		panic(err)
	}

	w.Header().Set("Content-Type", "application/vnd.api+json")
	json.NewEncoder(w).Encode(task)
}
Exemple #4
0
func (c *appContext) createtaskHandler(w http.ResponseWriter, r *http.Request) {
	body := context.Get(r, "body").(*models.TaskResource)
	repo := models.TaskRepo{c.db.C("tasks")}
	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)
}
Exemple #5
0
func (c *appContext) updatetaskHandler(w http.ResponseWriter, r *http.Request) {
	params := context.Get(r, "params").(httprouter.Params)
	body := context.Get(r, "body").(*models.TaskResource)
	body.Data.Id = bson.ObjectIdHex(params.ByName("id"))
	repo := models.TaskRepo{c.db.C("tasks")}
	err := repo.Update(&body.Data)
	if err != nil {
		panic(err)
	}

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