feat: 添加内容置顶功能,更新相关数据模型和前端视图

This commit is contained in:
2026-01-05 12:54:26 +08:00
parent a5cc9b13d6
commit c231847f81
8 changed files with 278 additions and 176 deletions

View File

@@ -41,6 +41,7 @@ type ContentUpdateForm struct {
Key string `json:"key"`
Price *float64 `json:"price"`
Status string `json:"status"`
IsPinned *bool `json:"is_pinned"`
CoverIDs []string `json:"cover_ids"`
MediaIDs []string `json:"media_ids"`
}
@@ -71,6 +72,7 @@ type CreatorContentItem struct {
VideoCount int `json:"video_count"`
AudioCount int `json:"audio_count"`
Status string `json:"status"`
IsPinned bool `json:"is_pinned"`
IsPurchased bool `json:"is_purchased"`
}

View File

@@ -9,9 +9,9 @@ type UploadInitForm struct {
}
type UploadInitResponse struct {
UploadID string `json:"upload_id"`
Key string `json:"key"` // For S3 direct
ChunkSize int64 `json:"chunk_size"`
UploadID string `json:"upload_id"`
Key string `json:"key"` // For S3 direct
ChunkSize int64 `json:"chunk_size"`
}
type UploadPartForm struct {

View File

@@ -101,7 +101,7 @@ func (s *common) InitUpload(ctx context.Context, userID int64, form *common_dto.
localPath = "./storage"
}
tempDir := filepath.Join(localPath, "temp", uploadID)
if err := os.MkdirAll(tempDir, 0755); err != nil {
if err := os.MkdirAll(tempDir, 0o755); err != nil {
return nil, errorx.ErrInternalError.WithCause(err)
}
@@ -205,7 +205,7 @@ func (s *common) CompleteUpload(ctx context.Context, userID int64, form *common_
}
hash := hex.EncodeToString(hasher.Sum(nil))
dst.Close(); // Ensure flush before potential removal
dst.Close() // Ensure flush before potential removal
os.RemoveAll(tempDir)
// Deduplication Logic (Similar to Upload)

View File

@@ -3,6 +3,7 @@ package services
import (
"context"
"errors"
"fmt"
"time"
"quyun/v2/app/errorx"
@@ -129,6 +130,7 @@ func (s *creator) ListContents(
}
}
if filter.Key != nil && *filter.Key != "" {
fmt.Printf("DEBUG: Filter Key: '%s'\n", *filter.Key)
q = q.Where(tbl.Key.Eq(*filter.Key))
}
if filter.Keyword != nil && *filter.Keyword != "" {
@@ -205,6 +207,7 @@ func (s *creator) ListContents(
VideoCount: videoCount,
AudioCount: audioCount,
Status: string(item.Status),
IsPinned: item.IsPinned,
IsPurchased: false,
})
}
@@ -311,11 +314,48 @@ func (s *creator) UpdateContent(
if form.Status != "" {
contentUpdates.Status = consts.ContentStatus(form.Status)
}
// Determine final status
finalStatus := c.Status
if form.Status != "" {
finalStatus = consts.ContentStatus(form.Status)
}
// Validation: Only published content can be pinned
if form.IsPinned != nil && *form.IsPinned && finalStatus != consts.ContentStatusPublished {
return errorx.ErrBadRequest.WithMsg("只有已发布的内容支持置顶")
}
// Perform standard updates
_, err = tx.Content.WithContext(ctx).Where(tx.Content.ID.Eq(cid)).Updates(contentUpdates)
if err != nil {
return err
}
// Handle IsPinned Logic
if finalStatus != consts.ContentStatusPublished {
// Force Unpin if not published
_, err = tx.Content.WithContext(ctx).Where(tx.Content.ID.Eq(cid)).UpdateSimple(tx.Content.IsPinned.Value(false))
if err != nil {
return err
}
} else if form.IsPinned != nil {
// Explicit Pin Update requested
_, err = tx.Content.WithContext(ctx).Where(tx.Content.ID.Eq(cid)).UpdateSimple(tx.Content.IsPinned.Value(*form.IsPinned))
if err != nil {
return err
}
// If setting to true, unpin others
if *form.IsPinned {
if _, err := tx.Content.WithContext(ctx).
Where(tx.Content.TenantID.Eq(tid), tx.Content.ID.Neq(cid)).
UpdateSimple(tx.Content.IsPinned.Value(false)); err != nil {
return err
}
}
}
// 3. Update Price
// Check if price exists
if form.Price != nil {

View File

@@ -0,0 +1,6 @@
-- +goose Up
ALTER TABLE contents ADD COLUMN is_pinned BOOLEAN DEFAULT FALSE;
COMMENT ON COLUMN contents.is_pinned IS 'Whether content is pinned/featured';
-- +goose Down
ALTER TABLE contents DROP COLUMN is_pinned;

View File

@@ -38,10 +38,11 @@ type Content struct {
CreatedAt time.Time `gorm:"column:created_at;type:timestamp with time zone;default:now()" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp with time zone;default:now()" json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:timestamp with time zone" json:"deleted_at"`
Key string `gorm:"column:key;type:character varying(32);comment:Musical key/tone" json:"key"` // Musical key/tone
ContentAssets []*ContentAsset `gorm:"foreignKey:ContentID;references:ID" json:"content_assets,omitempty"`
Key string `gorm:"column:key;type:character varying(32);comment:Musical key/tone" json:"key"` // Musical key/tone
IsPinned bool `gorm:"column:is_pinned;type:boolean;comment:Whether content is pinned/featured" json:"is_pinned"` // Whether content is pinned/featured
Comments []*Comment `gorm:"foreignKey:ContentID;references:ID" json:"comments,omitempty"`
Author *User `gorm:"foreignKey:UserID;references:ID" json:"author,omitempty"`
ContentAssets []*ContentAsset `gorm:"foreignKey:ContentID;references:ID" json:"content_assets,omitempty"`
}
// Quick operations without importing query package

View File

@@ -45,12 +45,7 @@ func newContent(db *gorm.DB, opts ...gen.DOOption) contentQuery {
_contentQuery.UpdatedAt = field.NewTime(tableName, "updated_at")
_contentQuery.DeletedAt = field.NewField(tableName, "deleted_at")
_contentQuery.Key = field.NewString(tableName, "key")
_contentQuery.ContentAssets = contentQueryHasManyContentAssets{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("ContentAssets", "ContentAsset"),
}
_contentQuery.IsPinned = field.NewBool(tableName, "is_pinned")
_contentQuery.Comments = contentQueryHasManyComments{
db: db.Session(&gorm.Session{}),
@@ -63,6 +58,12 @@ func newContent(db *gorm.DB, opts ...gen.DOOption) contentQuery {
RelationField: field.NewRelation("Author", "User"),
}
_contentQuery.ContentAssets = contentQueryHasManyContentAssets{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("ContentAssets", "ContentAsset"),
}
_contentQuery.fillFieldMap()
return _contentQuery
@@ -92,12 +93,13 @@ type contentQuery struct {
UpdatedAt field.Time
DeletedAt field.Field
Key field.String // Musical key/tone
ContentAssets contentQueryHasManyContentAssets
Comments contentQueryHasManyComments
IsPinned field.Bool // Whether content is pinned/featured
Comments contentQueryHasManyComments
Author contentQueryBelongsToAuthor
ContentAssets contentQueryHasManyContentAssets
fieldMap map[string]field.Expr
}
@@ -133,6 +135,7 @@ func (c *contentQuery) updateTableName(table string) *contentQuery {
c.UpdatedAt = field.NewTime(table, "updated_at")
c.DeletedAt = field.NewField(table, "deleted_at")
c.Key = field.NewString(table, "key")
c.IsPinned = field.NewBool(table, "is_pinned")
c.fillFieldMap()
@@ -165,7 +168,7 @@ func (c *contentQuery) GetFieldByName(fieldName string) (field.OrderExpr, bool)
}
func (c *contentQuery) fillFieldMap() {
c.fieldMap = make(map[string]field.Expr, 23)
c.fieldMap = make(map[string]field.Expr, 24)
c.fieldMap["id"] = c.ID
c.fieldMap["tenant_id"] = c.TenantID
c.fieldMap["user_id"] = c.UserID
@@ -186,109 +189,29 @@ func (c *contentQuery) fillFieldMap() {
c.fieldMap["updated_at"] = c.UpdatedAt
c.fieldMap["deleted_at"] = c.DeletedAt
c.fieldMap["key"] = c.Key
c.fieldMap["is_pinned"] = c.IsPinned
}
func (c contentQuery) clone(db *gorm.DB) contentQuery {
c.contentQueryDo.ReplaceConnPool(db.Statement.ConnPool)
c.ContentAssets.db = db.Session(&gorm.Session{Initialized: true})
c.ContentAssets.db.Statement.ConnPool = db.Statement.ConnPool
c.Comments.db = db.Session(&gorm.Session{Initialized: true})
c.Comments.db.Statement.ConnPool = db.Statement.ConnPool
c.Author.db = db.Session(&gorm.Session{Initialized: true})
c.Author.db.Statement.ConnPool = db.Statement.ConnPool
c.ContentAssets.db = db.Session(&gorm.Session{Initialized: true})
c.ContentAssets.db.Statement.ConnPool = db.Statement.ConnPool
return c
}
func (c contentQuery) replaceDB(db *gorm.DB) contentQuery {
c.contentQueryDo.ReplaceDB(db)
c.ContentAssets.db = db.Session(&gorm.Session{})
c.Comments.db = db.Session(&gorm.Session{})
c.Author.db = db.Session(&gorm.Session{})
c.ContentAssets.db = db.Session(&gorm.Session{})
return c
}
type contentQueryHasManyContentAssets struct {
db *gorm.DB
field.RelationField
}
func (a contentQueryHasManyContentAssets) Where(conds ...field.Expr) *contentQueryHasManyContentAssets {
if len(conds) == 0 {
return &a
}
exprs := make([]clause.Expression, 0, len(conds))
for _, cond := range conds {
exprs = append(exprs, cond.BeCond().(clause.Expression))
}
a.db = a.db.Clauses(clause.Where{Exprs: exprs})
return &a
}
func (a contentQueryHasManyContentAssets) WithContext(ctx context.Context) *contentQueryHasManyContentAssets {
a.db = a.db.WithContext(ctx)
return &a
}
func (a contentQueryHasManyContentAssets) Session(session *gorm.Session) *contentQueryHasManyContentAssets {
a.db = a.db.Session(session)
return &a
}
func (a contentQueryHasManyContentAssets) Model(m *Content) *contentQueryHasManyContentAssetsTx {
return &contentQueryHasManyContentAssetsTx{a.db.Model(m).Association(a.Name())}
}
func (a contentQueryHasManyContentAssets) Unscoped() *contentQueryHasManyContentAssets {
a.db = a.db.Unscoped()
return &a
}
type contentQueryHasManyContentAssetsTx struct{ tx *gorm.Association }
func (a contentQueryHasManyContentAssetsTx) Find() (result []*ContentAsset, err error) {
return result, a.tx.Find(&result)
}
func (a contentQueryHasManyContentAssetsTx) Append(values ...*ContentAsset) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Append(targetValues...)
}
func (a contentQueryHasManyContentAssetsTx) Replace(values ...*ContentAsset) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Replace(targetValues...)
}
func (a contentQueryHasManyContentAssetsTx) Delete(values ...*ContentAsset) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Delete(targetValues...)
}
func (a contentQueryHasManyContentAssetsTx) Clear() error {
return a.tx.Clear()
}
func (a contentQueryHasManyContentAssetsTx) Count() int64 {
return a.tx.Count()
}
func (a contentQueryHasManyContentAssetsTx) Unscoped() *contentQueryHasManyContentAssetsTx {
a.tx = a.tx.Unscoped()
return &a
}
type contentQueryHasManyComments struct {
db *gorm.DB
@@ -451,6 +374,87 @@ func (a contentQueryBelongsToAuthorTx) Unscoped() *contentQueryBelongsToAuthorTx
return &a
}
type contentQueryHasManyContentAssets struct {
db *gorm.DB
field.RelationField
}
func (a contentQueryHasManyContentAssets) Where(conds ...field.Expr) *contentQueryHasManyContentAssets {
if len(conds) == 0 {
return &a
}
exprs := make([]clause.Expression, 0, len(conds))
for _, cond := range conds {
exprs = append(exprs, cond.BeCond().(clause.Expression))
}
a.db = a.db.Clauses(clause.Where{Exprs: exprs})
return &a
}
func (a contentQueryHasManyContentAssets) WithContext(ctx context.Context) *contentQueryHasManyContentAssets {
a.db = a.db.WithContext(ctx)
return &a
}
func (a contentQueryHasManyContentAssets) Session(session *gorm.Session) *contentQueryHasManyContentAssets {
a.db = a.db.Session(session)
return &a
}
func (a contentQueryHasManyContentAssets) Model(m *Content) *contentQueryHasManyContentAssetsTx {
return &contentQueryHasManyContentAssetsTx{a.db.Model(m).Association(a.Name())}
}
func (a contentQueryHasManyContentAssets) Unscoped() *contentQueryHasManyContentAssets {
a.db = a.db.Unscoped()
return &a
}
type contentQueryHasManyContentAssetsTx struct{ tx *gorm.Association }
func (a contentQueryHasManyContentAssetsTx) Find() (result []*ContentAsset, err error) {
return result, a.tx.Find(&result)
}
func (a contentQueryHasManyContentAssetsTx) Append(values ...*ContentAsset) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Append(targetValues...)
}
func (a contentQueryHasManyContentAssetsTx) Replace(values ...*ContentAsset) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Replace(targetValues...)
}
func (a contentQueryHasManyContentAssetsTx) Delete(values ...*ContentAsset) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Delete(targetValues...)
}
func (a contentQueryHasManyContentAssetsTx) Clear() error {
return a.tx.Clear()
}
func (a contentQueryHasManyContentAssetsTx) Count() int64 {
return a.tx.Count()
}
func (a contentQueryHasManyContentAssetsTx) Unscoped() *contentQueryHasManyContentAssetsTx {
a.tx = a.tx.Unscoped()
return &a
}
type contentQueryDo struct{ gen.DO }
func (c contentQueryDo) Debug() *contentQueryDo {