forked from wangsongyan/wblog
/
tag.go
75 lines (70 loc) · 1.79 KB
/
tag.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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"
"github.com/wangsongyan/wblog/system"
)
func TagCreate(c *gin.Context) {
var (
err error
res = gin.H{}
)
defer writeJSON(c, res)
name := c.PostForm("value")
tag := &models.Tag{Name: name}
err = tag.Insert()
if err != nil {
res["message"] = err.Error()
return
}
res["succeed"] = true
res["data"] = tag
}
func TagGet(c *gin.Context) {
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))),
"maxReadPosts": models.MustListMaxReadPost(),
"maxCommentPosts": models.MustListMaxCommentPost(),
})
}