From 58f1e780549e8ac3cc6c2a36bf59d2640f3627e3 Mon Sep 17 00:00:00 2001 From: yanghao05 Date: Fri, 11 Apr 2025 16:09:06 +0800 Subject: [PATCH] feat: update --- backend/app/http/posts.go | 14 +++++- backend/app/http/routes.gen.go | 10 +++- backend/app/models/posts_test.go | 13 +++++ backend/app/models/users.go | 86 +++++++++++++++++++++----------- backend/test.http | 4 ++ 5 files changed, 95 insertions(+), 32 deletions(-) diff --git a/backend/app/http/posts.go b/backend/app/http/posts.go index 52ca823..1ec3e94 100644 --- a/backend/app/http/posts.go +++ b/backend/app/http/posts.go @@ -6,6 +6,7 @@ import ( "quyun/database/schemas/public/model" "github.com/gofiber/fiber/v3" + "github.com/gofiber/fiber/v3/log" ) type ListQuery struct { @@ -16,7 +17,7 @@ type ListQuery struct { type posts struct{} // List posts -// @Router /v1/posts [get] +// @Router /posts [get] // @Bind pagination query // @Bind query query func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery) (*requests.Pager, error) { @@ -25,8 +26,17 @@ func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *Li } // Show -// @Router /v1/posts/:id [get] +// @Router /show/:id [get] // @Bind id path func (ctl *posts) Show(ctx fiber.Ctx, id int64) (*model.Posts, error) { return models.Posts.GetByID(ctx.Context(), id) } + +// Mine posts +// @Router /mine [get] +// @Bind pagination query +// @Bind query query +func (ctl *posts) Mine(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery) (*requests.Pager, error) { + log.Infof("Fetching posts for user with pagination: %+v and keyword: %v", pagination, query.Keyword) + return models.Users.PostList(ctx.Context(), 1, pagination, query.Keyword) +} diff --git a/backend/app/http/routes.gen.go b/backend/app/http/routes.gen.go index ef212be..0cc7995 100644 --- a/backend/app/http/routes.gen.go +++ b/backend/app/http/routes.gen.go @@ -28,15 +28,21 @@ func (r *Routes) Name() string { func (r *Routes) Register(router fiber.Router) { // 注册路由组: posts - router.Get("/v1/posts", DataFunc2( + router.Get("/", DataFunc2( r.posts.List, Query[requests.Pagination]("pagination"), Query[ListQuery]("query"), )) - router.Get("/v1/posts/:id", DataFunc1( + router.Get("/show/:id", DataFunc1( r.posts.Show, PathParam[int64]("id"), )) + router.Get("/mine", DataFunc2( + r.posts.Mine, + Query[requests.Pagination]("pagination"), + Query[ListQuery]("query"), + )) + } diff --git a/backend/app/models/posts_test.go b/backend/app/models/posts_test.go index 664d5d3..05a1d7e 100644 --- a/backend/app/models/posts_test.go +++ b/backend/app/models/posts_test.go @@ -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) + } + } + }) +} diff --git a/backend/app/models/users.go b/backend/app/models/users.go index c59c5d7..c5ce636 100644 --- a/backend/app/models/users.go +++ b/backend/app/models/users.go @@ -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 +} diff --git a/backend/test.http b/backend/test.http index c6b344a..9b8dfd0 100644 --- a/backend/test.http +++ b/backend/test.http @@ -54,4 +54,8 @@ Content-Type: application/json ### get orders GET {{host}}/v1/admin/orders HTTP/1.1 +Content-Type: application/json + +### get orders +GET {{host}}/mine HTTP/1.1 Content-Type: application/json \ No newline at end of file