Exemple #1
0
func updateTodo(rend render.Render, req *http.Request, params martini.Params, dbh *db.DBHandle) {
	todo_id, err := strconv.ParseInt(params["id"], 10, 64)
	if err != nil {
		rend.JSON(500, err.Error())
		return
	}

	err = req.ParseForm()
	if err != nil {
		rend.JSON(500, err.Error())
		return
	}

	u := unmarshalTodoJSONContainer{}
	err = json.NewDecoder(req.Body).Decode(&u)
	if err != nil {
		rend.JSON(500, err.Error())
		return
	}

	dbtodo := db.Todo{Id: todo_id}
	err = dbh.ORM.Read(&dbtodo)
	if err != nil {
		if err == orm.ErrNoRows {
			rend.JSON(404, fmt.Sprintf("No Todo with id %d found.", todo_id))
			return
		} else {
			rend.JSON(500, err.Error())
			return
		}
	}
	if u.Todo.Text != "" {
		dbtodo.Text = u.Todo.Text
	}
	if u.Todo.Category != "" {
		dbtodo.Category = u.Todo.Category
	}
	dbh.ORM.Update(&dbtodo)
	rend.JSON(200, todoWithPersonIdJSON{&dbtodo, dbtodo.Person.Id})
}