29 lines
712 B
Go
29 lines
712 B
Go
|
|
package model
|
||
|
|
|
||
|
|
import (
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/google/uuid"
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Favorite 收藏
|
||
|
|
type Favorite struct {
|
||
|
|
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
||
|
|
PostID string `json:"post_id" gorm:"type:varchar(36);not null;index;uniqueIndex:idx_favorite_post_user,priority:1"`
|
||
|
|
UserID string `json:"user_id" gorm:"type:varchar(36);not null;index;uniqueIndex:idx_favorite_post_user,priority:2"`
|
||
|
|
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// BeforeCreate 创建前生成UUID
|
||
|
|
func (f *Favorite) BeforeCreate(tx *gorm.DB) error {
|
||
|
|
if f.ID == "" {
|
||
|
|
f.ID = uuid.New().String()
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (Favorite) TableName() string {
|
||
|
|
return "favorites"
|
||
|
|
}
|