예제 #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)
}
예제 #3
0
func IndexTodos(c *gin.Context) {
	todos, err := models.FindTodos(app.GetDB(c))
	if err != nil {
		utils.AbortWithPublicError(c, http.StatusInternalServerError, err, "Couldn't find todos")
		return
	}

	c.JSON(http.StatusOK, todos)
}
예제 #4
0
func CreateTodo(c *gin.Context) {
	var todo models.Todo

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

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

	c.JSON(http.StatusCreated, todo)
}