Skip to content

Commit

Permalink
fix: 更新文章的观看数和评论数
Browse files Browse the repository at this point in the history
  • Loading branch information
dokidokikoi committed Sep 25, 2023
1 parent 6c0133f commit f505408
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

logs
harukaze.top.crt
harukaze.top_bundle.crt
harukaze.top.key

pg/
Expand Down
23 changes: 23 additions & 0 deletions internal/controller/article/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,28 @@ func (c *Controller) Get(ctx *gin.Context) {
core.WriteResponse(ctx, myErrors.ApiRecordNotFound, nil)
return
}
go func() {
cnt := 0
for cnt < 10 {
node := &meta.WhereNode{
Conditions: []*meta.Condition{
{
Field: "view_counts",
Operator: meta.EQUAL,
Value: s.ViewCounts,
},
},
}
err := c.srv.Article().UpdateByWhereNode(ctx, &article.Article{ID: s.ID, ViewCounts: s.ViewCounts + 1}, node, nil)
if err == nil {
return
}
s, err = c.srv.Article().Get(ctx, &article.Article{ID: s.ID}, nil)
if err != nil {
return
}
cnt++
}
}()
core.WriteResponse(ctx, nil, s)
}
25 changes: 25 additions & 0 deletions internal/controller/comment/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
myErrors "go-blog/internal/errors"

zaplog "github.com/dokidokikoi/go-common/log/zap"
meta "github.com/dokidokikoi/go-common/meta/option"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -51,5 +52,29 @@ func (c *Controller) Create(ctx *gin.Context) {
core.WriteResponse(ctx, myErrors.ApiErrDatabase, nil)
return
}

go func() {
cnt := 0
for cnt < 10 {
s, err := c.srv.Article().Get(ctx, &article.Article{ID: input.ArticleID}, nil)
if err != nil {
return
}
node := &meta.WhereNode{
Conditions: []*meta.Condition{
{
Field: "comment_counts",
Operator: meta.EQUAL,
Value: s.CommentCounts,
},
},
}
err = c.srv.Article().UpdateByWhereNode(ctx, &article.Article{ID: s.ID, CommentCounts: s.CommentCounts + 1}, node, nil)
if err == nil {
return
}
cnt++
}
}()
core.WriteResponse(ctx, nil, nil)
}
5 changes: 5 additions & 0 deletions internal/service/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type ArticleSrv interface {
DeleteCollection(ctx context.Context, examples []*article.Article, option *meta.DeleteCollectionOption) []error
List(ctx context.Context, example *article.Article, option *meta.ListOption) ([]*article.Article, int64, error)
ListByWhereNode(ctx context.Context, example *article.Article, node *meta.WhereNode, option *meta.ListOption) ([]*article.Article, int64, error)
UpdateByWhereNode(ctx context.Context, example *article.Article, node *meta.WhereNode, option *meta.UpdateOption) error

DeleteArticleAllTags(ctx context.Context, articleID uint) error
CreateArticleTagsCollection(ctx context.Context, ats []*article.ArticleTag, option *meta.CreateCollectionOption) []error
Expand Down Expand Up @@ -66,6 +67,10 @@ func (as *articleSrv) ListByWhereNode(ctx context.Context, example *article.Arti
return list, total, err
}

func (as *articleSrv) UpdateByWhereNode(ctx context.Context, example *article.Article, node *meta.WhereNode, option *meta.UpdateOption) error {
return as.store.Article().UpdateByWhere(ctx, node, example, option)
}

func (as *articleSrv) DeleteArticleAllTags(ctx context.Context, articleID uint) error {
return as.store.ArticleTag().Delete(ctx, &article.ArticleTag{ArticleID: articleID}, nil)
}
Expand Down

0 comments on commit f505408

Please sign in to comment.