package initializers import ( "net/http" "database/sql" "strconv" . "Elektromarket/views" . "Elektromarket/models" . "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} 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} Pages["purchasement"] = Page{"/purchasement", "templates/purchasement.html", map[string]interface{}{}, PurchasementView} } func insertCategories() { for i := range Categories { DbInsert("Category", "name", "'"+Categories[i].Name+"'") } } 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)+"'") } } 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") products := make(map[int][]CartProduct) for rowsProducts.Next() { var pid, ppurchasementId, pproductId, pquantity, ppriceTotal int rowsProducts.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() { DbConnect() DbBuild() initializeCategories() initializeProducts() initializePurchasements() initializePages() for k := range Pages { http.HandleFunc(Pages[k].Path, Pages[k].HandlePage) } http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) http.ListenAndServe(":8000", nil) }