Beispiel #1
0
// ListTodos accepts a request to retrieve a list of todos.
//
//     GET /api/todos
//
func ListTodos(c web.C, w http.ResponseWriter, r *http.Request) {
	var (
		ctx    = context.FromC(c)
		limit  = ToLimit(r)
		offset = ToOffset(r)
	)

	todos, err := datastore.ListTodos(ctx, limit, offset)
	if err != nil {
		log.FromContext(ctx).WithField("err", err).Error("Error listing todos")
		w.WriteHeader(http.StatusNotFound)
		return
	}

	json.NewEncoder(w).Encode(todos)
}
Beispiel #2
0
// DeleteTodo accepts a request to delete a todo.
//
//     DELETE /api/todos/:todo
//
func DeleteTodo(c web.C, w http.ResponseWriter, r *http.Request) {
	var (
		ctx   = context.FromC(c)
		idStr = c.URLParams["todo"]
	)

	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	err = datastore.DeleteTodo(ctx, id)
	if err != nil {
		log.FromContext(ctx).WithField("err", err).Error("Error deleting todo")
		w.WriteHeader(http.StatusNotFound)
		return
	}

	w.WriteHeader(http.StatusNoContent)
}
Beispiel #3
0
// GetTodo accepts a request to retrieve information about a particular todo.
//
//     GET /api/todos/:todo
//
func GetTodo(c web.C, w http.ResponseWriter, r *http.Request) {
	var (
		ctx   = context.FromC(c)
		idStr = c.URLParams["todo"]
	)

	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	todo, err := datastore.GetTodo(ctx, id)
	if err != nil {
		log.FromContext(ctx).WithField("err", err).Error("Error getting todo")
		w.WriteHeader(http.StatusNotFound)
		return
	}

	json.NewEncoder(w).Encode(todo)
}