From c9740e6403b0245dbd693b95bfbd7f4ce8ac4749 Mon Sep 17 00:00:00 2001 From: yanghao05 Date: Tue, 29 Apr 2025 11:24:54 +0800 Subject: [PATCH] feat: update player --- backend/app/http/posts.go | 48 +++++++++++++++++-- backend/app/http/routes.gen.go | 5 +- backend/app/models/users.go | 20 ++++++-- backend/test.http | 2 +- frontend/wechat/src/api/postApi.js | 2 +- frontend/wechat/src/views/ArticleDetail.vue | 12 +++++ .../wechat/src/views/PurchasedArticles.vue | 6 +-- 7 files changed, 81 insertions(+), 14 deletions(-) diff --git a/backend/app/http/posts.go b/backend/app/http/posts.go index 8923d20..2e4b07d 100644 --- a/backend/app/http/posts.go +++ b/backend/app/http/posts.go @@ -99,7 +99,7 @@ type PostItem struct { } // Show -// @Router /posts/:id [get] +// @Router /posts/:id/show [get] // @Bind id path // @Bind user local func (ctl *posts) Show(ctx fiber.Ctx, id int64, user *model.Users) (*PostItem, error) { @@ -184,13 +184,55 @@ func (ctl *posts) Play(ctx fiber.Ctx, id int64, user *model.Users) (*PlayUrl, er // @Router /posts/mine [get] // @Bind pagination query // @Bind query query -func (ctl *posts) Mine(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery) (*requests.Pager, error) { +// @Bind user local +func (ctl *posts) Mine(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery, user *model.Users) (*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, func(item model.Posts) model.Posts { + + cond := models.Posts.BuildConditionWithKey(query.Keyword) + pager, err := models.Users.PostList(ctx.Context(), 1, pagination, cond, func(item model.Posts) model.Posts { item.Assets = fields.ToJson([]fields.MediaAsset{}) item.Content = "" return item }) + if err != nil { + log.WithError(err).Errorf("post list err: %v", err) + return nil, err + } + + postIds := lo.Map(pager.Items.([]model.Posts), func(item model.Posts, _ int) int64 { return item.ID }) + if len(postIds) > 0 { + items := lo.FilterMap(pager.Items.([]model.Posts), func(item model.Posts, _ int) (PostItem, bool) { + medias, err := models.Posts.GetMediaByIds(ctx.Context(), item.HeadImages.Data) + if err != nil { + log.Errorf("GetMediaByIds err: %v", err) + return PostItem{}, false + } + mediaUrls := lo.FilterMap(medias, func(item model.Medias, _ int) (string, bool) { + url, err := ctl.oss.GetSignedUrl(ctx.Context(), item.Path) + if err != nil { + log.WithError(err).Errorf("head image GetSignedUrl err: %v", err) + return "", false + } + + return url, true + }) + + return PostItem{ + ID: item.ID, + Title: item.Title, + Description: item.Description, + Price: item.Price, + Discount: item.Discount, + Views: item.Views, + Likes: item.Likes, + Tags: item.Tags.Data, + HeadImages: mediaUrls, + }, true + }) + + pager.Items = items + } + return pager, nil } // Buy diff --git a/backend/app/http/routes.gen.go b/backend/app/http/routes.gen.go index a471c34..19b7c99 100644 --- a/backend/app/http/routes.gen.go +++ b/backend/app/http/routes.gen.go @@ -57,7 +57,7 @@ func (r *Routes) Register(router fiber.Router) { Local[*model.Users]("user"), )) - router.Get("/posts/:id", DataFunc2( + router.Get("/posts/:id/show", DataFunc2( r.posts.Show, PathParam[int64]("id"), Local[*model.Users]("user"), @@ -69,10 +69,11 @@ func (r *Routes) Register(router fiber.Router) { Local[*model.Users]("user"), )) - router.Get("/posts/mine", DataFunc2( + router.Get("/posts/mine", DataFunc3( r.posts.Mine, Query[requests.Pagination]("pagination"), Query[ListQuery]("query"), + Local[*model.Users]("user"), )) router.Get("/posts/:id/buy", DataFunc2( diff --git a/backend/app/models/users.go b/backend/app/models/users.go index f4cf9a8..1648ae6 100644 --- a/backend/app/models/users.go +++ b/backend/app/models/users.go @@ -186,19 +186,24 @@ func (m *usersModel) DeleteByID(ctx context.Context, id int64) error { } // PostList returns a paginated list of posts for a user -func (m *usersModel) PostList(ctx context.Context, userId int64, pagination *requests.Pagination, keyword *string, callbacks ...func(model.Posts) model.Posts) (*requests.Pager, error) { +func (m *usersModel) PostList(ctx context.Context, userId int64, pagination *requests.Pagination, cond BoolExpression, callbacks ...func(model.Posts) model.Posts) (*requests.Pager, error) { pagination.Format() tblUserPosts := table.UserPosts + cond = cond.AND( + tblUserPosts.UserID.EQ(Int64(userId)), + ) + tbl := table.Posts stmt := SELECT(tbl.AllColumns). FROM(tbl. - INNER_JOIN( + RIGHT_JOIN( tblUserPosts, - tblUserPosts.PostID.EQ(tbl.ID). - AND(tblUserPosts.UserID.EQ(Int64(userId)))), + tblUserPosts.PostID.EQ(tbl.ID), + ), ). + WHERE(cond). ORDER_BY(tblUserPosts.ID.DESC()). LIMIT(pagination.Limit). OFFSET(pagination.Offset) @@ -207,6 +212,13 @@ func (m *usersModel) PostList(ctx context.Context, userId int64, pagination *req var posts []model.Posts err := stmt.QueryContext(ctx, db, &posts) if err != nil { + if errors.Is(err, qrm.ErrNoRows) { + return &requests.Pager{ + Items: nil, + Total: 0, + Pagination: *pagination, + }, nil + } m.log.Errorf("error querying posts: %v", err) return nil, err } diff --git a/backend/test.http b/backend/test.http index 3b5fd95..12fa3f9 100644 --- a/backend/test.http +++ b/backend/test.http @@ -59,7 +59,7 @@ GET {{host}}/v1/admin/orders HTTP/1.1 Content-Type: application/json ### get orders -GET {{host}}/mine HTTP/1.1 +GET {{host}}/v1/posts/mine HTTP/1.1 Content-Type: application/json diff --git a/frontend/wechat/src/api/postApi.js b/frontend/wechat/src/api/postApi.js index cb96eb0..6395ce4 100644 --- a/frontend/wechat/src/api/postApi.js +++ b/frontend/wechat/src/api/postApi.js @@ -15,7 +15,7 @@ export const postApi = { return client.get(`/posts/${id}/play`); }, show(id) { - return client.get(`/posts/${id}`); + return client.get(`/posts/${id}/show`); }, mine({ page = 1, limit = 10 } = {}) { return client.get('/posts/mine', { diff --git a/frontend/wechat/src/views/ArticleDetail.vue b/frontend/wechat/src/views/ArticleDetail.vue index ca754d0..00050f6 100644 --- a/frontend/wechat/src/views/ArticleDetail.vue +++ b/frontend/wechat/src/views/ArticleDetail.vue @@ -114,6 +114,11 @@ onUnmounted(() => { +
@@ -125,6 +130,13 @@ onUnmounted(() => {

{{ article.title }}

+
+ + #{{ tag }} + +
+

{{ article.content }}

diff --git a/frontend/wechat/src/views/PurchasedArticles.vue b/frontend/wechat/src/views/PurchasedArticles.vue index f949403..99b94d7 100644 --- a/frontend/wechat/src/views/PurchasedArticles.vue +++ b/frontend/wechat/src/views/PurchasedArticles.vue @@ -17,14 +17,14 @@ const fetchArticles = async () => { if (loading.value) return loading.value = true try { - const response = await postApi.mine({ + const { data } = await postApi.mine({ page: currentPage.value, limit }) - if (response.data.data?.length === 0) { + if (data.items?.length === 0) { hasMore.value = false } else { - purchasedArticles.value.push(...response.data.data) + purchasedArticles.value.push(...data.items) currentPage.value += 1 } } catch (error) {