示例#1
0
文件: post.go 项目: shwing/moledro
// PostIndex shows all post in a given sub
func PostIndex(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 0, 64)
	sub := models.Sub{}
	if err != nil {
		// if it is not an id, maybe it is a slug
		slug := models.Slugify(c.Param("id"))
		sub, err = models.GetSubBySlug(slug)
		if err != nil {
			c.JSON(http.StatusNotFound, gin.H{"status": "not found"})
			return
		}
	} else {
		sub, err = models.GetSubByID(id)
		if err != nil {
			c.JSON(http.StatusNotFound, gin.H{"status": "not found"})
			return
		}
	}

	posts, err := models.GetPosts(sub.ID)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"status": "bad request"})
		return
	}
	c.JSON(http.StatusOK, posts)
}
示例#2
0
文件: sub.go 项目: shwing/moledro
// SubShow shows a sub by id
func SubShow(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 0, 64)
	sub := models.Sub{}
	if err != nil {
		// if it is not an id, maybe it is a slug
		slug := models.Slugify(c.Param("id"))
		sub, err = models.GetSubBySlug(slug)
		if err != nil {
			c.JSON(http.StatusNotFound, gin.H{"status": "not found"})
			return
		}
	} else {
		sub, err = models.GetSubByID(id)
		if err != nil {
			c.JSON(http.StatusNotFound, gin.H{"status": "not found"})
			return
		}
	}

	c.JSON(http.StatusOK, sub)
}