From d9245cfc215871982b096bca42a5ae5dd5effd19 Mon Sep 17 00:00:00 2001 From: Rogee Date: Tue, 6 May 2025 16:14:36 +0800 Subject: [PATCH] feat: add incrment of views --- backend/app/http/posts.go | 1 + backend/app/models/posts.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/backend/app/http/posts.go b/backend/app/http/posts.go index 02eece9..8fa0264 100644 --- a/backend/app/http/posts.go +++ b/backend/app/http/posts.go @@ -165,6 +165,7 @@ func (ctl *posts) Play(ctx fiber.Ctx, id int64, user *model.Users) (*PlayUrl, er log.WithError(err).Errorf("GetByID err: %v", err) return nil, err } + go models.Posts.IncrViewCount(ctx.Context(), post.ID) preview := false bought, err := models.Users.HasBought(ctx.Context(), user.ID, post.ID) diff --git a/backend/app/models/posts.go b/backend/app/models/posts.go index a62f0ad..b5227de 100644 --- a/backend/app/models/posts.go +++ b/backend/app/models/posts.go @@ -26,6 +26,21 @@ func (m *postsModel) Prepare() error { return nil } +func (m *postsModel) IncrViewCount(ctx context.Context, id int64) error { + tbl := table.Posts + + stmt := tbl.UPDATE(tbl.Views).SET(tbl.Views.ADD(Int64(1))).WHERE(tbl.ID.EQ(Int64(id))) + m.log.Infof("sql: %s", stmt.DebugSql()) + + var post model.Posts + err := stmt.QueryContext(ctx, db, &post) + if err != nil { + m.log.Errorf("error updating post view count: %v", err) + return err + } + return nil +} + // GetByID func (m *postsModel) GetByID(ctx context.Context, id int64, cond ...conds.Cond) (*model.Posts, error) { tbl := table.Posts