137 lines
2.5 KiB
Go
137 lines
2.5 KiB
Go
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"log"
|
||
|
|
"os"
|
||
|
|
"path"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
uuid "github.com/satori/go.uuid"
|
||
|
|
|
||
|
|
sqlite "gorm.io/driver/sqlite"
|
||
|
|
"gorm.io/gorm"
|
||
|
|
"gorm.io/gorm/logger"
|
||
|
|
)
|
||
|
|
|
||
|
|
const (
|
||
|
|
PRE_ORDER_STATE_CREATED = "CREATED"
|
||
|
|
)
|
||
|
|
|
||
|
|
func initDB() (*gorm.DB, error) {
|
||
|
|
log.Printf("Connect to DB on path: %v", path.Join(ROOT_DIR, "data/db/data.db"))
|
||
|
|
|
||
|
|
newLogger := logger.New(
|
||
|
|
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
|
||
|
|
logger.Config{
|
||
|
|
SlowThreshold: time.Second, // Slow SQL threshold
|
||
|
|
LogLevel: logger.Info, // Log level
|
||
|
|
Colorful: false, // Disable color
|
||
|
|
},
|
||
|
|
)
|
||
|
|
|
||
|
|
db, err := gorm.Open(sqlite.Open(path.Join(ROOT_DIR, "data/db/data.db")), &gorm.Config{
|
||
|
|
Logger: newLogger,
|
||
|
|
})
|
||
|
|
return db, err
|
||
|
|
}
|
||
|
|
|
||
|
|
type Product struct {
|
||
|
|
gorm.Model
|
||
|
|
Name string
|
||
|
|
Image string
|
||
|
|
Description string
|
||
|
|
Price int
|
||
|
|
}
|
||
|
|
|
||
|
|
type Item struct {
|
||
|
|
gorm.Model
|
||
|
|
ProductID uint
|
||
|
|
Product Product
|
||
|
|
PreOrderID uuid.UUID
|
||
|
|
PreOrder PreOrder
|
||
|
|
}
|
||
|
|
|
||
|
|
type MolliePayment struct {
|
||
|
|
gorm.Model
|
||
|
|
MolliePaymentID string
|
||
|
|
Amount int
|
||
|
|
Status string
|
||
|
|
PreOrderID uuid.UUID
|
||
|
|
PreOrder PreOrder
|
||
|
|
}
|
||
|
|
|
||
|
|
type PreOrder struct {
|
||
|
|
ID uuid.UUID `sql:"type:string;primary_key;"`
|
||
|
|
CreatedAt time.Time
|
||
|
|
UpdatedAt time.Time
|
||
|
|
DeletedAt *time.Time `sql:"index"`
|
||
|
|
Name string
|
||
|
|
Email string
|
||
|
|
Items []Item
|
||
|
|
Address string
|
||
|
|
Postcode string
|
||
|
|
City string
|
||
|
|
Country string
|
||
|
|
Status string
|
||
|
|
MolliePayments []MolliePayment
|
||
|
|
}
|
||
|
|
|
||
|
|
var nullUUID = uuid.UUID{}
|
||
|
|
|
||
|
|
func (base *PreOrder) BeforeCreate(scope *gorm.DB) error {
|
||
|
|
fmt.Println(base.ID.String(), nullUUID.String())
|
||
|
|
if base.ID.String() == nullUUID.String() {
|
||
|
|
id := uuid.NewV4()
|
||
|
|
base.ID = id
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (po *PreOrder) CalcTotal() int {
|
||
|
|
total := 0
|
||
|
|
|
||
|
|
for _, i := range po.Items {
|
||
|
|
total += i.Product.Price
|
||
|
|
}
|
||
|
|
|
||
|
|
return total
|
||
|
|
}
|
||
|
|
|
||
|
|
func insertProducts(db *gorm.DB) {
|
||
|
|
db.Create(&Product{
|
||
|
|
Name: "Methods to Madness | LP",
|
||
|
|
Image: "/static/img/mtm.jpg",
|
||
|
|
Description: "Methods to Madness LP",
|
||
|
|
Price: 2999,
|
||
|
|
})
|
||
|
|
|
||
|
|
db.Create(&Product{
|
||
|
|
Name: "Methods to Madness | CD",
|
||
|
|
Image: "/static/img/mtm.jpg",
|
||
|
|
Description: "Methods to Madness CD",
|
||
|
|
Price: 1999,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
func GetDB() *gorm.DB {
|
||
|
|
db, err := initDB()
|
||
|
|
|
||
|
|
if err != nil {
|
||
|
|
panic("failed to connect database")
|
||
|
|
}
|
||
|
|
|
||
|
|
// Migrate the schema
|
||
|
|
db.AutoMigrate(
|
||
|
|
&Product{},
|
||
|
|
&Item{},
|
||
|
|
&PreOrder{},
|
||
|
|
&MolliePayment{},
|
||
|
|
)
|
||
|
|
|
||
|
|
products := []Product{}
|
||
|
|
db.Find(&products)
|
||
|
|
|
||
|
|
return db
|
||
|
|
}
|