@@ -893,7 +893,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
893
893
}
894
894
fallthrough
895
895
case CommentTypeComment :
896
- if _ , err = db . Exec (ctx , "UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?" , opts .Issue .ID ); err != nil {
896
+ if err := UpdateIssueNumComments (ctx , opts .Issue .ID ); err != nil {
897
897
return err
898
898
}
899
899
fallthrough
@@ -1182,8 +1182,8 @@ func DeleteComment(ctx context.Context, comment *Comment) error {
1182
1182
return err
1183
1183
}
1184
1184
1185
- if comment .Type == CommentTypeComment {
1186
- if _ , err := e . ID ( comment .IssueID ). Decr ( "num_comments" ). Update ( new ( Issue ) ); err != nil {
1185
+ if comment .Type == CommentTypeComment || comment . Type == CommentTypeReview {
1186
+ if err := UpdateIssueNumComments ( ctx , comment .IssueID ); err != nil {
1187
1187
return err
1188
1188
}
1189
1189
}
@@ -1300,6 +1300,26 @@ func (c *Comment) HasOriginalAuthor() bool {
1300
1300
return c .OriginalAuthor != "" && c .OriginalAuthorID != 0
1301
1301
}
1302
1302
1303
+ func CountCommentsBuilder (issueID int64 ) * builder.Builder {
1304
+ return builder .Select ("count(*)" ).From ("comment" ).Where (builder.Eq {
1305
+ "issue_id" : issueID ,
1306
+ }.And (builder .Or (
1307
+ builder.Eq {"type" : CommentTypeComment },
1308
+ builder.Eq {"type" : CommentTypeReview },
1309
+ builder.Eq {"type" : CommentTypeCode },
1310
+ )).And (builder.Neq {
1311
+ "invalidated" : true ,
1312
+ }))
1313
+ }
1314
+
1315
+ func UpdateIssueNumComments (ctx context.Context , issueID int64 ) error {
1316
+ _ , err := db .GetEngine (ctx ).
1317
+ SetExpr ("num_comments" , CountCommentsBuilder (issueID )).
1318
+ ID (issueID ).
1319
+ Update (new (Issue ))
1320
+ return err
1321
+ }
1322
+
1303
1323
// InsertIssueComments inserts many comments of issues.
1304
1324
func InsertIssueComments (ctx context.Context , comments []* Comment ) error {
1305
1325
if len (comments ) == 0 {
@@ -1332,8 +1352,7 @@ func InsertIssueComments(ctx context.Context, comments []*Comment) error {
1332
1352
}
1333
1353
1334
1354
for _ , issueID := range issueIDs {
1335
- if _ , err := db .Exec (ctx , "UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ? AND `type`=?) WHERE id = ?" ,
1336
- issueID , CommentTypeComment , issueID ); err != nil {
1355
+ if err := UpdateIssueNumComments (ctx , issueID ); err != nil {
1337
1356
return err
1338
1357
}
1339
1358
}
0 commit comments