117 lines
1.9 KiB
Go
117 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/rs/xid"
|
|
"github.com/rs/zerolog/log"
|
|
"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
|
|
Brief string
|
|
Img string
|
|
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
type Product struct {
|
|
ID uint `gorm:"primaryKey"`
|
|
|
|
Categories []Category `gorm:"many2many:product_categories;"`
|
|
Img string
|
|
Name string
|
|
Brief string
|
|
Description string
|
|
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
type Category struct {
|
|
Name string `gorm:"primaryKey"`
|
|
}
|
|
|
|
const (
|
|
OrderReceived = "RECEIVED"
|
|
OrderPrepared = "PREPARED"
|
|
OrderClaiming = "CLAIMING"
|
|
OrderClaimAccept = "CLAIM_ACCEPT"
|
|
OrderClaimReject = "CLAIM_REJECT"
|
|
OrderClaimPaid = "CLAIM_PAID"
|
|
)
|
|
|
|
type Order struct {
|
|
ID string
|
|
|
|
UserID string `gorm:"primaryKey"`
|
|
ProductID uint `gorm:"primaryKey"`
|
|
|
|
Start time.Time
|
|
End time.Time
|
|
Attachment string
|
|
Price float32
|
|
Status string
|
|
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
type Node struct {
|
|
ID uint `gorm:"primaryKey"`
|
|
|
|
Name string
|
|
UserID string
|
|
|
|
CreatedAt 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
|
|
}
|
|
|
|
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()
|
|
}
|
|
o.Status = OrderReceived
|
|
return
|
|
}
|