Files
neo-blog/internal/model/like.go
Snowykami dd7641bf6e
All checks were successful
Push to Helm Chart Repository / build (push) Successful in 13s
feat: implement advanced comment features including reply and like functionality
- Added support for nested comments with reply functionality.
- Implemented like/unlike feature for comments and posts.
- Enhanced comment DTO to include reply count, like count, and like status.
- Updated comment and like services to handle new functionalities.
- Created new API endpoints for toggling likes and listing comments.
- Improved UI components for comments to support replies and likes with animations.
- Added localization for new comment-related messages.
- Introduced a TODO list for future enhancements in the comment module.
2025-09-09 00:24:25 +08:00

44 lines
1.2 KiB
Go

package model
import (
"fmt"
"github.com/snowykami/neo-blog/pkg/constant"
"gorm.io/gorm"
)
type Like struct {
gorm.Model
TargetType string
TargetID uint
UserID uint
}
// AfterCreate 点赞后更新被点赞对象的计数
func (l *Like) AfterCreate(tx *gorm.DB) (err error) {
switch l.TargetType {
case constant.TargetTypePost:
return tx.Model(&Post{}).Where("id = ?", l.TargetID).
UpdateColumn("like_count", gorm.Expr("like_count + ?", 1)).Error
case constant.TargetTypeComment:
return tx.Model(&Comment{}).Where("id = ?", l.TargetID).
UpdateColumn("like_count", gorm.Expr("like_count + ?", 1)).Error
default:
return fmt.Errorf("不支持的目标类型: %s", l.TargetType)
}
}
// AfterDelete 取消点赞后更新被点赞对象的计数
func (l *Like) AfterDelete(tx *gorm.DB) (err error) {
switch l.TargetType {
case constant.TargetTypePost:
return tx.Model(&Post{}).Where("id = ?", l.TargetID).
UpdateColumn("like_count", gorm.Expr("like_count - ?", 1)).Error
case constant.TargetTypeComment:
return tx.Model(&Comment{}).Where("id = ?", l.TargetID).
UpdateColumn("like_count", gorm.Expr("like_count - ?", 1)).Error
default:
return fmt.Errorf("不支持的目标类型: %s", l.TargetType)
}
}