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
|
2022-01-04 05:01:42 +00:00
|
|
|
Img 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;"`
|
2022-01-04 05:01:42 +00:00
|
|
|
Img string
|
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"`
|
|
|
|
}
|
|
|
|
|
2022-01-04 05:01:42 +00:00
|
|
|
const (
|
|
|
|
OrderReceived = "RECEIVED"
|
|
|
|
OrderPrepared = "PREPARED"
|
|
|
|
OrderClaiming = "CLAIMING"
|
|
|
|
OrderClaimAccept = "CLAIM_ACCEPT"
|
|
|
|
OrderClaimReject = "CLAIM_REJECT"
|
|
|
|
OrderClaimPaid = "CLAIM_PAID"
|
|
|
|
)
|
|
|
|
|
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
|
2022-01-04 05:01:42 +00:00
|
|
|
Price float32
|
|
|
|
Status string
|
2021-12-30 09:16:20 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2022-01-04 05:01:42 +00:00
|
|
|
o.Status = OrderReceived
|
2022-01-03 05:06:26 +00:00
|
|
|
return
|
|
|
|
}
|