2021-12-30 09:16:20 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rs/xid"
|
2022-01-03 05:06:26 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-12-30 09:16:20 +00:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// User User model
|
|
|
|
type User struct {
|
|
|
|
ID string `gorm:"primaryKey"`
|
|
|
|
|
|
|
|
Email string
|
|
|
|
LastLogin time.Time
|
|
|
|
|
2021-12-30 13:39:00 +00:00
|
|
|
Products []Product `gorm:"many2many:orders;"`
|
2021-12-30 09:16:20 +00:00
|
|
|
|
|
|
|
ProfileID uint `gorm:"default:NULL"`
|
|
|
|
Profile Profile
|
|
|
|
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type Profile struct {
|
|
|
|
ID uint `gorm:"primaryKey"`
|
|
|
|
|
2022-01-03 14:50:49 +00:00
|
|
|
Name string
|
|
|
|
Brief string
|
2021-12-30 09:16:20 +00:00
|
|
|
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type Product struct {
|
|
|
|
ID uint `gorm:"primaryKey"`
|
|
|
|
|
2022-01-03 05:06:26 +00:00
|
|
|
Categories []Category `gorm:"many2many:product_categories;"`
|
2021-12-30 09:16:20 +00:00
|
|
|
Name string
|
|
|
|
Brief string
|
|
|
|
Description string
|
|
|
|
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
}
|
|
|
|
|
2022-01-03 05:06:26 +00:00
|
|
|
type Category struct {
|
|
|
|
Name string `gorm:"primaryKey"`
|
|
|
|
}
|
|
|
|
|
2021-12-30 09:16:20 +00:00
|
|
|
type Order struct {
|
2022-01-03 05:06:26 +00:00
|
|
|
ID string
|
|
|
|
|
2021-12-30 09:16:20 +00:00
|
|
|
UserID string `gorm:"primaryKey"`
|
|
|
|
ProductID uint `gorm:"primaryKey"`
|
|
|
|
|
2022-01-03 05:06:26 +00:00
|
|
|
Start time.Time
|
|
|
|
End time.Time
|
|
|
|
Attachment string
|
|
|
|
|
2021-12-30 09:16:20 +00:00
|
|
|
Price float32
|
|
|
|
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
}
|
|
|
|
|
2021-12-30 13:39:00 +00:00
|
|
|
type Node struct {
|
|
|
|
ID uint `gorm:"primaryKey"`
|
|
|
|
|
|
|
|
Name string
|
|
|
|
UserID string
|
|
|
|
|
|
|
|
CreatedAt time.Time
|
|
|
|
}
|
|
|
|
|
2021-12-30 09:16:20 +00:00
|
|
|
// 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
|
|
|
|
}
|
2022-01-03 05:06:26 +00:00
|
|
|
|
|
|
|
func (u *User) AfterFind(tx *gorm.DB) (err error) {
|
|
|
|
if u.ProfileID != 0 {
|
|
|
|
profile := &Profile{}
|
|
|
|
if err := tx.First(profile, "id = ?", u.ProfileID).Error; err != nil {
|
|
|
|
log.Error().Err(err).Msg("get profile error")
|
|
|
|
} else {
|
|
|
|
u.Profile = *profile
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Order) BeforeCreate(tx *gorm.DB) (err error) {
|
|
|
|
if o.ID == "" {
|
|
|
|
o.ID = xid.New().String()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|