主要变更: - 采用 go-zero 框架替代 Gin,提升开发效率 - 重构项目结构,API 文件模块化组织 - 将 model 移至 api/internal/model 目录 - 移除 common 包,改为标准 pkg 目录结构 - 实现统一的仓储模式,支持配置驱动数据库切换 - 简化测试策略,专注 API 集成测试 - 更新 CLAUDE.md 文档,提供详细的开发指导 技术栈更新: - 框架: Gin → go-zero v1.6.0+ - 代码生成: 引入 goctl 工具 - 架构模式: 四层架构 → go-zero 三层架构 (Handler→Logic→Model) - 项目布局: 遵循 Go 社区标准和 go-zero 最佳实践
84 lines
2.9 KiB
Go
84 lines
2.9 KiB
Go
package entity
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Album 相册实体
|
|
type Album struct {
|
|
ID uint `json:"id" gorm:"primarykey"`
|
|
Title string `json:"title" gorm:"not null;size:200"`
|
|
Description string `json:"description" gorm:"type:text"`
|
|
Slug string `json:"slug" gorm:"uniqueIndex;size:255"`
|
|
CoverPhotoID *uint `json:"cover_photo_id" gorm:"index"`
|
|
UserID uint `json:"user_id" gorm:"not null;index"`
|
|
CategoryID *uint `json:"category_id" gorm:"index"`
|
|
IsPublic bool `json:"is_public" gorm:"default:true;index"`
|
|
IsFeatured bool `json:"is_featured" gorm:"default:false;index"`
|
|
Password string `json:"-" gorm:"size:255"` // 私密相册密码
|
|
ViewCount int `json:"view_count" gorm:"default:0;index"`
|
|
LikeCount int `json:"like_count" gorm:"default:0;index"`
|
|
PhotoCount int `json:"photo_count" gorm:"default:0;index"`
|
|
SortOrder int `json:"sort_order" gorm:"default:0;index"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
|
|
|
|
// 关联
|
|
User User `json:"user,omitempty" gorm:"foreignKey:UserID"`
|
|
Category *Category `json:"category,omitempty" gorm:"foreignKey:CategoryID"`
|
|
CoverPhoto *Photo `json:"cover_photo,omitempty" gorm:"foreignKey:CoverPhotoID"`
|
|
Photos []Photo `json:"photos,omitempty" gorm:"foreignKey:AlbumID"`
|
|
}
|
|
|
|
// AlbumStats 相册统计信息
|
|
type AlbumStats struct {
|
|
Total int64 `json:"total"` // 总相册数
|
|
Published int64 `json:"published"` // 已发布相册数
|
|
Private int64 `json:"private"` // 私有相册数
|
|
Featured int64 `json:"featured"` // 精选相册数
|
|
TotalViews int64 `json:"total_views"` // 总浏览量
|
|
TotalLikes int64 `json:"total_likes"` // 总点赞数
|
|
TotalPhotos int64 `json:"total_photos"` // 总照片数
|
|
CategoryCounts map[string]int64 `json:"category_counts"` // 各分类相册数量
|
|
}
|
|
|
|
// TableName 指定表名
|
|
func (Album) TableName() string {
|
|
return "albums"
|
|
}
|
|
|
|
// HasPassword 检查是否设置了密码
|
|
func (a *Album) HasPassword() bool {
|
|
return a.Password != ""
|
|
}
|
|
|
|
// IsEmpty 检查相册是否为空
|
|
func (a *Album) IsEmpty() bool {
|
|
return a.PhotoCount == 0
|
|
}
|
|
|
|
// CanViewBy 检查指定用户是否可以查看相册
|
|
func (a *Album) CanViewBy(user *User) bool {
|
|
// 公开相册
|
|
if a.IsPublic && !a.HasPassword() {
|
|
return true
|
|
}
|
|
|
|
// 相册所有者或管理员
|
|
if user != nil && (user.ID == a.UserID || user.IsAdmin()) {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// CanEditBy 检查指定用户是否可以编辑相册
|
|
func (a *Album) CanEditBy(user *User) bool {
|
|
if user == nil {
|
|
return false
|
|
}
|
|
return user.ID == a.UserID || user.IsAdmin()
|
|
} |