Files
neo-blog/internal/model/post.go
Snowykami 88166a2c7d feat: add sidebar component with context and mobile support
- 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.
2025-09-14 23:52:18 +08:00

74 lines
2.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package model
import (
"github.com/snowykami/neo-blog/internal/dto"
"github.com/snowykami/neo-blog/pkg/constant"
"gorm.io/gorm"
)
type Post struct {
gorm.Model
UserID uint `gorm:"index"` // 发布者的用户ID
User User `gorm:"foreignKey:UserID;references:ID"` // 关联的用户
Title string `gorm:"type:text;not null"` // 帖子标题
Cover string `gorm:"type:text"` // 帖子封面图
Content string `gorm:"type:text;not null"` // 帖子内容
Type string `gorm:"type:text;default:markdown"` // markdown类型支持markdown或html或txt
CategoryID uint `gorm:"index"` // 帖子分类ID
Category Category `gorm:"foreignKey:CategoryID;references:ID"` // 关联的分类
Labels []Label `gorm:"many2many:post_labels;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` // 关联的标签
IsOriginal bool `gorm:"default:true"` // 是否为原创帖子
IsPrivate bool `gorm:"default:false"`
LikeCount uint64
CommentCount uint64
ViewCount uint64
Heat uint64
}
// CalculateHeat 热度计算
func (p *Post) CalculateHeat() float64 {
return float64(
p.LikeCount*constant.HeatFactorLikeWeight +
p.CommentCount*constant.HeatFactorCommentWeight +
p.ViewCount*constant.HeatFactorViewWeight,
)
}
func (p *Post) ToDto() *dto.PostDto {
return &dto.PostDto{
ID: p.ID,
UserID: p.UserID,
Title: p.Title,
Content: p.Content,
Cover: p.Cover,
Type: p.Type,
IsOriginal: p.IsOriginal,
IsPrivate: p.IsPrivate,
Labels: func() []dto.LabelDto {
labelDtos := make([]dto.LabelDto, len(p.Labels))
for i, label := range p.Labels {
labelDtos[i] = label.ToDto()
}
return labelDtos
}(),
LikeCount: p.LikeCount,
CommentCount: p.CommentCount,
ViewCount: p.ViewCount,
Heat: uint64(p.CalculateHeat()),
CreatedAt: p.CreatedAt,
UpdatedAt: p.UpdatedAt,
User: p.User.ToDto(),
}
}
// ToDtoWithShortContent 返回一个简化的 DTO内容可以根据需要截断
func (p *Post) ToDtoWithShortContent(contentLength int) *dto.PostDto {
dtoPost := p.ToDto()
if len(p.Content) > contentLength {
dtoPost.Content = p.Content[:contentLength] + "..."
} else {
dtoPost.Content = p.Content
}
return dtoPost
}