Exemple #1
0
// 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)
}
Exemple #2
0
// 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)
}