-
Notifications
You must be signed in to change notification settings - Fork 1
/
archive.go
61 lines (56 loc) · 1.59 KB
/
archive.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package api
import (
"net/http"
"strconv"
"math"
"github.com/gin-gonic/gin"
"github.com/gitlubtaotao/wblog/models"
"github.com/gitlubtaotao/wblog/system"
"github.com/microcosm-cc/bluemonday"
"gopkg.in/russross/blackfriday.v2"
)
func ArchiveGet(c *gin.Context) {
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.Run([]byte(post.Body), blackfriday.WithNoExtensions())))
}
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))),
"maxReadPosts": models.MustListMaxReadPost(),
"maxCommentPosts": models.MustListMaxCommentPost(),
})
}