feat: 更新内容管理功能,支持价格为可选字段,添加状态管理及媒体计数显示

This commit is contained in:
2026-01-04 19:56:53 +08:00
parent 581cbc3991
commit 4b32e64d35
3 changed files with 106 additions and 26 deletions

View File

@@ -38,7 +38,8 @@ type ContentUpdateForm struct {
Title string `json:"title"`
Genre string `json:"genre"`
Key string `json:"key"`
Price float64 `json:"price"`
Price *float64 `json:"price"`
Status string `json:"status"`
CoverIDs []string `json:"cover_ids"`
MediaIDs []string `json:"media_ids"`
}
@@ -64,6 +65,11 @@ type CreatorContentItem struct {
Price float64 `json:"price"`
Views int `json:"views"`
Likes int `json:"likes"`
Cover string `json:"cover"`
ImageCount int `json:"image_count"`
VideoCount int `json:"video_count"`
AudioCount int `json:"audio_count"`
Status string `json:"status"`
IsPurchased bool `json:"is_purchased"`
}

View File

@@ -117,7 +117,12 @@ func (s *creator) ListContents(
q = q.Where(tbl.Title.Like("%" + *filter.Keyword + "%"))
}
list, err := q.Order(tbl.CreatedAt.Desc()).Find()
var list []*models.Content
err = q.Order(tbl.CreatedAt.Desc()).
UnderlyingDB().
Preload("ContentAssets").
Preload("ContentAssets.Asset").
Find(&list).Error
if err != nil {
return nil, errorx.ErrDatabaseError.WithCause(err)
}
@@ -137,6 +142,38 @@ func (s *creator) ListContents(
var data []creator_dto.CreatorContentItem
for _, item := range list {
var imageCount, videoCount, audioCount int
var cover string
var firstImage string
for _, ca := range item.ContentAssets {
if ca.Asset == nil {
continue
}
// Count logic
switch ca.Asset.Type {
case consts.MediaAssetTypeImage:
imageCount++
if firstImage == "" {
firstImage = Common.GetAssetURL(ca.Asset.ObjectKey)
}
case consts.MediaAssetTypeVideo:
videoCount++
case consts.MediaAssetTypeAudio:
audioCount++
}
// Cover logic
if ca.Role == consts.ContentAssetRoleCover && cover == "" {
cover = Common.GetAssetURL(ca.Asset.ObjectKey)
}
}
if cover == "" {
cover = firstImage
}
data = append(data, creator_dto.CreatorContentItem{
ID: cast.ToString(item.ID),
Title: item.Title,
@@ -145,6 +182,11 @@ func (s *creator) ListContents(
Price: priceMap[item.ID],
Views: int(item.Views),
Likes: int(item.Likes),
Cover: cover,
ImageCount: imageCount,
VideoCount: videoCount,
AudioCount: audioCount,
Status: string(item.Status),
IsPurchased: false,
})
}
@@ -239,34 +281,40 @@ func (s *creator) UpdateContent(
}
// 2. Update Content
_, err = tx.Content.WithContext(ctx).Where(tx.Content.ID.Eq(cid)).Updates(&models.Content{
contentUpdates := &models.Content{
Title: form.Title,
Genre: form.Genre,
Key: form.Key,
})
}
if form.Status != "" {
contentUpdates.Status = consts.ContentStatus(form.Status)
}
_, err = tx.Content.WithContext(ctx).Where(tx.Content.ID.Eq(cid)).Updates(contentUpdates)
if err != nil {
return err
}
// 3. Update Price
// Check if price exists
count, _ := tx.ContentPrice.WithContext(ctx).Where(tx.ContentPrice.ContentID.Eq(cid)).Count()
newPrice := int64(form.Price * 100)
if count > 0 {
_, err = tx.ContentPrice.WithContext(ctx).
Where(tx.ContentPrice.ContentID.Eq(cid)).
UpdateSimple(tx.ContentPrice.PriceAmount.Value(newPrice))
} else {
err = tx.ContentPrice.WithContext(ctx).Create(&models.ContentPrice{
TenantID: tid,
UserID: c.UserID,
ContentID: cid,
PriceAmount: newPrice,
Currency: consts.CurrencyCNY,
})
}
if err != nil {
return err
if form.Price != nil {
count, _ := tx.ContentPrice.WithContext(ctx).Where(tx.ContentPrice.ContentID.Eq(cid)).Count()
newPrice := int64(*form.Price * 100)
if count > 0 {
_, err = tx.ContentPrice.WithContext(ctx).
Where(tx.ContentPrice.ContentID.Eq(cid)).
UpdateSimple(tx.ContentPrice.PriceAmount.Value(newPrice))
} else {
err = tx.ContentPrice.WithContext(ctx).Create(&models.ContentPrice{
TenantID: tid,
UserID: c.UserID,
ContentID: cid,
PriceAmount: newPrice,
Currency: consts.CurrencyCNY,
})
}
if err != nil {
return err
}
}
// 4. Update Assets (Full replacement strategy)