feat: update player

This commit is contained in:
yanghao05
2025-04-29 11:24:54 +08:00
parent ee21908887
commit c9740e6403
7 changed files with 81 additions and 14 deletions

View File

@@ -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

View File

@@ -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(

View File

@@ -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
}

View File

@@ -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

View File

@@ -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', {

View File

@@ -114,6 +114,11 @@ onUnmounted(() => {
<button @click="handleBack" class="text-gray-700">
<BsChevronLeft class="w-6 h-6" />
</button>
<template v-if="article">
<h1 class="text-lg font-semibold text-gray-800 ml-4">
{{ article.title }}
</h1>
</template>
</div>
<div class="flex-1">
@@ -125,6 +130,13 @@ onUnmounted(() => {
</video>
<div class="p-4">
<h1 class="text-2xl my-1.5">{{ article.title }}</h1>
<div class="py-2 flex items-center gap-2">
<span v-for="tag in article.tags" :key="tag"
class="px-1.5 py-0.5 text-xs bg-gray-100 text-gray-600 rounded">
<span class="mr-1">#</span>{{ tag }}
</span>
</div>
<p class="text-gray-600">{{ article.content }}</p>
</div>
</div>

View File

@@ -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) {