mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-26 19:16:24 +00:00
- Implemented Sidebar component with collapsible functionality. - Added SidebarProvider for managing open state and keyboard shortcuts. - Created SidebarTrigger, SidebarRail, and various sidebar elements (Header, Footer, Content, etc.). - Integrated mobile responsiveness using Sheet component. - Added utility hooks for mobile detection. feat: create table component for structured data display - Developed Table component with subcomponents: TableHeader, TableBody, TableFooter, TableRow, TableCell, and TableCaption. - Enhanced styling for better readability and usability. feat: implement tabs component for navigation - Created Tabs component with TabsList, TabsTrigger, and TabsContent for tabbed navigation. - Ensured accessibility and responsive design. feat: add toggle group component for grouped toggle buttons - Developed ToggleGroup and ToggleGroupItem components for managing toggle states. - Integrated context for consistent styling and behavior. feat: create toggle component for binary state representation - Implemented Toggle component with variant and size options. - Enhanced user interaction with visual feedback. feat: add tooltip component for contextual information - Developed Tooltip, TooltipTrigger, and TooltipContent for displaying additional information on hover. - Integrated animations for a smoother user experience. feat: implement mobile detection hook - Created useIsMobile hook to determine if the user is on a mobile device. - Utilized matchMedia for responsive design adjustments.
54 lines
1.5 KiB
Go
54 lines
1.5 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:
|
|
// 点赞数+1
|
|
if err := tx.Model(&Post{}).Where("id = ?", l.TargetID).
|
|
UpdateColumn("like_count", gorm.Expr("like_count + ?", 1)).Error; err != nil {
|
|
return err
|
|
}
|
|
// 查询最新 Post
|
|
var post Post
|
|
if err := tx.First(&post, l.TargetID).Error; err != nil {
|
|
return err
|
|
}
|
|
// 更新热度
|
|
return tx.Model(&post).UpdateColumn("heat", post.CalculateHeat()).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)
|
|
}
|
|
}
|