chore(ci, dto): update build workflow and enhance post DTOs
- Added NODE_NO_WARNINGS environment variable to build and deploy workflows. - Upgraded actions/upload-artifact and actions/download-artifact to version 4 in the build workflow. - Introduced ContentEditedAt field in PostResponse and PostDetailResponse DTOs to track content modification timestamps. - Updated post conversion functions to include ContentEditedAt in responses. - Ensured ContentEditedAt is set during post updates in the repository.
This commit is contained in:
@@ -73,6 +73,7 @@ func (r *CommentRepository) Delete(id string) error {
|
||||
UpdateColumns(map[string]interface{}{
|
||||
"comments_count": gorm.Expr("comments_count - 1"),
|
||||
"hot_score": gorm.Expr("likes_count * 2 + (comments_count - 1) * 3 + views_count * 0.1"),
|
||||
"updated_at": gorm.Expr("updated_at"),
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -102,6 +103,7 @@ func (r *CommentRepository) ApplyPublishedStats(comment *model.Comment) error {
|
||||
UpdateColumns(map[string]any{
|
||||
"comments_count": gorm.Expr("comments_count + 1"),
|
||||
"hot_score": gorm.Expr("likes_count * 2 + (comments_count + 1) * 3 + views_count * 0.1"),
|
||||
"updated_at": gorm.Expr("updated_at"),
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -61,7 +61,8 @@ func (r *PostRepository) Create(post *model.Post, images []string) error {
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
// 新建帖子:保证 updated_at 与 created_at 一致(避免历史 NULL/库默认值导致前端误判为已编辑)
|
||||
return tx.Model(post).Where("id = ?", post.ID).UpdateColumn("updated_at", post.CreatedAt).Error
|
||||
})
|
||||
}
|
||||
|
||||
@@ -77,14 +78,18 @@ func (r *PostRepository) GetByID(id string) (*model.Post, error) {
|
||||
|
||||
// Update 更新帖子
|
||||
func (r *PostRepository) Update(post *model.Post) error {
|
||||
post.UpdatedAt = time.Now()
|
||||
now := time.Now()
|
||||
post.UpdatedAt = now
|
||||
post.ContentEditedAt = &now
|
||||
return r.db.Save(post).Error
|
||||
}
|
||||
|
||||
// UpdateWithImages 更新帖子及其图片(images=nil 表示不更新图片)
|
||||
func (r *PostRepository) UpdateWithImages(post *model.Post, images *[]string) error {
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
post.UpdatedAt = time.Now()
|
||||
now := time.Now()
|
||||
post.UpdatedAt = now
|
||||
post.ContentEditedAt = &now
|
||||
if err := tx.Save(post).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -120,6 +125,7 @@ func (r *PostRepository) UpdateModerationStatus(postID string, status model.Post
|
||||
"reviewed_at": gorm.Expr("CURRENT_TIMESTAMP"),
|
||||
"reviewed_by": reviewedBy,
|
||||
"reject_reason": rejectReason,
|
||||
"updated_at": gorm.Expr("updated_at"), // 防止 MySQL ON UPDATE CURRENT_TIMESTAMP 误刷新
|
||||
}
|
||||
return r.db.Model(&model.Post{}).Where("id = ?", postID).UpdateColumns(updates).Error
|
||||
}
|
||||
@@ -258,6 +264,7 @@ func (r *PostRepository) Like(postID, userID string) error {
|
||||
UpdateColumns(map[string]any{
|
||||
"likes_count": gorm.Expr("likes_count + 1"),
|
||||
"hot_score": gorm.Expr("(likes_count + 1) * 2 + comments_count * 3 + views_count * 0.1"),
|
||||
"updated_at": gorm.Expr("updated_at"),
|
||||
}).Error
|
||||
})
|
||||
}
|
||||
@@ -276,6 +283,7 @@ func (r *PostRepository) Unlike(postID, userID string) error {
|
||||
UpdateColumns(map[string]any{
|
||||
"likes_count": gorm.Expr("likes_count - 1"),
|
||||
"hot_score": gorm.Expr("(likes_count - 1) * 2 + comments_count * 3 + views_count * 0.1"),
|
||||
"updated_at": gorm.Expr("updated_at"),
|
||||
}).Error
|
||||
}
|
||||
return nil
|
||||
@@ -338,9 +346,12 @@ func (r *PostRepository) Favorite(postID, userID string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// 增加帖子收藏数
|
||||
// 增加帖子收藏数(显式固定 updated_at,避免 MySQL ON UPDATE 误刷新)
|
||||
return tx.Model(&model.Post{}).Where("id = ?", postID).
|
||||
UpdateColumn("favorites_count", gorm.Expr("favorites_count + 1")).Error
|
||||
UpdateColumns(map[string]any{
|
||||
"favorites_count": gorm.Expr("favorites_count + 1"),
|
||||
"updated_at": gorm.Expr("updated_at"),
|
||||
}).Error
|
||||
})
|
||||
}
|
||||
|
||||
@@ -354,7 +365,10 @@ func (r *PostRepository) Unfavorite(postID, userID string) error {
|
||||
if result.RowsAffected > 0 {
|
||||
// 减少帖子收藏数
|
||||
return tx.Model(&model.Post{}).Where("id = ?", postID).
|
||||
UpdateColumn("favorites_count", gorm.Expr("favorites_count - 1")).Error
|
||||
UpdateColumns(map[string]any{
|
||||
"favorites_count": gorm.Expr("favorites_count - 1"),
|
||||
"updated_at": gorm.Expr("updated_at"),
|
||||
}).Error
|
||||
}
|
||||
return nil
|
||||
})
|
||||
@@ -401,6 +415,7 @@ func (r *PostRepository) IncrementViews(postID string) error {
|
||||
UpdateColumns(map[string]interface{}{
|
||||
"views_count": gorm.Expr("views_count + 1"),
|
||||
"hot_score": gorm.Expr("likes_count * 2 + comments_count * 3 + (views_count + 1) * 0.1"),
|
||||
"updated_at": gorm.Expr("updated_at"),
|
||||
}).Error
|
||||
}
|
||||
|
||||
@@ -537,7 +552,7 @@ func (r *PostRepository) createWithTx(tx *gorm.DB, post *model.Post, images []st
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return tx.Model(post).Where("id = ?", post.ID).UpdateColumn("updated_at", post.CreatedAt).Error
|
||||
}
|
||||
|
||||
// GetByIDWithContext 根据ID获取帖子(支持事务)
|
||||
@@ -552,7 +567,9 @@ func (r *PostRepository) GetByIDWithContext(ctx context.Context, id string) (*mo
|
||||
|
||||
// UpdateWithContext 更新帖子(支持事务)
|
||||
func (r *PostRepository) UpdateWithContext(ctx context.Context, post *model.Post) error {
|
||||
post.UpdatedAt = time.Now()
|
||||
now := time.Now()
|
||||
post.UpdatedAt = now
|
||||
post.ContentEditedAt = &now
|
||||
return r.getDB(ctx).WithContext(ctx).Save(post).Error
|
||||
}
|
||||
|
||||
@@ -706,14 +723,20 @@ func (r *PostRepository) BatchUpdateStatus(ids []string, status model.PostStatus
|
||||
func (r *PostRepository) UpdatePinStatus(postID string, isPinned bool) error {
|
||||
// 置顶状态属于管理操作,不应影响帖子内容更新时间(updated_at)
|
||||
return r.db.Model(&model.Post{}).Where("id = ?", postID).
|
||||
UpdateColumn("is_pinned", isPinned).Error
|
||||
UpdateColumns(map[string]any{
|
||||
"is_pinned": isPinned,
|
||||
"updated_at": gorm.Expr("updated_at"),
|
||||
}).Error
|
||||
}
|
||||
|
||||
// UpdateFeatureStatus 更新帖子加精状态
|
||||
func (r *PostRepository) UpdateFeatureStatus(postID string, isFeatured bool) error {
|
||||
// 加精状态属于管理操作,不应影响帖子内容更新时间(updated_at)
|
||||
return r.db.Model(&model.Post{}).Where("id = ?", postID).
|
||||
UpdateColumn("is_featured", isFeatured).Error
|
||||
UpdateColumns(map[string]any{
|
||||
"is_featured": isFeatured,
|
||||
"updated_at": gorm.Expr("updated_at"),
|
||||
}).Error
|
||||
}
|
||||
|
||||
// ========== Cursor Pagination Methods ==========
|
||||
|
||||
Reference in New Issue
Block a user