Elektromarket/initializers/initializers.go

94 lines
3.8 KiB
Go
Raw Normal View History

package initializers
import (
"net/http"
2018-10-28 11:33:41 +01:00
"database/sql"
"strconv"
. "Elektromarket/views"
. "Elektromarket/models"
2018-10-28 11:33:41 +01:00
. "Elektromarket/db"
)
func initializePages() {
Pages = make(map[string]Page)
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}
Pages["removeFromCart"] = Page{"/removeFromCart", "templates/removeFromCart.html", map[string]interface{}{}, RemoveFromCartView}
2018-10-27 15:45:05 +02:00
Pages["checkout"] = Page{"/checkout", "templates/checkout.html", map[string]interface{}{}, CheckoutView}
Pages["purchasements"] = Page{"/purchasements", "templates/purchasements.html", map[string]interface{}{}, PurchasementsView}
Pages["paymentDone"] = Page{"/paymentDone", "templates/paymentDone.html", map[string]interface{}{}, PaymentDoneView}
}
2018-10-28 11:33:41 +01:00
func insertCategories() {
for i := range Categories {
DbInsert("Category", "name", "'"+Categories[i].Name+"'")
}
2018-10-28 11:33:41 +01:00
}
2018-10-28 11:33:41 +01:00
func insertProducts() {
for i := range Products {
DbInsert("Product", "categoryId, name, description, imgUrl, quantity, price","'"+strconv.Itoa(Products[i].Category.Id)+"','"+Products[i].Name+"','"+Products[i].Description+"','"+Products[i].ImgUrl+"','"+strconv.Itoa(Products[i].Quantity)+"','"+strconv.Itoa(Products[i].Price)+"'")
}
}
2018-10-28 11:33:41 +01:00
func initializeCategories() {
var rows *sql.Rows = DbSelect("Category", "id, name")
var id int
var name string
for rows.Next() {
rows.Scan(&id, &name)
Categories = append(Categories, Category{id, name})
}
}
func initializeProducts() {
var rows *sql.Rows = DbSelect("Product", "id, categoryId, name, description, imgUrl, quantity, price")
var id, categoryId, quantity, price int
var name, description, imgUrl string
for rows.Next() {
rows.Scan(&id, &categoryId, &name, &description, &imgUrl, &quantity, &price)
Products = append(Products, Product{id, GetCategoryById(categoryId), name, description, imgUrl, quantity, price})
}
}
func initializePurchasements() {
var rows *sql.Rows = DbSelect("Purchasement", "id, priceTotal, firstName, lastName, paymentMethod, deliveryDate")
var rowsProducts *sql.Rows = DbSelect("PurchasementProduct", "id, purchasementId, productId, quantity, priceTotal")
var products map[int][]CartProduct
for rowsProducts.Next() {
var Pid, PpurchasementId, PproductId, Pquantity, PpriceTotal int
rows.Scan(&Pid, &PpurchasementId, &PproductId, &Pquantity, &PpriceTotal)
products[PpurchasementId] = append(products[PpurchasementId], CartProduct{GetProductById(PproductId), Pquantity, PpriceTotal})
}
for rows.Next() {
var purchasementproducts []CartProduct
var id, priceTotal, paymentMethod int
var firstName, lastName, deliveryDate string
rows.Scan(&id, &priceTotal, &firstName, &lastName, &paymentMethod, &deliveryDate)
for i := range products[id]{
purchasementproducts = append(purchasementproducts, products[id][i])
}
Purchasements = append(Purchasements, Purchasement{id, purchasementproducts, priceTotal, firstName, lastName, paymentMethod, deliveryDate})
if id > LastPurchasementId {
LastPurchasementId = id
}
}
}
func Initialize() {
2018-10-28 11:33:41 +01:00
DbConnect()
DbBuild()
initializeCategories()
initializeProducts()
initializePurchasements()
initializePages()
for k := range Pages {
http.HandleFunc(Pages[k].Path, Pages[k].HandlePage)
}
2018-10-25 20:31:50 +02:00
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.ListenAndServe(":8000", nil)
}