Exemplo n.º 1
0
Arquivo: todo.go Projeto: dthomas/tudu
func serveTodoUpdate(w http.ResponseWriter, r *http.Request) *AppResponse {
	todo := new(datastore.Todo)
	err := json.NewDecoder(r.Body).Decode(&AppRequest{todo})
	if err != nil {
		return &AppResponse{"Bad Request", http.StatusBadRequest, todo, nil}
	}

	id, err := strconv.Atoi(mux.Vars(r)["ID"])
	if err != nil {
		return &AppResponse{"Invalid ID", http.StatusBadRequest, nil, nil} // Send 400 Bad Request
	}

	todo.ID = id

	errors, success := todo.Validate()

	if success != true {
		return &AppResponse{"Unprocessable Entity", 422, todo, errors}
	}

	err = todo.Update()
	if err != nil {
		return &AppResponse{err.Error(), 422, todo, nil}
	}

	resp := &AppResponse{"Success", http.StatusOK, todo, nil}
	return resp
}
Exemplo n.º 2
0
Arquivo: todo.go Projeto: dthomas/tudu
func serveTodoCreate(w http.ResponseWriter, r *http.Request) *AppResponse {
	todo := new(datastore.Todo)
	err := json.NewDecoder(r.Body).Decode(&AppRequest{todo})
	if err != nil {
		return &AppResponse{"Bad Request", http.StatusBadRequest, nil, nil}
	}

	errors, success := todo.Validate()

	if success != true {
		return &AppResponse{"Unprocessable Entity", 422, todo, errors}
	}

	err = todo.Save()

	if err != nil {
		return &AppResponse{err.Error(), 422, nil, nil}
	}

	resp := &AppResponse{"Success", http.StatusCreated, todo, nil}
	return resp
}