Ejemplo n.º 1
0
func createTodo(rend render.Render, req *http.Request, params martini.Params, dbh *db.DBHandle) {
	err := req.ParseForm()
	if err != nil {
		rend.JSON(500, err.Error())
		return
	}
	u := unmarshalTodoJSON{}
	glog.Info("Decoded json: %+v", u)
	err = json.NewDecoder(req.Body).Decode(&u)
	if err != nil {
		rend.JSON(500, err.Error())
		return
	}

	queryParams, _ := url.ParseQuery(req.URL.RawQuery)
	dbtodo := db.Todo{
		Text:     u.Text,
		Category: u.Category,
		Date:     u.Date,
	}

	if _, ok := queryParams["addToAll"]; ok {
		glog.Info(u)
		//do something here
		err = dbh.AddTodoToAllPeople(&dbtodo)
		if err != nil {
			rend.JSON(http.StatusInternalServerError, err.Error())
			return
		}
	} else {
		p := db.Person{Id: u.PersonId}
		err = dbh.ORM.Read(&p)
		if err != nil {
			rend.JSON(500, fmt.Sprintf("Unknown Person id: %d", u.PersonId))
			return
		}

		dbtodo.Person = &p
		_, err = dbh.ORM.Insert(&dbtodo)
		if err != nil {
			rend.JSON(500, err.Error())
			return
		}
		rend.JSON(200, todoWithPersonIdJSON{&dbtodo, p.Id})
	}
}
Ejemplo n.º 2
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})
}