2024-07-19 08:03:35 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
gonanoid "github.com/matoous/go-nanoid/v2"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ID interface {
|
2024-11-06 10:22:31 +00:00
|
|
|
UserID | SourceID | PostID | UserSourceID | UserFavoriteID | PoolID | PostReportID
|
2024-07-19 08:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type BaseModel[T ID] struct {
|
|
|
|
ID T `json:"id" gorm:"primaryKey"`
|
2024-08-13 08:54:21 +00:00
|
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
UpdatedAt time.Time `json:"updated_at"`
|
2024-07-19 08:03:35 +00:00
|
|
|
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (base *BaseModel[T]) BeforeCreate(db *gorm.DB) error {
|
|
|
|
var defaultVar T
|
|
|
|
|
|
|
|
if base.ID == defaultVar {
|
|
|
|
id, err := gonanoid.New(25)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
base.ID = T(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|