2018-10-26 22:53:46 +02:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
2018-10-27 17:22:45 +02:00
|
|
|
http.Redirect(w, r, "/cart", http.StatusSeeOther)
|
2018-10-26 22:53:46 +02:00
|
|
|
}
|
2018-10-27 12:56:16 +02:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2018-10-27 17:22:45 +02:00
|
|
|
http.Redirect(w, r, "/cart", http.StatusSeeOther)
|
2018-10-27 12:56:16 +02:00
|
|
|
}
|
2018-10-27 15:45:05 +02:00
|
|
|
|
|
|
|
func CheckoutView(p Page, w http.ResponseWriter, r *http.Request) {
|
|
|
|
ExecuteView(w, r, p.Template, p.Data)
|
|
|
|
}
|
2018-10-27 17:22:45 +02:00
|
|
|
|
|
|
|
func PaymentDoneView(p Page, w http.ResponseWriter, r *http.Request) {
|
|
|
|
fn := r.URL.Query()["fn"][0]
|
|
|
|
ln := r.URL.Query()["ln"][0]
|
|
|
|
pm, err := strconv.Atoi(r.URL.Query()["pm"][0])
|
|
|
|
dd := r.URL.Query()["dd"][0]
|
|
|
|
if fn != "" && ln != "" && dd != "" && err == nil {
|
|
|
|
purchasement := Purchasement{ShoppingCart.Products, ShoppingCart.PriceTotal, fn, ln, pm, dd}
|
|
|
|
Purchasements = append(Purchasements, purchasement)
|
|
|
|
ShoppingCart.Products = nil
|
|
|
|
ShoppingCart.Calculate()
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, "/purchasements", http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PurchasementsView(p Page, w http.ResponseWriter, r *http.Request) {
|
|
|
|
p.Data = map[string]interface{}{
|
|
|
|
"purchasements": Purchasements,
|
|
|
|
}
|
|
|
|
ExecuteView(w, r, p.Template, p.Data)
|
|
|
|
}
|