forked from shitfSign/gosense
/
admin-controller.go
254 lines (238 loc) · 6.16 KB
/
admin-controller.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package main
import (
"database/sql"
"fmt"
"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
_ "github.com/go-sql-driver/mysql"
"github.com/golang/groupcache/lru"
"html/template"
"log"
"net/http"
"strconv"
"time"
)
type AdminLoginForm struct {
Username string `form:"username" binding:"required"`
Password string `form:"password" binding:"required"`
}
type BlogItem struct {
Title string `form:"title" binding:"required"`
Content string `form:"content" binding:"required"`
}
type EditBlogItem struct {
Aid string `form:"aid" binding:"required"`
Title string `form:"title" binding:"required"`
Content string `form:"content" binding:"required"`
}
type AdminController struct {
}
func (ac *AdminController) ListBlogCtr(c *gin.Context) {
session := sessions.Default(c)
username := session.Get("username")
fmt.Println(username)
if username == nil {
(&umsg{"You have no permission", "/"}).ShowMessage(c)
return
}
page, err := strconv.Atoi(c.DefaultQuery("page", "1"))
if err != nil {
log.Fatal(err)
}
page -= 1
if page < 0 {
page = 0
}
prev_page := page
if prev_page < 1 {
prev_page = 1
}
next_page := page + 2
var blogList string
rpp := 20
offset := page * rpp
log.Println(rpp)
log.Println(offset)
rows, err := DB.Query("Select aid, title from top_article where publish_status = 1 order by aid desc limit ? offset ? ", &rpp, &offset)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var (
aid int
title sql.NullString
)
for rows.Next() {
err := rows.Scan(&aid, &title)
if err != nil {
log.Fatal(err)
}
blogList += fmt.Sprintf(
"<li><a href=\"/view/%d\">%s</a> [<a href=\"/admin/editblog/%d\">Edit</a>] [<a href=\"/admin/deleteblog/%d\">Delete</a>]</li>",
aid,
title.String,
aid,
aid,
)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
c.HTML(http.StatusOK, "admin.list.blog.html", gin.H{
"bloglist": template.HTML(blogList),
"username": username,
"prev_page": prev_page,
"next_page": next_page,
})
}
func (ac *AdminController) EditBlogCtr(c *gin.Context) {
session := sessions.Default(c)
username := session.Get("username")
if username == nil {
(&umsg{"You have no permission", "/"}).ShowMessage(c)
return
}
id := c.Param("id")
var blog VBlogItem
CKey := fmt.Sprintf("blogitem-%s", id)
val, ok := Cache.Get(CKey)
if val != nil && ok == true {
fmt.Println("Ok, we found cache, Cache Len: ", Cache.Len())
blog = val.(VBlogItem)
} else {
rows, err := DB.Query("Select * from top_article where aid = ?", &id)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var ()
for rows.Next() {
err := rows.Scan(&blog.aid, &blog.title, &blog.content, &blog.publish_time, &blog.publish_status)
if err != nil {
log.Fatal(err)
}
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
Cache.Add(CKey, blog)
}
c.HTML(http.StatusOK, "edit-blog.html", gin.H{
"aid": blog.aid,
"title": blog.title.String,
"content": template.HTML(blog.content.String),
"publish_time": blog.publish_time.String,
})
}
func (ac *AdminController) DeleteBlogCtr(c *gin.Context) {
session := sessions.Default(c)
username := session.Get("username")
if username == nil {
(&umsg{"You have no permission", "/"}).ShowMessage(c)
return
}
var BI EditBlogItem
c.BindWith(&BI, binding.Form)
if BI.Aid == "" {
(&umsg{"Can not find the blog been delete", "/"}).ShowMessage(c)
return
}
_, err := DB.Exec("delete from top_article where aid = ? limit 1", BI.Aid)
if err == nil {
Cache = lru.New(8192)
(&umsg{"Deleted Success", "/"}).ShowMessage(c)
} else {
(&umsg{"Failed to delete blog", "/"}).ShowMessage(c)
}
}
func (ac *AdminController) AddBlogCtr(c *gin.Context) {
session := sessions.Default(c)
username := session.Get("username")
if username == nil {
(&umsg{"You have no permission", "/"}).ShowMessage(c)
return
}
c.HTML(http.StatusOK, "add-blog.html", gin.H{})
}
func (ac *AdminController) SaveBlogEditCtr(c *gin.Context) {
session := sessions.Default(c)
username := session.Get("username")
if username == nil {
(&umsg{"You have no permission", "/"}).ShowMessage(c)
return
}
var BI EditBlogItem
c.BindWith(&BI, binding.Form)
if BI.Aid == "" {
(&umsg{"Can not find the blog been edit", "/"}).ShowMessage(c)
return
}
if BI.Title == "" {
(&umsg{"Title can not empty", "/"}).ShowMessage(c)
return
}
if BI.Content == "" {
(&umsg{"Content can not empty", "/"}).ShowMessage(c)
return
}
_, err := DB.Exec("update top_article set title=?, content=? where aid = ?", BI.Title, BI.Content, BI.Aid)
if err == nil {
Cache = lru.New(8192)
(&umsg{"Success", "/"}).ShowMessage(c)
} else {
(&umsg{"Failed to save blog", "/"}).ShowMessage(c)
}
}
func (ac *AdminController) SaveBlogAddCtr(c *gin.Context) {
session := sessions.Default(c)
username := session.Get("username")
if username == nil {
(&umsg{"You have no permission", "/"}).ShowMessage(c)
return
}
var BI BlogItem
c.BindWith(&BI, binding.Form)
if BI.Title == "" {
(&umsg{"Title can not empty", "/"}).ShowMessage(c)
return
}
if BI.Content == "" {
(&umsg{"Content can not empty", "/"}).ShowMessage(c)
return
}
_, err := DB.Exec(
"insert into top_article (title, content, publish_time, publish_status) values (?, ?, ?, 1)",
BI.Title, BI.Content, time.Now().Format("2006-01-02 15:04:05"))
if err == nil {
Cache = lru.New(8192)
(&umsg{"Success", "/"}).ShowMessage(c)
} else {
(&umsg{"Failed to save blog", "/"}).ShowMessage(c)
}
}
func (ac *AdminController) LoginCtr(c *gin.Context) {
c.HTML(http.StatusOK, "admin-login.html", gin.H{})
}
func (ac *AdminController) LoginProcessCtr(c *gin.Context) {
var form AdminLoginForm
c.BindWith(&form, binding.Form)
session := sessions.Default(c)
if form.Username == Config.Admin_user && form.Password == Config.Admin_password {
session.Set("username", "netroby")
session.Save()
c.Redirect(301, "/")
} else {
session.Delete("username")
session.Save()
(&umsg{"Login Failed. You have no permission", "/"}).ShowMessage(c)
}
}
func (ac *AdminController) LogoutCtr(c *gin.Context) {
session := sessions.Default(c)
session.Delete("username")
session.Save()
c.Redirect(301, "/")
}