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 @@
-