Beispiel #1
0
func (i *ItemsAPI) post(w http.ResponseWriter, r *http.Request) {
	var payload = &ItemGetResponse{}
	if err := json.NewDecoder(r.Body).Decode(payload); err != nil {
		http.Error(w, fmt.Sprintf("Error encoding JSON (%s)", err), 500)
		return
	}

	postItem := payload.TodoItem
	item := &model.TodoItem{
		Title:       postItem.Title,
		Description: postItem.Description,
		Done:        postItem.Done,
		DoneAt:      postItem.DoneAt,
		Group:       postItem.Group,
	}

	if err := i.Items.Insert(item); err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	util.WriteJSONResponse(w, &ItemGetResponse{
		TodoItem: item,
	})
}
Beispiel #2
0
func (i *ItemsAPI) list(w http.ResponseWriter, r *http.Request) {
	if items, err := i.Items.GetAll(); err != nil {
		http.Error(w, err.Error(), 500)
	} else {
		util.WriteJSONResponse(w, &ItemListResponse{
			TodoItems: items,
		})
	}
}
Beispiel #3
0
func (l *ListsAPI) list(w http.ResponseWriter, r *http.Request) {
	if lists, err := l.Lists.GetAll(); err != nil {
		http.Error(w, err.Error(), 500)
	} else {
		util.WriteJSONResponse(w, &ListListResponse{
			TodoLists: lists,
		})
	}
}
Beispiel #4
0
func (i *ItemsAPI) get(w http.ResponseWriter, r *http.Request) {
	id, err := strconv.ParseInt(mux.Vars(r)["id"], 10, 64)
	if err != nil {
		http.Error(w, err.Error(), 500)
	}

	if item, err := i.Items.Find(id); err != nil {
		http.Error(w, err.Error(), 500)
	} else if item == nil {
		http.Error(w, "Item not found", 404)
	} else {
		util.WriteJSONResponse(w, &ItemGetResponse{
			TodoItem: item,
		})
	}
}
Beispiel #5
0
func (l *ListsAPI) get(w http.ResponseWriter, r *http.Request) {
	id, err := strconv.ParseInt(mux.Vars(r)["id"], 10, 64)
	if err != nil {
		http.Error(w, err.Error(), 500)
	}

	if list, err := l.Lists.Find(id); err != nil {
		http.Error(w, err.Error(), 500)
	} else if list == nil {
		http.Error(w, "List not found", 404)
	} else {
		util.WriteJSONResponse(w, &ListGetResponse{
			TodoList: list,
		})
	}
}
Beispiel #6
0
func (i *ItemsAPI) put(w http.ResponseWriter, r *http.Request) {
	id, err := strconv.ParseInt(mux.Vars(r)["id"], 10, 64)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	item, err := i.Items.Find(id)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	if item == nil {
		http.Error(w, "Item not found", 404)
		return
	}

	var payload = &ItemGetResponse{}
	if err := json.NewDecoder(r.Body).Decode(payload); err != nil {
		http.Error(w, fmt.Sprintf("Error encoding JSON (%s)", err), 500)
		return
	}

	item.Title = payload.TodoItem.Title
	item.Description = payload.TodoItem.Description
	item.Done = payload.TodoItem.Done
	item.DoneAt = payload.TodoItem.DoneAt
	item.Group = payload.TodoItem.Group

	if err := i.Items.Update(item); err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	util.WriteJSONResponse(w, &ItemGetResponse{
		TodoItem: item,
	})
}
Beispiel #7
0
func (l *ListsAPI) post(w http.ResponseWriter, r *http.Request) {
	var payload = &ListGetResponse{}
	if err := json.NewDecoder(r.Body).Decode(payload); err != nil {
		http.Error(w, fmt.Sprintf("Error encoding JSON (%s)", err), 500)
		return
	}

	postList := payload.TodoList
	list := &model.TodoList{
		Title:       postList.Title,
		Description: postList.Description,
	}

	if err := l.Lists.Insert(list); err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	util.WriteJSONResponse(w, &ListGetResponse{
		TodoList: list,
	})
}
Beispiel #8
0
func (l *ListsAPI) put(w http.ResponseWriter, r *http.Request) {
	id, err := strconv.ParseInt(mux.Vars(r)["id"], 10, 64)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	list, err := l.Lists.Find(id)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	if list == nil {
		http.Error(w, "List not found", 404)
		return
	}

	var payload = &ListGetResponse{}
	if err := json.NewDecoder(r.Body).Decode(payload); err != nil {
		http.Error(w, fmt.Sprintf("Error encoding JSON (%s)", err), 500)
		return
	}

	list.Title = payload.TodoList.Title
	list.Description = payload.TodoList.Description

	if err := l.Lists.Update(list); err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	util.WriteJSONResponse(w, &ListGetResponse{
		TodoList: list,
	})
}