Elektromarket/views/views.go

117 lines
3.9 KiB
Go
Raw Normal View History

package views
import (
"net/http"
"strconv"
"html/template"
. "Elektromarket/models"
2018-10-28 11:33:41 +01:00
. "Elektromarket/db"
)
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()
}
}
http.Redirect(w, r, "/cart", http.StatusSeeOther)
}
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()
}
http.Redirect(w, r, "/cart", http.StatusSeeOther)
}
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)
}
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 {
2018-10-28 11:33:41 +01:00
LastPurchasementId+=1
purchasement := Purchasement{LastPurchasementId, ShoppingCart.Products, ShoppingCart.PriceTotal, fn, ln, pm, dd}
Purchasements = append(Purchasements, purchasement)
2018-10-28 11:33:41 +01:00
DbInsert("Purchasement", "priceTotal, firstName, lastName, paymentMethod, deliveryDate", "'" +strconv.Itoa(purchasement.PriceTotal) +"','"+ purchasement.FirstName + "','" + purchasement.LastName + "','" + strconv.Itoa(purchasement.PaymentMethod) + "','" + purchasement.DeliveryDate +"'")
for i:= range purchasement.Products {
DbInsert("PurchasementProduct", "purchasementId, productId, quantity, priceTotal", "'" +strconv.Itoa(LastPurchasementId) + "','" + strconv.Itoa(purchasement.Products[i].Product.Id) + "','" + strconv.Itoa(purchasement.Products[i].Quantity) + "','" + strconv.Itoa(purchasement.Products[i].PriceTotal) + "'")
}
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)
}