예제 #1
0
func UpdateTodo(c *gin.Context) {
	var todo models.Todo

	if err := c.BindJSON(&todo); err != nil {
		return
	}

	oldTodo, err := models.FindTodoById(app.GetDB(c), c.Param("id"))
	if err != nil {
		if err == mgo.ErrNotFound {
			utils.AbortWithPublicError(c, http.StatusUnauthorized, err, "You can't access to the todo")
		} else {
			utils.AbortWithPublicError(c, http.StatusInternalServerError, err, "Couldn't find the todo")
		}
		return
	}
	todo.Id = oldTodo.Id

	if err = todo.Update(app.GetDB(c)); err != nil {
		utils.AbortWithPublicError(c, http.StatusInternalServerError, err, "Couldn't update the todo")
		return
	}

	c.JSON(http.StatusOK, todo)
}
예제 #2
0
func ViewTodo(c *gin.Context) {
	todo, err := models.FindTodoById(app.GetDB(c), c.Param("id"))
	if err != nil {
		if err == mgo.ErrNotFound {
			utils.AbortWithPublicError(c, http.StatusUnauthorized, err, "You can't access to the todo")
		} else {
			utils.AbortWithPublicError(c, http.StatusInternalServerError, err, "Couldn't find the todo")
		}
		return
	}

	c.JSON(http.StatusOK, todo)
}