Add cart template & adding products to cart, implement views

This commit is contained in:
mikgor 2018-10-26 22:53:46 +02:00
parent 4e0f22d0d2
commit 1aa32c0de9
8 changed files with 204 additions and 32 deletions

View File

@ -2,14 +2,17 @@ package initializers
import ( import (
"net/http" "net/http"
. "Elektromarket/views"
. "Elektromarket/models" . "Elektromarket/models"
) )
func initializePages() { func initializePages() {
Pages = make(map[string]Page) Pages = make(map[string]Page)
Pages["index"] = Page{"/", "templates/index.html", map[string]interface{}{"categories": Categories}, nil} Pages["index"] = Page{"/", "templates/index.html", map[string]interface{}{"categories": Categories}, IndexView}
Pages["category"] = Page{"/category", "templates/category.html", map[string]interface{}{}, DynamicDataLoaderCategory} Pages["category"] = Page{"/category", "templates/category.html", map[string]interface{}{}, CategoryView}
Pages["product"] = Page{"/product", "templates/product.html", map[string]interface{}{}, DynamicDataLoaderProduct} 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() { func initializeData() {

View File

@ -2,39 +2,17 @@ package models
import ( import (
"net/http" "net/http"
"html/template"
"strconv"
) )
type Page struct { type Page struct {
Path string Path string
Template string Template string
Data map[string]interface{} Data map[string]interface{}
DynamicDataLoader func(int) map[string]interface{} View func(Page, http.ResponseWriter, *http.Request)
}
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),
}
} }
func (p Page) HandlePage(w http.ResponseWriter, r *http.Request) { func (p Page) HandlePage(w http.ResponseWriter, r *http.Request) {
if p.DynamicDataLoader != nil { p.View(p, w , r )
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)
} }
type Category struct { type Category struct {
@ -48,13 +26,45 @@ type Product struct {
Name string Name string
Description string Description string
ImgUrl string ImgUrl string
Quantity uint Quantity int
Price uint 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 Pages map[string]Page
var Categories []Category var Categories []Category
var Products []Product 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 { func GetCategoryByName(name string) Category {
var category Category var category Category

View File

@ -10,6 +10,45 @@ body {
header { header {
box-shadow: 0 0 5px rgba(0,0,0,.4); box-shadow: 0 0 5px rgba(0,0,0,.4);
padding: 20px; 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 { a {
@ -111,3 +150,26 @@ input[type="button"] {
border-radius: 10px; border-radius: 10px;
color: white; 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;
}

0
templates/addToCart.html Normal file
View File

View File

@ -9,7 +9,10 @@
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"> <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
</head> </head>
<body> <body>
<header><a href="http://127.0.0.1:8000">[Strona główna]</a></header> <header>
<div class="headerColumn"><a href="http://127.0.0.1:8000"><div class="logoIcon"></div></a></div>
<div class="headerColumn"><a href="http://127.0.0.1:8000/cart"><div class="cartIcon"><div class="cartItems">{{.cart.ItemsTotal}}</div></a></div>
</header>
<div id="container">{{template "content" .}}</div> <div id="container">{{template "content" .}}</div>
</body> </body>
</html> </html>

21
templates/cart.html Normal file
View File

@ -0,0 +1,21 @@
{{define "content"}}
<h1>Koszyk</h1>
<div class="cartItem">
<div class="cartItemColumn"><h3>Produkt</h3></div>
<div class="cartItemColumn"><h3>Cena za szt.</h3></div>
<div class="cartItemColumn"><h3>Ilość</h3></div>
<div class="cartItemColumn"><h3>Łącznie</h3></div>
</div>
{{range $.cart.Products}}
<div class="cartItem">
<div class="cartItemColumn">
<a href="http://127.0.0.1:8000/product?id={{ .Product.Id }}"><b>{{ .Product.Name }}</b>
<div class="cartItemIcon" style="background-image: url({{ .Product.ImgUrl }});"></div></a>
</div>
<div class="cartItemColumn">{{ .Product.Price }} zł</div>
<div class="cartItemColumn">{{ .Quantity }}</div>
<div class="cartItemColumn">{{ .PriceTotal }} zł</div>
</div>
{{end}}
{{ .cart.PriceTotal }} zł
{{end}}

View File

@ -1,4 +1,9 @@
{{define "content"}} {{define "content"}}
<script type="text/javascript">
function buy() {
location.href = 'http://127.0.0.1:8000/addToCart?id=' + {{.product.Id}} + '&quantity=' + document.getElementById("quantity").value;
}
</script>
<div class="productDescription"> <div class="productDescription">
<div class="productDescriptionColumn"> <div class="productDescriptionColumn">
<div class="productDescriptionContent"> <div class="productDescriptionContent">
@ -9,10 +14,10 @@
<div class="productDescriptionColumn"> <div class="productDescriptionColumn">
<div class="productDescriptionText"> <div class="productDescriptionText">
{{.product.Description}}<br><br> {{.product.Description}}<br><br>
<div class="productPrice">Ilość sztuk: <input type="number" name="quantity" min="1" max="{{.product.Quantity}}" value="1"></div> <div class="productPrice">Ilość sztuk: <input id="quantity" type="number" name="quantity" min="1" max="{{.product.Quantity}}" value="1"></div>
<small>Dostępna Ilość: {{.product.Quantity}}</small><br><br> <small>Dostępna Ilość: {{.product.Quantity}}</small><br><br>
<div class="productPrice">Cena: {{.product.Price}} zł</div><br> <div class="productPrice">Cena: {{.product.Price}} zł</div><br>
<input type="button" value="Dodaj do koszyka" onclick="f()"> <input type="button" value="Dodaj do koszyka" onclick="buy();">
</div> </div>
</div> </div>
</div> </div>

68
views/views.go Normal file
View File

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