diff --git a/db5.sqlite3 b/db5.sqlite3 index 6768b26..4665bc9 100644 Binary files a/db5.sqlite3 and b/db5.sqlite3 differ diff --git a/src/a3 b/src/a3 index bbc49df..30a93f2 100755 Binary files a/src/a3 and b/src/a3 differ diff --git a/src/a3.nim b/src/a3.nim index 0e5baec..d53ac6b 100644 --- a/src/a3.nim +++ b/src/a3.nim @@ -79,6 +79,34 @@ import compileTemplateFile(getScriptDir() / "a3a" / "cart.nimja") +"/add-to-cart" -> get: + + var + email: string + password: string + db = newDatabase() + + try: + email = ctx.cookies["email"] + password = ctx.cookies["password"] + except: + email = "" + password = "" + + if email == "": + ctx.redirect("/login") + + else: + var + cart: Cart + + cart.userId = db.getUserId(email, password) + cart.productId = db.getProductByName(ctx.queryParams["prod"]).id + + db.addToCart(cart) + + ctx.redirect("/cart") + "/checkout" -> get: var @@ -166,7 +194,7 @@ import productName = ctx.queryParams["prod"] - product = db.getProduct(productName) + product = db.getProductByName(productName) products: seq[Products] diff --git a/src/a3a/shop-single.nimja b/src/a3a/shop-single.nimja index 3e0ad7e..2f13bba 100644 --- a/src/a3a/shop-single.nimja +++ b/src/a3a/shop-single.nimja @@ -99,14 +99,14 @@
- +
-

Add To Cart

+

Add To Cart

@@ -188,4 +188,12 @@ + {% endblock %} diff --git a/src/a3c/cart.nim b/src/a3c/cart.nim index c83ca38..f6ac594 100644 --- a/src/a3c/cart.nim +++ b/src/a3c/cart.nim @@ -37,4 +37,7 @@ proc getUserCart*(db: DbConn, userId: int): seq[Cart] = cartDetails.add(product) - return cartDetails \ No newline at end of file + return cartDetails + +proc addToCart*(db: DbConn, cart: Cart) = + db.exec(sql"INSERT INTO cart (user_id, product_id, quantity) VALUES (?, ?, ?)", cart.userId, cart.productId, cart.quantity) \ No newline at end of file diff --git a/src/a3c/products.nim b/src/a3c/products.nim index 0c9d531..40e0796 100644 --- a/src/a3c/products.nim +++ b/src/a3c/products.nim @@ -49,7 +49,7 @@ proc availableProducts*(db: DbConn): seq[Products]= return products -proc getProduct*(db: DbConn, name: string): Products = +proc getProductByName*(db: DbConn, name: string): Products = var row = db.getRow(sql"SELECT * FROM products WHERE name = ?", name) var product: Products product.id = parseInt(row[0])