Repository navigation
Expand file tree
/
Copy pathcomment_controller.go
More file actions
78 lines (62 loc) · 1.91 KB
/
Copy pathcomment_controller.go
File metadata and controls
78 lines (62 loc) · 1.91 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
package admin
import (
"bbs-go/pkg/markdown"
"strconv"
"bbs-go/controllers/render"
"github.com/kataras/iris/v12"
"github.com/mlogclub/simple"
"bbs-go/services"
)
type CommentController struct {
Ctx iris.Context
}
func (c *CommentController) GetBy(id int64) *simple.JsonResult {
t := services.CommentService.Get(id)
if t == nil {
return simple.JsonErrorMsg("Not found, id=" + strconv.FormatInt(id, 10))
}
return simple.JsonData(t)
}
func (c *CommentController) AnyList() *simple.JsonResult {
var (
id = simple.FormValueInt64Default(c.Ctx, "id", 0)
userId = simple.FormValueInt64Default(c.Ctx, "userId", 0)
entityType = simple.FormValueDefault(c.Ctx, "entityType", "")
entityId = simple.FormValueInt64Default(c.Ctx, "entityId", 0)
)
params := simple.NewQueryParams(c.Ctx).
EqByReq("status").
PageByReq().Desc("id")
if id > 0 {
params.Eq("id", id)
}
if userId > 0 {
params.Eq("user_id", userId)
}
if simple.IsNotBlank(entityType) && entityId > 0 {
params.Eq("entity_type", entityType).Eq("entity_id", entityId)
}
if id <= 0 && userId <= 0 && (simple.IsBlank(entityType) || entityId <= 0) {
// return simple.JsonErrorMsg("请输入必要的查询参数。")
return simple.JsonSuccess()
}
list, paging := services.CommentService.FindPageByParams(params)
var results []map[string]interface{}
for _, comment := range list {
builder := simple.NewRspBuilderExcludes(comment, "content")
// 用户
builder = builder.Put("user", render.BuildUserInfoDefaultIfNull(comment.UserId))
// 简介
content := markdown.ToHTML(comment.Content)
builder.Put("content", content)
results = append(results, builder.Build())
}
return simple.JsonPageData(results, paging)
}
func (c *CommentController) PostDeleteBy(id int64) *simple.JsonResult {
if err := services.CommentService.Delete(id); err != nil {
return simple.JsonErrorMsg(err.Error())
} else {
return simple.JsonSuccess()
}
}