数据结构优化
This commit is contained in:
@@ -1,38 +1,102 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Work 作品模型
|
||||
type Work struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Category string `json:"category"`
|
||||
Year string `json:"year"`
|
||||
HeroImg string `json:"heroImg"`
|
||||
Description string `json:"desc"`
|
||||
IsFeatured int `json:"isFeatured"`
|
||||
CreatedAt int64 `json:"createdAt"`
|
||||
UpdatedAt int64 `json:"updatedAt"`
|
||||
DeletedAt int64 `json:"deletedAt"`
|
||||
ID string `json:"id" gorm:"primaryKey;column:id"`
|
||||
Title string `json:"title" gorm:"column:title"`
|
||||
Category string `json:"category" gorm:"column:category"`
|
||||
Year string `json:"year" gorm:"column:year"`
|
||||
HeroImg string `json:"heroImg" gorm:"column:hero_img"`
|
||||
Description string `json:"desc" gorm:"column:description;type:text"`
|
||||
IsFeatured int `json:"isFeatured" gorm:"column:is_featured;default:0"`
|
||||
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
|
||||
UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
|
||||
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
func (Work) TableName() string {
|
||||
return "works"
|
||||
}
|
||||
|
||||
// BeforeCreate 创建前钩子
|
||||
func (w *Work) BeforeCreate(tx *gorm.DB) error {
|
||||
now := time.Now().Unix()
|
||||
if w.CreatedAt == 0 {
|
||||
w.CreatedAt = now
|
||||
}
|
||||
if w.UpdatedAt == 0 {
|
||||
w.UpdatedAt = now
|
||||
}
|
||||
if w.DeletedAt == 0 {
|
||||
w.DeletedAt = 0
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// BeforeUpdate 更新前钩子
|
||||
func (w *Work) BeforeUpdate(tx *gorm.DB) error {
|
||||
w.UpdatedAt = time.Now().Unix()
|
||||
return nil
|
||||
}
|
||||
|
||||
// WorkTechStack 作品技术栈模型
|
||||
type WorkTechStack struct {
|
||||
ID uint `json:"id"`
|
||||
WorkID string `json:"workId"`
|
||||
Category string `json:"category"`
|
||||
Item string `json:"item"`
|
||||
CreatedAt int64 `json:"createdAt"`
|
||||
DeletedAt int64 `json:"deletedAt"`
|
||||
ID uint `json:"id" gorm:"primaryKey;column:id"`
|
||||
WorkID string `json:"workId" gorm:"column:work_id;index"`
|
||||
Category string `json:"category" gorm:"column:category"`
|
||||
Item string `json:"item" gorm:"column:item"`
|
||||
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
|
||||
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
func (WorkTechStack) TableName() string {
|
||||
return "work_tech_stack"
|
||||
}
|
||||
|
||||
// BeforeCreate 创建前钩子
|
||||
func (wts *WorkTechStack) BeforeCreate(tx *gorm.DB) error {
|
||||
if wts.CreatedAt == 0 {
|
||||
wts.CreatedAt = time.Now().Unix()
|
||||
}
|
||||
if wts.DeletedAt == 0 {
|
||||
wts.DeletedAt = 0
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WorkGallery 作品图库模型
|
||||
type WorkGallery struct {
|
||||
ID uint `json:"id"`
|
||||
WorkID string `json:"workId"`
|
||||
ImageURL string `json:"imageUrl"`
|
||||
SortOrder uint `json:"sortOrder"`
|
||||
Description string `json:"description"`
|
||||
CreatedAt int64 `json:"createdAt"`
|
||||
DeletedAt int64 `json:"deletedAt"`
|
||||
ID uint `json:"id" gorm:"primaryKey;column:id"`
|
||||
WorkID string `json:"workId" gorm:"column:work_id;index"`
|
||||
ImageURL string `json:"imageUrl" gorm:"column:image_url"`
|
||||
SortOrder uint `json:"sortOrder" gorm:"column:sort_order;default:0"`
|
||||
Description string `json:"description" gorm:"column:description"`
|
||||
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
|
||||
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
func (WorkGallery) TableName() string {
|
||||
return "work_gallery"
|
||||
}
|
||||
|
||||
// BeforeCreate 创建前钩子
|
||||
func (wg *WorkGallery) BeforeCreate(tx *gorm.DB) error {
|
||||
if wg.CreatedAt == 0 {
|
||||
wg.CreatedAt = time.Now().Unix()
|
||||
}
|
||||
if wg.DeletedAt == 0 {
|
||||
wg.DeletedAt = 0
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WorkResponse 作品响应模型,包含关联数据
|
||||
|
||||
Reference in New Issue
Block a user