feat: update

This commit is contained in:
yanghao05
2025-04-11 16:09:06 +08:00
parent 79972e963c
commit 58f1e78054
5 changed files with 95 additions and 32 deletions

View File

@@ -74,3 +74,16 @@ func (s *PostsTestSuite) Test_BatchInsert() {
}
})
}
func (s *PostsTestSuite) Test_BatchInsertUserPosts() {
Convey("Test_Demo", s.T(), func() {
database.Truncate(context.Background(), db, table.UserPosts.TableName())
count := 10
for i := 0; i < count; i++ {
if err := Users.Own(context.Background(), 1, int64(i)+1); err != nil {
s.T().Fatal(err)
}
}
})
}

View File

@@ -9,7 +9,6 @@ import (
"quyun/database/schemas/public/table"
. "github.com/go-jet/jet/v2/postgres"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
)
@@ -44,38 +43,23 @@ func (m *usersModel) GetByID(ctx context.Context, id int64) (*model.Users, error
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)
func (m *usersModel) Own(ctx context.Context, userID, postID int64) error {
tbl := table.UserPosts
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(&model.UserPosts{
UserID: userID,
PostID: postID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
})
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
if _, err := stmt.ExecContext(ctx, db); err != nil {
m.log.Errorf("error inserting user post: %v", err)
return err
}
m.log.Infof("user post inserted successfully: userID=%d, postID=%d", userID, postID)
return posts, nil
return nil
}
// BuildConditionWithKey builds the WHERE clause for user queries
@@ -197,3 +181,49 @@ func (m *usersModel) DeleteByID(ctx context.Context, id int64) error {
}
return nil
}
// PostList returns a paginated list of posts for a user
func (m *usersModel) PostList(ctx context.Context, userId int64, pagination *requests.Pagination, keyword *string) (*requests.Pager, error) {
pagination.Format()
tblUserPosts := table.UserPosts
tbl := table.Posts
stmt := SELECT(tbl.AllColumns).
FROM(tbl.
INNER_JOIN(
tblUserPosts,
tblUserPosts.PostID.EQ(tbl.ID).
AND(tblUserPosts.UserID.EQ(Int64(userId)))),
).
ORDER_BY(tblUserPosts.ID.DESC()).
LIMIT(pagination.Limit).
OFFSET(pagination.Offset)
m.log.Infof("sql: %s", stmt.DebugSql())
var posts []model.Posts
err := stmt.QueryContext(ctx, db, &posts)
if err != nil {
m.log.Errorf("error querying posts: %v", err)
return nil, err
}
// total count
var cnt struct {
Cnt int64
}
stmtCnt := tblUserPosts.SELECT(COUNT(tbl.ID).AS("cnt")).WHERE(tblUserPosts.UserID.EQ(Int64(userId)))
m.log.Infof("sql: %s", stmtCnt.DebugSql())
if err := stmtCnt.QueryContext(ctx, db, &cnt); err != nil {
m.log.Errorf("error counting users: %v", err)
return nil, err
}
return &requests.Pager{
Items: posts,
Total: cnt.Cnt,
Pagination: *pagination,
}, nil
}