SoXX
c120c583e3
All checks were successful
Gitea Build Check / Build (push) Successful in 2m56s
- added migration - added model
35 lines
630 B
Go
35 lines
630 B
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
gonanoid "github.com/matoous/go-nanoid/v2"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type ID interface {
|
|
UserID | SourceID | PostID | UserSourceID | UserFavoriteID | PoolID
|
|
}
|
|
|
|
type BaseModel[T ID] struct {
|
|
ID T `json:"id" gorm:"primaryKey"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
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
|
|
}
|