示例#1
0
func (tr *TodoResource) PatchTodo(c *gin.Context) {
	var json []api.Patch
	if c.Bind(&json) != nil {
		c.JSON(400, api.NewError("problem decoding body"))
		return
	}

	id, err := tr.getId(c)
	if err != nil {
		c.JSON(400, api.NewError("problem decoding id sent"))
		return
	}
	var todo api.Todo

	if tr.db.First(&todo, id).RecordNotFound() {
		c.JSON(404, api.NewError("not found"))
	} else {
		todo.Status = json[0].Value

		tr.db.Save(&todo)
		c.JSON(200, todo)
	}

	c.Bind(&json)
}
示例#2
0
func (tr *TodoResource) CreateTodo(c *gin.Context) {
	var todo api.Todo
	if c.Bind(&todo) != nil {
		c.JSON(400, api.NewError("problem decoding body"))
		return
	}
	todo.Status = api.TodoStatus
	todo.Created = int32(time.Now().Unix())

	tr.db.Save(&todo)

	c.JSON(201, todo)
}
示例#3
0
func (tr *TodoResource) UpdateTodo(c *gin.Context) {
	id, err := tr.getId(c)
	if err != nil {
		c.JSON(400, api.NewError("problem decoding id sent"))
		return
	}
	var todo api.Todo

	if c.Bind(&todo) != nil {
		c.JSON(400, api.NewError("problem decoding body"))
		return
	}
	todo.Id = int32(id)
	var existing api.Todo
	if tr.db.First(&existing, id).RecordNotFound() {
		c.JSON(404, api.NewError("not found"))
	} else {
		tr.db.Save(&todo)
		c.JSON(200, todo)
	}

}