-
Notifications
You must be signed in to change notification settings - Fork 1
/
comment_article.go
65 lines (59 loc) · 2 KB
/
comment_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
package comment
import (
"fmt"
"github.com/admpub/webx/application/dbschema"
"github.com/admpub/webx/application/model/official/article"
"github.com/webx-top/db"
"github.com/webx-top/echo"
"github.com/webx-top/echo/code"
)
func commentArticleGetTarget(ctx echo.Context, targetID uint64) (res echo.H, breadcrumb []echo.KV, detailURL string, err error) {
articleM := article.NewArticle(ctx)
err = articleM.Get(nil, `id`, targetID)
if err != nil {
if err == db.ErrNoMoreRows {
err = ctx.NewError(code.DataNotFound, `文章不存在`).SetZone(`id`)
}
return
}
res = articleM.AsMap()
id := fmt.Sprint(targetID)
breadcrumb = []echo.KV{
{K: `official/article/index`, V: ctx.T(`文章列表`)},
{K: `official/article/edit?id=` + id, V: articleM.Title},
}
detailURL = `article/` + id + ctx.DefaultExtension()
return
}
func commentArticleCheck(ctx echo.Context, f *dbschema.OfficialCommonComment) error {
if f.TargetId < 1 {
return ctx.NewError(code.InvalidParameter, `文章ID无效`).SetZone(`TargetId`)
}
newsM := dbschema.NewOfficialCommonArticle(ctx)
err := newsM.Get(nil, `id`, f.TargetId)
if err != nil {
if err != db.ErrNoMoreRows {
return err
}
return ctx.NewError(code.DataNotFound, `您要评论的文章(ID:%d)不存在`, f.TargetId).SetZone(`id`)
}
if newsM.CommentAutoDisplay == `Y` {
f.Display = `Y`
} else {
f.Display = `N`
}
f.TargetOwnerId = newsM.OwnerId
f.TargetOwnerType = newsM.OwnerType
return nil
}
func commentArticleAfterAdd(ctx echo.Context, f *dbschema.OfficialCommonComment) error {
if f.ReplyCommentId == 0 { //不包含对评论的回复统计,只包含根评论
newsM := dbschema.NewOfficialCommonArticle(ctx)
return newsM.UpdateField(nil, `comments`, db.Raw(`comments+1`), db.Cond{`id`: f.TargetId})
}
return nil
}
func commentArticleAfterDelete(ctx echo.Context, f *dbschema.OfficialCommonComment) error {
newsM := dbschema.NewOfficialCommonArticle(ctx)
return newsM.UpdateField(nil, `comments`, db.Raw(`comments-1`), db.Cond{`id`: f.TargetId})
}