Repository navigation
Expand file tree
/
Copy pathtopic_controller.go
More file actions
92 lines (80 loc) · 2.36 KB
/
Copy pathtopic_controller.go
File metadata and controls
92 lines (80 loc) · 2.36 KB
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
package admin
import (
"strconv"
"github.com/kataras/iris/v12"
"github.com/mlogclub/simple"
"bbs-go/controllers/render"
"bbs-go/services"
)
type TopicController struct {
Ctx iris.Context
}
func (c *TopicController) GetBy(id int64) *simple.JsonResult {
t := services.TopicService.Get(id)
if t == nil {
return simple.JsonErrorMsg("Not found, id=" + strconv.FormatInt(id, 10))
}
return simple.JsonData(t)
}
func (c *TopicController) AnyList() *simple.JsonResult {
list, paging := services.TopicService.FindPageByParams(simple.NewQueryParams(c.Ctx).
EqByReq("id").EqByReq("user_id").EqByReq("status").EqByReq("recommend").LikeByReq("title").PageByReq().Desc("id"))
var results []map[string]interface{}
for _, topic := range list {
item := render.BuildSimpleTopic(&topic)
builder := simple.NewRspBuilder(item)
builder.Put("status", topic.Status)
results = append(results, builder.Build())
}
return simple.JsonData(&simple.PageResult{Results: results, Page: paging})
}
// 推荐
func (c *TopicController) PostRecommend() *simple.JsonResult {
id, err := simple.FormValueInt64(c.Ctx, "id")
if err != nil {
return simple.JsonErrorMsg(err.Error())
}
err = services.TopicService.SetRecommend(id, true)
if err != nil {
return simple.JsonErrorMsg(err.Error())
}
return simple.JsonSuccess()
}
// 取消推荐
func (c *TopicController) DeleteRecommend() *simple.JsonResult {
id, err := simple.FormValueInt64(c.Ctx, "id")
if err != nil {
return simple.JsonErrorMsg(err.Error())
}
err = services.TopicService.SetRecommend(id, false)
if err != nil {
return simple.JsonErrorMsg(err.Error())
}
return simple.JsonSuccess()
}
func (c *TopicController) PostDelete() *simple.JsonResult {
id, err := simple.FormValueInt64(c.Ctx, "id")
if err != nil {
return simple.JsonErrorMsg(err.Error())
}
user := services.UserTokenService.GetCurrent(c.Ctx)
if user == nil {
return simple.JsonError(simple.ErrorNotLogin)
}
err = services.TopicService.Delete(id, user.Id, c.Ctx.Request())
if err != nil {
return simple.JsonErrorMsg(err.Error())
}
return simple.JsonSuccess()
}
func (c *TopicController) PostUndelete() *simple.JsonResult {
id, err := simple.FormValueInt64(c.Ctx, "id")
if err != nil {
return simple.JsonErrorMsg(err.Error())
}
err = services.TopicService.Undelete(id)
if err != nil {
return simple.JsonErrorMsg(err.Error())
}
return simple.JsonSuccess()
}