Skip to content

Commit

Permalink
add pager
Browse files Browse the repository at this point in the history
  • Loading branch information
wangsongyan committed Dec 5, 2018
1 parent 674eb66 commit 2792965
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 69 deletions.
3 changes: 2 additions & 1 deletion conf/conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ addr: :8090
backup_key:
dsn: wblog.db?_loc=Asia/Shanghai
#dsn: root:mysql@/wblog?charset=utf8&parseTime=True&loc=Asia/Shanghai
notify_emails:
notify_emails:
page_size: 5
64 changes: 46 additions & 18 deletions controllers/archive.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,59 @@
package controllers

import (
"net/http"
"strconv"

"math"

"github.com/gin-gonic/gin"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday"
"github.com/wangsongyan/wblog/models"
"net/http"
"strconv"
"github.com/wangsongyan/wblog/system"
)

func ArchiveGet(c *gin.Context) {
year := c.Param("year")
month := c.Param("month")
posts, err := models.ListPostByArchive(year, month)
if err == nil {
policy := bluemonday.StrictPolicy()
for _, post := range posts {
post.Tags, _ = models.ListTagByPostId(strconv.FormatUint(uint64(post.ID), 10))
post.Body = policy.Sanitize(string(blackfriday.MarkdownCommon([]byte(post.Body))))
}
c.HTML(http.StatusOK, "index/index.html", gin.H{
"posts": posts,
"tags": models.MustListTag(),
"archives": models.MustListPostArchives(),
"links": models.MustListLinks(),
})
} else {
var (
year string
month string
page string
pageIndex int
pageSize = system.GetConfiguration().PageSize
total int
err error
posts []*models.Post
policy *bluemonday.Policy
)
year = c.Param("year")
month = c.Param("month")
page = c.Query("page")
pageIndex, _ = strconv.Atoi(page)
if pageIndex <= 0 {
pageIndex = 1
}
posts, err = models.ListPostByArchive(year, month, pageIndex, pageSize)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
total, err = models.CountPostByArchive(year, month)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
policy = bluemonday.StrictPolicy()
for _, post := range posts {
post.Tags, _ = models.ListTagByPostId(strconv.FormatUint(uint64(post.ID), 10))
post.Body = policy.Sanitize(string(blackfriday.MarkdownCommon([]byte(post.Body))))
}
c.HTML(http.StatusOK, "index/index.html", gin.H{
"posts": posts,
"tags": models.MustListTag(),
"archives": models.MustListPostArchives(),
"links": models.MustListLinks(),
"pageIndex": pageIndex,
"totalPage": int(math.Ceil(float64(total) / float64(pageSize))),
})

}
2 changes: 1 addition & 1 deletion controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func CreateXMLSitemap() {
Priority: 1,
})

posts, err := models.ListPublishedPost("")
posts, err := models.ListPublishedPost("", 0, 0)
if err == nil {
for _, post := range posts {
items = append(items, sitemap.Item{
Expand Down
62 changes: 44 additions & 18 deletions controllers/index.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,59 @@
package controllers

import (
"net/http"
"strconv"

"math"

"github.com/gin-gonic/gin"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday"
"github.com/wangsongyan/wblog/models"
"net/http"
"strconv"
"github.com/wangsongyan/wblog/system"
)

func IndexGet(c *gin.Context) {
posts, err := models.ListPublishedPost("")
if err == nil {
policy := bluemonday.StrictPolicy()
for _, post := range posts {
post.Tags, _ = models.ListTagByPostId(strconv.FormatUint(uint64(post.ID), 10))
post.Body = policy.Sanitize(string(blackfriday.MarkdownCommon([]byte(post.Body))))
}
user, _ := c.Get(CONTEXT_USER_KEY)
c.HTML(http.StatusOK, "index/index.html", gin.H{
"posts": posts,
"tags": models.MustListTag(),
"archives": models.MustListPostArchives(),
"links": models.MustListLinks(),
"user": user,
})
} else {
var (
pageIndex int
pageSize = system.GetConfiguration().PageSize
total int
page string
err error
posts []*models.Post
policy *bluemonday.Policy
)
page = c.Query("page")
pageIndex, _ = strconv.Atoi(page)
if pageIndex <= 0 {
pageIndex = 1
}
posts, err = models.ListPublishedPost("", pageIndex, pageSize)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
total, err = models.CountPostByTag("")
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
policy = bluemonday.StrictPolicy()
for _, post := range posts {
post.Tags, _ = models.ListTagByPostId(strconv.FormatUint(uint64(post.ID), 10))
post.Body = policy.Sanitize(string(blackfriday.MarkdownCommon([]byte(post.Body))))
}
user, _ := c.Get(CONTEXT_USER_KEY)
c.HTML(http.StatusOK, "index/index.html", gin.H{
"posts": posts,
"tags": models.MustListTag(),
"archives": models.MustListPostArchives(),
"links": models.MustListLinks(),
"user": user,
"pageIndex": pageIndex,
"totalPage": int(math.Ceil(float64(total) / float64(pageSize))),
"path": c.Request.URL.Path,
})
}

func AdminIndex(c *gin.Context) {
Expand Down
2 changes: 1 addition & 1 deletion controllers/rss.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func RssGet(c *gin.Context) {
}

feed.Items = make([]*feeds.Item, 0)
posts, err := models.ListPublishedPost("")
posts, err := models.ListPublishedPost("", 0, 0)
if err == nil {
for _, post := range posts {
item := &feeds.Item{
Expand Down
60 changes: 43 additions & 17 deletions controllers/tag.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package controllers

import (
"net/http"
"strconv"

"math"

"github.com/gin-gonic/gin"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday"
"github.com/wangsongyan/wblog/models"
"net/http"
"strconv"
"github.com/wangsongyan/wblog/system"
)

func TagCreate(c *gin.Context) {
Expand All @@ -25,21 +29,43 @@ func TagCreate(c *gin.Context) {
}

func TagGet(c *gin.Context) {
tagName := c.Param("tag")
posts, err := models.ListPublishedPost(tagName)
if err == nil {
policy := bluemonday.StrictPolicy()
for _, post := range posts {
post.Tags, _ = models.ListTagByPostId(strconv.FormatUint(uint64(post.ID), 10))
post.Body = policy.Sanitize(string(blackfriday.MarkdownCommon([]byte(post.Body))))
}
c.HTML(http.StatusOK, "index/index.html", gin.H{
"posts": posts,
"tags": models.MustListTag(),
"archives": models.MustListPostArchives(),
"links": models.MustListLinks(),
})
} else {
var (
tagName string
page string
pageIndex int
pageSize = system.GetConfiguration().PageSize
total int
err error
policy *bluemonday.Policy
posts []*models.Post
)
tagName = c.Param("tag")
page = c.Query("page")
pageIndex, _ = strconv.Atoi(page)
if pageIndex <= 0 {
pageIndex = 1
}
posts, err = models.ListPublishedPost(tagName, pageIndex, pageSize)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
total, err = models.CountPostByTag(tagName)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
policy = bluemonday.StrictPolicy()
for _, post := range posts {
post.Tags, _ = models.ListTagByPostId(strconv.FormatUint(uint64(post.ID), 10))
post.Body = policy.Sanitize(string(blackfriday.MarkdownCommon([]byte(post.Body))))
}
c.HTML(http.StatusOK, "index/index.html", gin.H{
"posts": posts,
"tags": models.MustListTag(),
"archives": models.MustListPostArchives(),
"links": models.MustListLinks(),
"pageIndex": pageIndex,
"totalPage": int(math.Ceil(float64(total) / float64(pageSize))),
})
}
7 changes: 6 additions & 1 deletion helpers/template.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package helpers

import (
"github.com/wangsongyan/wblog/models"
"strings"
"time"

"github.com/wangsongyan/wblog/models"
)

// 格式化时间
Expand Down Expand Up @@ -38,6 +39,10 @@ func Add(a1, a2 int) int {
return a1 + a2
}

func Minus(a1, a2 int) int {
return a1 - a2
}

func ListTag() string {
tags, err := models.ListTag()
if err == nil {
Expand Down
Loading

0 comments on commit 2792965

Please sign in to comment.