From ea6db56fb33b68fc297225ad322e264dc4599e4d Mon Sep 17 00:00:00 2001 From: rogeecn Date: Sat, 22 Mar 2025 20:01:45 +0800 Subject: [PATCH] feat: add user posts --- app/models/posts.go | 57 ++++++++++++++++++++++++++++++++++++ app/models/users.go | 62 ++++++++++++++++++++++++++++++++++++++++ app/service/http/http.go | 4 ++- 3 files changed, 122 insertions(+), 1 deletion(-) diff --git a/app/models/posts.go b/app/models/posts.go index 11ee502..4f38e61 100644 --- a/app/models/posts.go +++ b/app/models/posts.go @@ -2,6 +2,7 @@ package models import ( "context" + "errors" "quyun/app/requests" "quyun/database/fields" @@ -9,6 +10,7 @@ import ( "quyun/database/schemas/public/table" . "github.com/go-jet/jet/v2/postgres" + "github.com/go-jet/jet/v2/qrm" "github.com/sirupsen/logrus" ) @@ -147,3 +149,58 @@ func (m *postsModel) List(ctx context.Context, pagination *requests.Pagination, Pagination: *pagination, }, nil } + +func (m *postsModel) IsUserBought(ctx context.Context, userId int64, postId int64) (bool, error) { + tbl := table.UserPosts + stmt := tbl. + SELECT(tbl.ID). + WHERE( + tbl.UserID.EQ(Int64(userId)).AND( + tbl.PostID.EQ(Int64(postId)), + ), + ) + m.log.Infof("sql: %s", stmt.DebugSql()) + + var userPost model.UserPosts + err := stmt.QueryContext(ctx, db, &userPost) + if err != nil { + if errors.Is(err, qrm.ErrNoRows) { + return false, nil + } + + m.log.Errorf("error querying user post item: %v", err) + return false, err + } + + return userPost.ID > 0, nil +} + +func (m *postsModel) Buy(ctx context.Context, userId int64, postId int64) error { + tbl := table.UserPosts + + post, err := m.GetByID(ctx, postId) + if err != nil { + m.log.Errorf("error getting post by ID: %v", err) + return err + } + + user, err := Users.GetByID(ctx, userId) + if err != nil { + m.log.Errorf("error getting user by ID: %v", err) + return err + } + + record := model.UserPosts{ + UserID: user.ID, + PostID: post.ID, + Price: post.Price * int64(post.Discount) / 100, + } + stmt := tbl.INSERT(tbl.MutableColumns).MODEL(record) + m.log.Infof("sql: %s", stmt.DebugSql()) + + if _, err := stmt.ExecContext(ctx, db); err != nil { + m.log.Errorf("error buying post: %v", err) + return err + } + return nil +} diff --git a/app/models/users.go b/app/models/users.go index 4f5d6d4..c5fe6c4 100644 --- a/app/models/users.go +++ b/app/models/users.go @@ -1,6 +1,13 @@ package models import ( + "context" + + "quyun/database/schemas/public/model" + "quyun/database/schemas/public/table" + + . "github.com/go-jet/jet/v2/postgres" + "github.com/samber/lo" "github.com/sirupsen/logrus" ) @@ -13,3 +20,58 @@ func (m *usersModel) Prepare() error { m.log = logrus.WithField("model", "usersModel") return nil } + +// GetByID +func (m *usersModel) GetByID(ctx context.Context, id int64) (*model.Users, error) { + tbl := table.Users + + stmt := tbl. + SELECT(tbl.AllColumns). + WHERE( + tbl.ID.EQ(Int64(id)), + ) + m.log.Infof("sql: %s", stmt.DebugSql()) + + var user model.Users + err := stmt.QueryContext(ctx, db, &user) + if err != nil { + m.log.Errorf("error querying user by ID: %v", err) + return nil, err + } + + return &user, nil +} + +func (m *usersModel) Posts(ctx context.Context, userID int64) ([]*model.Posts, error) { + tblUserPosts := table.UserPosts + stmtUserPosts := tblUserPosts. + SELECT(tblUserPosts.PostID). + WHERE(tblUserPosts.UserID.EQ(Int64(userID))) + m.log.Infof("sql: %s", stmtUserPosts.DebugSql()) + + var userPosts []model.UserPosts + err := stmtUserPosts.QueryContext(ctx, db, &userPosts) + if err != nil { + m.log.Errorf("error querying user posts: %v", err) + return nil, err + } + + postIds := lo.Map(userPosts, func(up model.UserPosts, _ int) Expression { + return Int64(up.PostID) + }) + + tbl := table.Posts + stmt := tbl. + SELECT(tbl.AllColumns). + WHERE(tbl.ID.IN(postIds...)) + m.log.Infof("sql: %s", stmt.DebugSql()) + + var posts []*model.Posts + + if err := stmt.QueryContext(ctx, db, &posts); err != nil { + m.log.Errorf("error querying posts by user ID: %v", err) + return nil, err + } + + return posts, nil +} diff --git a/app/service/http/http.go b/app/service/http/http.go index 1c32372..80c37ea 100644 --- a/app/service/http/http.go +++ b/app/service/http/http.go @@ -45,7 +45,9 @@ func Command() atom.Option { defaultProviders(). With( jobs.Provide, - appHttp.Provide, + ). + WithProviders( + appHttp.Providers(), ), ), )