Elektromarket/models/models.go
2018-10-28 12:32:15 +01:00

134 lines
2.3 KiB
Go

package models
import (
"net/http"
)
type Page struct {
Path string
Template string
Data map[string]interface{}
View func(Page, http.ResponseWriter, *http.Request)
}
func (p Page) HandlePage(w http.ResponseWriter, r *http.Request) {
p.View(p, w , r )
}
type Category struct {
Id int
Name string
}
type Product struct {
Id int
Category Category
Name string
Description string
ImgUrl string
Quantity int
Price int
}
type CartProduct struct {
Product Product
Quantity int
PriceTotal int
}
type Cart struct {
Products []CartProduct
PriceTotal int
ItemsTotal int
}
type Purchasement struct {
Id int
Products []CartProduct
PriceTotal int
FirstName string
LastName string
PaymentMethod int
DeliveryDate string
}
func (c Cart) Calculate() {
ShoppingCart.PriceTotal = 0
ShoppingCart.ItemsTotal = 0
for i:= range c.Products {
ShoppingCart.Products[i].PriceTotal = ShoppingCart.Products[i].Product.Price * ShoppingCart.Products[i].Quantity
ShoppingCart.PriceTotal += ShoppingCart.Products[i].PriceTotal
ShoppingCart.ItemsTotal += ShoppingCart.Products[i].Quantity
}
}
var Pages map[string]Page
var Categories []Category
var Products []Product
var ShoppingCart Cart
var Purchasements []Purchasement
var LastPurchasementId int
func (p Product) Save() {
for i:= range Products {
if Products[i].Id == p.Id {
Products[i] = p
break
}
}
}
func GetCategoryByName(name string) Category {
var category Category
for i:= range Categories {
if Categories[i].Name == name {
category = Categories[i]
break
}
}
return category
}
func GetCategoryById(id int) Category {
var category Category
for i:= range Categories {
if Categories[i].Id == id {
category = Categories[i]
break
}
}
return category
}
func GetCategoryProducts(id int) []Product {
var products []Product
for i:= range Products {
if Products[i].Category.Id == id {
products = append(products, Products[i])
}
}
return products
}
func GetProductById(id int) Product {
var product Product
for i:= range Products {
if Products[i].Id == id {
product = Products[i]
break
}
}
return product
}
func GetPurchasementById(id int) Purchasement {
var purchasement Purchasement
for i:= range Purchasements {
if Purchasements[i].Id == id {
purchasement = Purchasements[i]
break
}
}
return purchasement
}