-
Notifications
You must be signed in to change notification settings - Fork 30
/
topic.go
158 lines (128 loc) · 3.83 KB
/
topic.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
package frontend
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
fe "github.com/hhandhuan/ku-bbs/internal/entity/frontend"
"github.com/hhandhuan/ku-bbs/internal/service"
"github.com/hhandhuan/ku-bbs/internal/service/frontend"
)
var Topic = cTopic{}
type cTopic struct{}
// PublishPage 发布页面
func (c *cTopic) PublishPage(ctx *gin.Context) {
s := service.Context(ctx)
if !s.Check() {
s.To("/login").WithError("请登录后,再继续操作").Redirect()
return
}
nodes, err := frontend.NodeService(ctx).GetEnableNodes()
if err != nil {
s.To("/").WithError(err.Error()).Redirect()
} else {
s.View("frontend.topic.publish", gin.H{"nodes": nodes})
}
}
// PublishSubmit 发布提交
func (c *cTopic) PublishSubmit(ctx *gin.Context) {
s := service.Context(ctx)
var req fe.PublishTopicReq
if err := ctx.ShouldBind(&req); err != nil {
s.Back().WithError(err).WithData(req).Redirect()
return
}
verr := g.Validator().Data(req).Run(context.Background())
if verr != nil {
s.Back().WithError(verr.FirstError()).WithData(req).Redirect()
return
}
id, err := frontend.TopicService(ctx).Publish(&req)
if err != nil {
s.Back().WithError(err).WithData(req).Redirect()
} else {
s.To(fmt.Sprintf("/topics/%d", id)).WithMsg("发布成功").Redirect()
}
}
// DetailPage 话题详情
func (c *cTopic) DetailPage(ctx *gin.Context) {
s := service.Context(ctx)
topicID := gconv.Uint64(ctx.Param("id"))
authorID := gconv.Uint64(ctx.Query("author_id"))
topic, err := frontend.TopicService(ctx).GetDetail(topicID)
if err != nil {
s.To("/").WithError(err).Redirect()
return
}
list, err := frontend.CommentService(ctx).GetList(topicID, authorID)
if err != nil {
s.To("/").WithError(err).Redirect()
return
}
data := gin.H{"topic": topic, "comments": list, "author_id": authorID}
s.SetTitle(topic.Title).View("frontend.topic.detail", data)
}
// DeleteSubmit 删除话题
func (c *cTopic) DeleteSubmit(ctx *gin.Context) {
s := service.Context(ctx)
i := gconv.Uint64(ctx.Param("id"))
err := frontend.TopicService(ctx).Delete(i)
if err != nil {
s.To(fmt.Sprintf("/topics/%d", i)).WithError(err).Redirect()
} else {
s.To("/").WithMsg("删除成功").Redirect()
}
}
// EditPage 编辑话题
func (c *cTopic) EditPage(ctx *gin.Context) {
s := service.Context(ctx)
i := gconv.Uint64(ctx.Param("id"))
if !s.Check() {
s.To("/login").WithError("请登录后,再继续操作").Redirect()
return
}
nodes, err := frontend.NodeService(ctx).GetEnableNodes()
if err != nil {
s.To("/").WithError(err.Error()).Redirect()
return
}
topic, err := frontend.TopicService(ctx).GetDetail(i)
if err != nil {
s.To("/").WithError(err.Error()).Redirect()
return
}
s.View("frontend.topic.edit", gin.H{"nodes": nodes, "topic": topic})
}
// EditSubmit 编辑提交
func (c *cTopic) EditSubmit(ctx *gin.Context) {
s := service.Context(ctx)
i := gconv.Uint64(ctx.Param("id"))
var req fe.PublishTopicReq
if err := ctx.ShouldBind(&req); err != nil {
s.Back().WithError(err).WithData(req).Redirect()
return
}
verr := g.Validator().Data(req).Run(context.Background())
if verr != nil {
s.Back().WithError(verr.FirstError()).WithData(req).Redirect()
return
}
id, err := frontend.TopicService(ctx).Edit(i, &req)
if err != nil {
s.Back().WithError(err).WithData(req).Redirect()
} else {
s.To(fmt.Sprintf("/topics/%d", id)).WithMsg("编辑成功").Redirect()
}
}
// SettingCommentStateSubmit 设置话题讨论状态
func (c *cTopic) SettingCommentStateSubmit(ctx *gin.Context) {
s := service.Context(ctx)
i := gconv.Uint64(ctx.Param("id"))
err := frontend.TopicService(ctx).SettingCommentState(i)
if err != nil {
s.To(fmt.Sprintf("/topics/%d", i)).WithError(err).Redirect()
} else {
s.To(fmt.Sprintf("/topics/%d", i)).WithMsg("操作成功").Redirect()
}
}