65 lines
941 B
Go
65 lines
941 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"time"
|
||
|
|
||
|
"github.com/rs/xid"
|
||
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
|
// User User model
|
||
|
type User struct {
|
||
|
ID string `gorm:"primaryKey"`
|
||
|
|
||
|
Email string
|
||
|
LastLogin time.Time
|
||
|
|
||
|
Products Product `gorm:"many2many:orders;"`
|
||
|
|
||
|
ProfileID uint `gorm:"default:NULL"`
|
||
|
Profile Profile
|
||
|
|
||
|
CreatedAt time.Time
|
||
|
UpdatedAt time.Time
|
||
|
}
|
||
|
|
||
|
type Profile struct {
|
||
|
ID uint `gorm:"primaryKey"`
|
||
|
|
||
|
Name string
|
||
|
|
||
|
CreatedAt time.Time
|
||
|
UpdatedAt time.Time
|
||
|
}
|
||
|
|
||
|
type Product struct {
|
||
|
ID uint `gorm:"primaryKey"`
|
||
|
|
||
|
Type string
|
||
|
Name string
|
||
|
Brief string
|
||
|
Description string
|
||
|
|
||
|
CreatedAt time.Time
|
||
|
UpdatedAt time.Time
|
||
|
}
|
||
|
|
||
|
type Order struct {
|
||
|
UserID string `gorm:"primaryKey"`
|
||
|
ProductID uint `gorm:"primaryKey"`
|
||
|
|
||
|
Price float32
|
||
|
|
||
|
CreatedAt time.Time
|
||
|
UpdatedAt time.Time
|
||
|
}
|
||
|
|
||
|
// BeforeCreate set UID
|
||
|
func (u *User) BeforeCreate(tx *gorm.DB) (err error) {
|
||
|
if u.ID == "" {
|
||
|
u.ID = xid.New().String()
|
||
|
}
|
||
|
u.LastLogin = time.Now()
|
||
|
return
|
||
|
}
|