Example #1
0
func (c *appContext) tasksHandler(w http.ResponseWriter, r *http.Request) {
	repo := tasksModel.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)
}
Example #2
0
func (c *appContext) deletetaskHandler(w http.ResponseWriter, r *http.Request) {
	params := context.Get(r, "params").(httprouter.Params)
	repo := tasksModel.TaskRepo{c.db.C("tasks")}
	err := repo.Delete(params.ByName("id"))
	if err != nil {
		panic(err)
	}

	w.WriteHeader(204)
	w.Write([]byte("\n"))
}
Example #3
0
func (c *appContext) taskHandler(w http.ResponseWriter, r *http.Request) {
	params := context.Get(r, "params").(httprouter.Params)
	repo := tasksModel.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)
}
Example #4
0
func (c *appContext) createtaskHandler(w http.ResponseWriter, r *http.Request) {
	body := context.Get(r, "body").(*tasksModel.TaskResource)
	repo := tasksModel.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)
}
Example #5
0
func (c *appContext) updatetaskHandler(w http.ResponseWriter, r *http.Request) {
	params := context.Get(r, "params").(httprouter.Params)
	body := context.Get(r, "body").(*tasksModel.TaskResource)
	body.Data.Id = bson.ObjectIdHex(params.ByName("id"))
	repo := tasksModel.TaskRepo{c.db.C("tasks")}
	err := repo.Update(&body.Data)
	if err != nil {
		panic(err)
	}

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