feat: update
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"quyun/database/schemas/public/model"
|
"quyun/database/schemas/public/model"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v3"
|
"github.com/gofiber/fiber/v3"
|
||||||
|
"github.com/gofiber/fiber/v3/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ListQuery struct {
|
type ListQuery struct {
|
||||||
@@ -16,7 +17,7 @@ type ListQuery struct {
|
|||||||
type posts struct{}
|
type posts struct{}
|
||||||
|
|
||||||
// List posts
|
// List posts
|
||||||
// @Router /v1/posts [get]
|
// @Router /posts [get]
|
||||||
// @Bind pagination query
|
// @Bind pagination query
|
||||||
// @Bind query query
|
// @Bind query query
|
||||||
func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery) (*requests.Pager, error) {
|
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
|
// Show
|
||||||
// @Router /v1/posts/:id [get]
|
// @Router /show/:id [get]
|
||||||
// @Bind id path
|
// @Bind id path
|
||||||
func (ctl *posts) Show(ctx fiber.Ctx, id int64) (*model.Posts, error) {
|
func (ctl *posts) Show(ctx fiber.Ctx, id int64) (*model.Posts, error) {
|
||||||
return models.Posts.GetByID(ctx.Context(), id)
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -28,15 +28,21 @@ func (r *Routes) Name() string {
|
|||||||
|
|
||||||
func (r *Routes) Register(router fiber.Router) {
|
func (r *Routes) Register(router fiber.Router) {
|
||||||
// 注册路由组: posts
|
// 注册路由组: posts
|
||||||
router.Get("/v1/posts", DataFunc2(
|
router.Get("/", DataFunc2(
|
||||||
r.posts.List,
|
r.posts.List,
|
||||||
Query[requests.Pagination]("pagination"),
|
Query[requests.Pagination]("pagination"),
|
||||||
Query[ListQuery]("query"),
|
Query[ListQuery]("query"),
|
||||||
))
|
))
|
||||||
|
|
||||||
router.Get("/v1/posts/:id", DataFunc1(
|
router.Get("/show/:id", DataFunc1(
|
||||||
r.posts.Show,
|
r.posts.Show,
|
||||||
PathParam[int64]("id"),
|
PathParam[int64]("id"),
|
||||||
))
|
))
|
||||||
|
|
||||||
|
router.Get("/mine", DataFunc2(
|
||||||
|
r.posts.Mine,
|
||||||
|
Query[requests.Pagination]("pagination"),
|
||||||
|
Query[ListQuery]("query"),
|
||||||
|
))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
"quyun/database/schemas/public/table"
|
"quyun/database/schemas/public/table"
|
||||||
|
|
||||||
. "github.com/go-jet/jet/v2/postgres"
|
. "github.com/go-jet/jet/v2/postgres"
|
||||||
"github.com/samber/lo"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -44,38 +43,23 @@ func (m *usersModel) GetByID(ctx context.Context, id int64) (*model.Users, error
|
|||||||
return &user, nil
|
return &user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *usersModel) Posts(ctx context.Context, userID int64) ([]*model.Posts, error) {
|
func (m *usersModel) Own(ctx context.Context, userID, postID int64) error {
|
||||||
tblUserPosts := table.UserPosts
|
tbl := table.UserPosts
|
||||||
stmtUserPosts := tblUserPosts.
|
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(&model.UserPosts{
|
||||||
SELECT(tblUserPosts.PostID).
|
UserID: userID,
|
||||||
WHERE(tblUserPosts.UserID.EQ(Int64(userID)))
|
PostID: postID,
|
||||||
m.log.Infof("sql: %s", stmtUserPosts.DebugSql())
|
CreatedAt: time.Now(),
|
||||||
|
UpdatedAt: time.Now(),
|
||||||
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())
|
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||||
|
|
||||||
var posts []*model.Posts
|
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||||
|
m.log.Errorf("error inserting user post: %v", err)
|
||||||
if err := stmt.QueryContext(ctx, db, &posts); err != nil {
|
return err
|
||||||
m.log.Errorf("error querying posts by user ID: %v", err)
|
|
||||||
return nil, 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
|
// BuildConditionWithKey builds the WHERE clause for user queries
|
||||||
@@ -197,3 +181,49 @@ func (m *usersModel) DeleteByID(ctx context.Context, id int64) error {
|
|||||||
}
|
}
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -55,3 +55,7 @@ Content-Type: application/json
|
|||||||
### get orders
|
### get orders
|
||||||
GET {{host}}/v1/admin/orders HTTP/1.1
|
GET {{host}}/v1/admin/orders HTTP/1.1
|
||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
|
|
||||||
|
### get orders
|
||||||
|
GET {{host}}/mine HTTP/1.1
|
||||||
|
Content-Type: application/json
|
||||||
Reference in New Issue
Block a user