Files
nl-blogs/server/models/about.go
2026-01-19 13:53:32 +08:00

59 lines
1.9 KiB
Go

package models
import (
"time"
"gorm.io/gorm"
)
type Experience struct {
Year string `json:"year"`
Role string `json:"role"`
Company string `json:"company"`
}
// AboutProfile 关于我页面数据模型
type AboutProfile struct {
ID uint `json:"id" gorm:"primaryKey;column:id"`
Name string `json:"name" gorm:"column:name"`
Avatar string `json:"avatar" gorm:"column:avatar"`
Location string `json:"location" gorm:"column:location"`
Bio string `json:"bio" gorm:"column:bio;type:text"`
Email string `json:"email" gorm:"column:email"`
Wechat string `json:"wechat" gorm:"column:wechat"`
TechStack string `json:"-" gorm:"column:tech_stack;type:text"` // Stored as string in DB
TechList []string `json:"techStack" gorm:"-"` // Exposed as array in JSON
ExperiencesStr string `json:"-" gorm:"column:experiences;type:text"` // Stored as string in DB
ExperienceList []Experience `json:"experiences" gorm:"-"` // Exposed as array in JSON
IsPrimary bool `json:"isPrimary" gorm:"column:is_primary;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 (AboutProfile) TableName() string {
return "about_profiles"
}
// BeforeCreate 创建前钩子
func (a *AboutProfile) BeforeCreate(tx *gorm.DB) error {
now := time.Now().Unix()
if a.CreatedAt == 0 {
a.CreatedAt = now
}
if a.UpdatedAt == 0 {
a.UpdatedAt = now
}
if a.DeletedAt == 0 {
a.DeletedAt = 0
}
return nil
}
// BeforeUpdate 更新前钩子
func (a *AboutProfile) BeforeUpdate(tx *gorm.DB) error {
a.UpdatedAt = time.Now().Unix()
return nil
}