diff --git a/initializers/initializers.go b/initializers/initializers.go index a161f94..606860d 100644 --- a/initializers/initializers.go +++ b/initializers/initializers.go @@ -2,14 +2,17 @@ package initializers import ( "net/http" + . "Elektromarket/views" . "Elektromarket/models" ) func initializePages() { Pages = make(map[string]Page) - Pages["index"] = Page{"/", "templates/index.html", map[string]interface{}{"categories": Categories}, nil} - Pages["category"] = Page{"/category", "templates/category.html", map[string]interface{}{}, DynamicDataLoaderCategory} - Pages["product"] = Page{"/product", "templates/product.html", map[string]interface{}{}, DynamicDataLoaderProduct} + Pages["index"] = Page{"/", "templates/index.html", map[string]interface{}{"categories": Categories}, IndexView} + Pages["category"] = Page{"/category", "templates/category.html", map[string]interface{}{}, CategoryView} + Pages["product"] = Page{"/product", "templates/product.html", map[string]interface{}{}, ProductView} + Pages["cart"] = Page{"/cart", "templates/cart.html", map[string]interface{}{}, CartView} + Pages["addToCart"] = Page{"/addToCart", "templates/addToCart.html", map[string]interface{}{}, AddToCartView} } func initializeData() { diff --git a/models/models.go b/models/models.go index 2c58ec5..79172d2 100644 --- a/models/models.go +++ b/models/models.go @@ -2,39 +2,17 @@ package models import ( "net/http" - "html/template" - "strconv" ) type Page struct { Path string Template string Data map[string]interface{} - DynamicDataLoader func(int) map[string]interface{} -} - -func DynamicDataLoaderCategory(id int) map[string]interface{}{ - return map[string]interface{}{ - "category": GetCategoryById(id), - "products": GetCategoryProducts(id), - } -} - -func DynamicDataLoaderProduct(id int) map[string]interface{}{ - return map[string]interface{}{ - "product": GetProductById(id), - } + View func(Page, http.ResponseWriter, *http.Request) } func (p Page) HandlePage(w http.ResponseWriter, r *http.Request) { - if p.DynamicDataLoader != nil { - id, err := strconv.Atoi(r.URL.Query()["id"][0]) - if err == nil { - p.Data = p.DynamicDataLoader(id) - } - } - t, _ := template.ParseFiles(p.Template, "templates/base.html") - t.ExecuteTemplate(w, "base", p.Data) + p.View(p, w , r ) } type Category struct { @@ -48,13 +26,45 @@ type Product struct { Name string Description string ImgUrl string - Quantity uint - Price uint + 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 diff --git a/static/css/style.css b/static/css/style.css index 666a25a..0463904 100644 --- a/static/css/style.css +++ b/static/css/style.css @@ -10,6 +10,45 @@ body { header { box-shadow: 0 0 5px rgba(0,0,0,.4); padding: 20px; + display: flex; +} + +.logoIcon { + width: 200px; + height: 50px; + background-size: 100%; + background-position: center; + background-repeat: no-repeat; + background-image: url(http://127.0.0.1:8000/static/icons/logo.png); + float: left; +} + +.headerColumn { + margin: auto; + width: 50%; +} + +.cartIcon { + width: 30px; + height: 30px; + background-size: 100%; + background-position: center; + background-repeat: no-repeat; + background-image: url(http://127.0.0.1:8000/static/icons/shopping-cart.png); + float: right; +} + +.cartItems { + background-color: #d6dae2; + position: absolute; + padding: 3px; + top: 19px; + padding-left: 7px; + border-radius: 3px; + padding-right: 7px; + font-size: small; + right: 9px; + color: white; } a { @@ -111,3 +150,26 @@ input[type="button"] { border-radius: 10px; color: white; } + +.cartItem { + width: 90%; + display: flex; + text-align: center; + margin: 20px auto; + border-bottom: 1px solid rgba(0,0,0,.4); +} + +.cartItemColumn { + width: 25%; + margin: auto; +} + +.cartItemIcon { + width: 90%; + width: 50px; + height: 50px; + background-size: 100%; + background-position: center; + background-repeat: no-repeat; + margin: auto; +} diff --git a/templates/addToCart.html b/templates/addToCart.html new file mode 100644 index 0000000..e69de29 diff --git a/templates/base.html b/templates/base.html index 7677511..3dd1202 100644 --- a/templates/base.html +++ b/templates/base.html @@ -9,7 +9,10 @@ -
[Strona główna]
+
+
+
+
{{template "content" .}}
diff --git a/templates/cart.html b/templates/cart.html new file mode 100644 index 0000000..9b01919 --- /dev/null +++ b/templates/cart.html @@ -0,0 +1,21 @@ +{{define "content"}} +

Koszyk

+
+

Produkt

+

Cena za szt.

+

Ilość

+

Łącznie

+
+ {{range $.cart.Products}} +
+
+ {{ .Product.Name }} +
+
+
{{ .Product.Price }} zł
+
{{ .Quantity }}
+
{{ .PriceTotal }} zł
+
+ {{end}} + {{ .cart.PriceTotal }} zł +{{end}} diff --git a/templates/product.html b/templates/product.html index e0d14ac..1623e73 100644 --- a/templates/product.html +++ b/templates/product.html @@ -1,4 +1,9 @@ {{define "content"}} +
@@ -9,10 +14,10 @@
{{.product.Description}}

-
Ilość sztuk:
+
Ilość sztuk:
Dostępna Ilość: {{.product.Quantity}}

Cena: {{.product.Price}} zł

- +
diff --git a/views/views.go b/views/views.go new file mode 100644 index 0000000..f4353cc --- /dev/null +++ b/views/views.go @@ -0,0 +1,68 @@ +package views + +import ( + "net/http" + "strconv" + "html/template" + . "Elektromarket/models" +) + +func ExecuteView(w http.ResponseWriter, r *http.Request, templateName string, data map[string]interface{}) { + data["cart"] = ShoppingCart + t, _ := template.ParseFiles(templateName, "templates/base.html") + t.ExecuteTemplate(w, "base", data) +} + +func IndexView(p Page, w http.ResponseWriter, r *http.Request) { + ExecuteView(w, r, p.Template, p.Data) +} + +func CategoryView(p Page, w http.ResponseWriter, r *http.Request) { + id, err := strconv.Atoi(r.URL.Query()["id"][0]) + if err == nil { + p.Data = map[string]interface{}{ + "category": GetCategoryById(id), + "products": GetCategoryProducts(id), + } + } + ExecuteView(w, r, p.Template, p.Data) +} + +func ProductView(p Page, w http.ResponseWriter, r *http.Request) { + id, err := strconv.Atoi(r.URL.Query()["id"][0]) + if err == nil { + p.Data = map[string]interface{}{ + "product": GetProductById(id), + } + } + ExecuteView(w, r, p.Template, p.Data) +} + +func CartView(p Page, w http.ResponseWriter, r *http.Request) { + ExecuteView(w, r, p.Template, p.Data) +} + +func AddToCartView(p Page, w http.ResponseWriter, r *http.Request) { + id, err := strconv.Atoi(r.URL.Query()["id"][0]) + quantity, erro := strconv.Atoi(r.URL.Query()["quantity"][0]) + if err == nil && erro == nil { + prod := GetProductById(id) + if prod.Quantity >= quantity { + prod.Quantity -= quantity + prod.Save() + var exists bool = false + for i:= range ShoppingCart.Products { + if prod.Id == ShoppingCart.Products[i].Product.Id { + exists = true + ShoppingCart.Products[i].Quantity += quantity + break + } + } + if !exists { + ShoppingCart.Products = append(ShoppingCart.Products, CartProduct{prod, quantity, 0}) + } + ShoppingCart.Calculate() + } + } + ExecuteView(w, r, "templates/cart.html", p.Data) +}