moved to gorm
This commit is contained in:
parent
66884c64bb
commit
a074f264df
30 changed files with 247 additions and 1040 deletions
|
|
@ -1,37 +0,0 @@
|
|||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type Cake struct {
|
||||
Name string
|
||||
ID int
|
||||
Price int // increments of 0.01
|
||||
Amount int
|
||||
Category string
|
||||
Availability string
|
||||
}
|
||||
|
||||
type Order struct {
|
||||
ID int
|
||||
Name string
|
||||
Surname string
|
||||
Phone string
|
||||
Location string
|
||||
Date time.Time
|
||||
Accepted time.Time
|
||||
Status string
|
||||
Paid int // increments of 0.01
|
||||
Cakes []Cake
|
||||
}
|
||||
|
||||
func (o *Order) Total() int {
|
||||
out := o.Paid * (-1)
|
||||
for _, e := range o.Cakes {
|
||||
out += e.Price * e.Amount
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (c *Cake) Total() int {
|
||||
return c.Amount * c.Price
|
||||
}
|
||||
41
models/order.go
Normal file
41
models/order.go
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// money amounts are in incremetns of 0.01
|
||||
|
||||
type OrderItem struct {
|
||||
Product Product
|
||||
Amount uint
|
||||
ProductID uint `gorm:"primaryKey;autoIncrement:false"`
|
||||
OrderID uint `gorm:"primaryKey;autoIncrement:false"`
|
||||
}
|
||||
|
||||
type Order struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
Surname string
|
||||
Phone string
|
||||
Location string
|
||||
Status string
|
||||
Prepaid uint
|
||||
Date time.Time
|
||||
OrderItems []OrderItem
|
||||
}
|
||||
|
||||
func (c *OrderItem) Total() uint {
|
||||
return c.Amount * c.Product.Price
|
||||
}
|
||||
|
||||
func (o *Order) Total() uint {
|
||||
var out uint
|
||||
for _, e := range o.OrderItems {
|
||||
out += e.Total()
|
||||
}
|
||||
out -= o.Prepaid
|
||||
return out
|
||||
}
|
||||
9
models/product.go
Normal file
9
models/product.go
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package models
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type Product struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
Price uint
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ package models
|
|||
import "time"
|
||||
|
||||
type Token struct {
|
||||
Token string `gorm:"primarykey"`
|
||||
User string
|
||||
Expiration time.Time
|
||||
}
|
||||
|
|
@ -1,6 +1,10 @@
|
|||
package models
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type User struct {
|
||||
gorm.Model
|
||||
Login string `gorm:"unique"`
|
||||
Password string
|
||||
Salt string
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue