Add removeFromCart template & displaying products availability

This commit is contained in:
mikgor 2018-10-27 12:56:16 +02:00
parent 1aa32c0de9
commit 141c357e32
7 changed files with 41 additions and 2 deletions

View File

@ -13,6 +13,7 @@ func initializePages() {
Pages["product"] = Page{"/product", "templates/product.html", map[string]interface{}{}, ProductView} Pages["product"] = Page{"/product", "templates/product.html", map[string]interface{}{}, ProductView}
Pages["cart"] = Page{"/cart", "templates/cart.html", map[string]interface{}{}, CartView} Pages["cart"] = Page{"/cart", "templates/cart.html", map[string]interface{}{}, CartView}
Pages["addToCart"] = Page{"/addToCart", "templates/addToCart.html", map[string]interface{}{}, AddToCartView} Pages["addToCart"] = Page{"/addToCart", "templates/addToCart.html", map[string]interface{}{}, AddToCartView}
Pages["removeFromCart"] = Page{"/removeFromCart", "templates/removeFromCart.html", map[string]interface{}{}, RemoveFromCartView}
} }
func initializeData() { func initializeData() {

View File

@ -160,7 +160,7 @@ input[type="button"] {
} }
.cartItemColumn { .cartItemColumn {
width: 25%; width: 20%;
margin: auto; margin: auto;
} }
@ -173,3 +173,8 @@ input[type="button"] {
background-repeat: no-repeat; background-repeat: no-repeat;
margin: auto; margin: auto;
} }
.productUnavailable {
color: red;
font-size: 20px;
}

View File

@ -1,10 +1,16 @@
{{define "content"}} {{define "content"}}
<script type="text/javascript">
function deleteitem(id) {
location.href = 'http://127.0.0.1:8000/removeFromCart?id=' + id;
}
</script>
<h1>Koszyk</h1> <h1>Koszyk</h1>
<div class="cartItem"> <div class="cartItem">
<div class="cartItemColumn"><h3>Produkt</h3></div> <div class="cartItemColumn"><h3>Produkt</h3></div>
<div class="cartItemColumn"><h3>Cena za szt.</h3></div> <div class="cartItemColumn"><h3>Cena za szt.</h3></div>
<div class="cartItemColumn"><h3>Ilość</h3></div> <div class="cartItemColumn"><h3>Ilość</h3></div>
<div class="cartItemColumn"><h3>Łącznie</h3></div> <div class="cartItemColumn"><h3>Łącznie</h3></div>
<div class="cartItemColumn"></div>
</div> </div>
{{range $.cart.Products}} {{range $.cart.Products}}
<div class="cartItem"> <div class="cartItem">
@ -15,7 +21,8 @@
<div class="cartItemColumn">{{ .Product.Price }} zł</div> <div class="cartItemColumn">{{ .Product.Price }} zł</div>
<div class="cartItemColumn">{{ .Quantity }}</div> <div class="cartItemColumn">{{ .Quantity }}</div>
<div class="cartItemColumn">{{ .PriceTotal }} zł</div> <div class="cartItemColumn">{{ .PriceTotal }} zł</div>
<div class="cartItemColumn"><input type="button" value="Usuń" onclick="deleteitem({{ .Product.Id }});" style="background-color: red;"></div>
</div> </div>
{{end}} {{end}}
{{ .cart.PriceTotal }} zł Wartość koszyka: {{ .cart.PriceTotal }} zł
{{end}} {{end}}

View File

@ -3,6 +3,9 @@
{{range $.products}} {{range $.products}}
<a href="http://127.0.0.1:8000/product?id={{ .Id }}"> <a href="http://127.0.0.1:8000/product?id={{ .Id }}">
<div class="productTile"> <div class="productTile">
{{if le .Quantity 0}}
<div class="productUnavailable" style="width: 350px; position:absolute;">Produkt niedostępny</div>
{{end}}
<div class="categoryTileContent"> <div class="categoryTileContent">
<div class="productIcon" style="background-image: url({{ .ImgUrl }});"></div> <div class="productIcon" style="background-image: url({{ .ImgUrl }});"></div>
<div class="categoryName">{{ .Name }}</div> <div class="categoryName">{{ .Name }}</div>

View File

@ -14,10 +14,16 @@ function buy() {
<div class="productDescriptionColumn"> <div class="productDescriptionColumn">
<div class="productDescriptionText"> <div class="productDescriptionText">
{{.product.Description}}<br><br> {{.product.Description}}<br><br>
{{if gt .product.Quantity 0}}
<div class="productPrice">Ilość sztuk: <input id="quantity" 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>
{{end}}
<div class="productPrice">Cena: {{.product.Price}} zł</div><br> <div class="productPrice">Cena: {{.product.Price}} zł</div><br>
{{if gt .product.Quantity 0}}
<input type="button" value="Dodaj do koszyka" onclick="buy();"> <input type="button" value="Dodaj do koszyka" onclick="buy();">
{{else}}
<div class="productUnavailable">Produkt niedostępny</div>
{{end}}
</div> </div>
</div> </div>
</div> </div>

View File

View File

@ -66,3 +66,20 @@ func AddToCartView(p Page, w http.ResponseWriter, r *http.Request) {
} }
ExecuteView(w, r, "templates/cart.html", p.Data) ExecuteView(w, r, "templates/cart.html", p.Data)
} }
func RemoveFromCartView(p Page, w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.URL.Query()["id"][0])
if err == nil {
for i:= range ShoppingCart.Products {
if id == ShoppingCart.Products[i].Product.Id {
prod := GetProductById(id)
prod.Quantity += ShoppingCart.Products[i].Quantity
prod.Save()
ShoppingCart.Products = append(ShoppingCart.Products[:i], ShoppingCart.Products[i+1:]...)
break
}
}
ShoppingCart.Calculate()
}
ExecuteView(w, r, "templates/cart.html", p.Data)
}