-
Notifications
You must be signed in to change notification settings - Fork 0
/
article.go
145 lines (138 loc) · 3.48 KB
/
article.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
package admin
import (
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/irellik/gblog/model"
sl "github.com/irellik/gblog/service/local"
"github.com/irellik/gblog/utils"
"net/http"
"strconv"
"time"
)
// 创建文章
func ArticleStore(c *gin.Context) {
session := sessions.Default(c)
user_info := session.Get("user_info")
u_info := user_info.(map[string]string)
var article_form model.ArticleForm
if err := c.ShouldBind(&article_form); err != nil {
utils.Failed(c, http.StatusBadRequest, err.Error(), nil)
return
}
// 时间格式化
if article_form.PublishedAt == "" {
article_form.PublishedAt = time.Now().Format("2006-01-02 15:04:05")
}
// 插入
uid, _ := strconv.Atoi(u_info["uid"])
articleId, err := model.InsertPost(article_form, int64(uid))
if err != nil {
utils.Failed(c, http.StatusInternalServerError, "保存失败", nil)
return
}
response := map[string]int64{
"article_id": articleId,
}
utils.Success(c, response)
}
// 文章列表
func ArticleList(c *gin.Context) {
config := sl.GetConfig()
page := sl.GetPage(c)
offset := (page - 1) * config.Site.PageSize
pageSize := 20
status := c.Query("status")
// 是否需要搜索
keyword := c.Query("s")
var posts []model.Post
var total int
if keyword == "" {
posts, total = model.GetPosts(offset, pageSize, status)
} else {
posts, total = model.SearchPosts(keyword, offset, pageSize)
}
responsePosts := make([]map[string]interface{}, 0)
for _, post := range posts {
responsePosts = append(responsePosts, map[string]interface{}{
"id": post.Id,
"title": post.Title,
"created_at": post.CreatedAt.Format("2006-01-02 15:04:05"),
"abstract": post.Abstract,
})
}
data := map[string]interface{}{
"posts": responsePosts,
"total": total,
}
utils.Success(c, data)
}
// 删除文章
func ArticleDelete(c *gin.Context) {
idList := c.QueryArray("id_list[]")
res := false
if len(idList) > 0 {
res = model.DeletePost(idList)
}
if res {
utils.Success(c, nil)
} else {
utils.Failed(c, http.StatusInternalServerError, "failed", nil)
}
}
/**
详情
*/
func ArticleDetail(c *gin.Context) {
id := c.Param("id")
idInt, err := strconv.Atoi(id)
if err != nil {
utils.Failed(c, http.StatusNotFound, "Not Found", nil)
return
}
post, err := model.GetPost(idInt, false)
if err != nil {
utils.Failed(c, http.StatusNotFound, "Not Found", nil)
return
}
utils.Success(c, map[string]interface{}{
"post": map[string]interface{}{
"id": post.Id,
"title": post.Title,
"content": post.Content,
"status": post.Status,
"category": post.CatId,
"published_at": post.PublishedAt.Unix(),
"abstract": post.Abstract,
"tags": post.Tags,
},
})
}
func ArticleUpdate(c *gin.Context) {
id := c.Param("id")
idInt, err := strconv.Atoi(id)
if err != nil {
utils.Failed(c, http.StatusNotFound, "Not Found", nil)
return
}
_, err = model.GetPost(idInt, false)
if err != nil {
utils.Failed(c, http.StatusNotFound, "Not Found", nil)
return
}
var article_form model.ArticleForm
if err := c.ShouldBind(&article_form); err != nil {
utils.Failed(c, http.StatusBadRequest, err.Error(), nil)
return
}
// 时间格式化
if article_form.PublishedAt == "" {
article_form.PublishedAt = time.Now().Format("2006-01-02 15:04:05")
}
// 更新
err = model.UpdatePost(idInt, article_form)
if err != nil {
utils.Failed(c, http.StatusInternalServerError, "Internal Server Error", nil)
return
}
utils.Success(c, nil)
}