Example #1
0
func HomeHandler(c *gin.Context) {
	// "SELECT * FROM posts ORDER BY id DESC LIMIT %s", sp
	// "SELECT count(*) FROM posts"
	var posts []Post
	DB.Order("pid desc").Limit(sp).Find(&posts)

	if len(posts) == 0 {
		c.Redirect(http.StatusFound, "/user/compose")
		return
	}
	var countrows int
	DB.Model(&Post{}).Count(&countrows)

	var sumpage int
	if countrows%sp == 0 {
		sumpage = countrows / sp
	} else {
		sumpage = countrows/sp + 1
	}
	//title and tag
	titles := component.GetTitle()
	tags := component.GetTag()
	c.HTML(http.StatusOK, "home.tmpl", gin.H{
		"home":    "welcome home",
		"posts":   posts,
		"sumpage": sumpage,
		"onpage":  1,
		"current": c.MustGet("current").(bool),
		"titles":  titles,
		"tags":    tags,
	})
}
Example #2
0
func GetAllHandler(c *gin.Context) {

	var posts []Post
	DB.Select("title,slug,published").Order("pid desc").Find(&posts)
	titles := component.GetTitle()
	tags := component.GetTag()

	c.HTML(http.StatusOK, "posts.html", gin.H{
		"posts":  posts,
		"titles": titles,
		"tags":   tags,
	})
}
Example #3
0
func GetOneHandler(c *gin.Context) {
	slug := c.Param("slug")
	if slug == "" {
		c.Redirect(http.StatusMovedPermanently, "/")
	}
	var posts []Post
	DB.Where("slug = ?", slug).Find(&posts)
	titles := component.GetTitle()
	tags := component.GetTag()
	c.HTML(http.StatusOK, "posts.html", gin.H{
		"posts":  posts,
		"titles": titles,
		"tags":   tags,
	})

}