111 lines
1.9 KiB
Go
111 lines
1.9 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
|
|
}
|
|
|
|
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
|
|
|
|
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
|
|
}
|