-
Notifications
You must be signed in to change notification settings - Fork 284
/
page.go
133 lines (123 loc) · 2.63 KB
/
page.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package controllers
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/wangsongyan/wblog/models"
)
func PageGet(c *gin.Context) {
id := c.Param("id")
page, err := models.GetPageById(id)
if err != nil || !page.IsPublished {
Handle404(c)
return
}
page.View++
page.UpdateView()
c.HTML(http.StatusOK, "page/display.html", gin.H{
"page": page,
})
}
func PageNew(c *gin.Context) {
c.HTML(http.StatusOK, "page/new.html", nil)
}
func PageCreate(c *gin.Context) {
title := c.PostForm("title")
body := c.PostForm("body")
isPublished := c.PostForm("isPublished")
published := "on" == isPublished
page := &models.Page{
Title: title,
Body: body,
IsPublished: published,
}
err := page.Insert()
if err != nil {
c.HTML(http.StatusOK, "page/new.html", gin.H{
"message": err.Error(),
"page": page,
})
return
}
c.Redirect(http.StatusMovedPermanently, "/admin/page")
}
func PageEdit(c *gin.Context) {
id := c.Param("id")
page, err := models.GetPageById(id)
if err != nil {
Handle404(c)
}
c.HTML(http.StatusOK, "page/modify.html", gin.H{
"page": page,
})
}
func PageUpdate(c *gin.Context) {
id := c.Param("id")
title := c.PostForm("title")
body := c.PostForm("body")
isPublished := c.PostForm("isPublished")
published := "on" == isPublished
pid, err := strconv.ParseUint(id, 10, 64)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
page := &models.Page{Title: title, Body: body, IsPublished: published}
page.ID = uint(pid)
err = page.Update()
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.Redirect(http.StatusMovedPermanently, "/admin/page")
}
func PagePublish(c *gin.Context) {
var (
err error
res = gin.H{}
)
defer writeJSON(c, res)
id := c.Param("id")
page, err := models.GetPageById(id)
if err == nil {
res["message"] = err.Error()
return
}
page.IsPublished = !page.IsPublished
err = page.Update()
if err == nil {
res["message"] = err.Error()
return
}
res["succeed"] = true
}
func PageDelete(c *gin.Context) {
var (
err error
res = gin.H{}
)
defer writeJSON(c, res)
id := c.Param("id")
pid, err := strconv.ParseUint(id, 10, 64)
if err != nil {
res["message"] = err.Error()
return
}
page := &models.Page{}
page.ID = uint(pid)
err = page.Delete()
if err != nil {
res["message"] = err.Error()
return
}
res["succeed"] = true
}
func PageIndex(c *gin.Context) {
pages, _ := models.ListAllPage()
user, _ := c.Get(CONTEXT_USER_KEY)
c.HTML(http.StatusOK, "admin/page.html", gin.H{
"pages": pages,
"user": user,
"comments": models.MustListUnreadComment(),
})
}