diff --git a/db5.sqlite3 b/db5.sqlite3 index 4665bc9..f8ffee8 100644 Binary files a/db5.sqlite3 and b/db5.sqlite3 differ diff --git a/src/a3 b/src/a3 index 30a93f2..f373684 100755 Binary files a/src/a3 and b/src/a3 differ diff --git a/src/a3.nim b/src/a3.nim index d53ac6b..a62f650 100644 --- a/src/a3.nim +++ b/src/a3.nim @@ -3,6 +3,7 @@ import segfaults, os, nimja/parser, + strutils, ./a3pkg/[models, mics], ./a3c/[products, users, cart] @@ -102,11 +103,43 @@ import cart.userId = db.getUserId(email, password) cart.productId = db.getProductByName(ctx.queryParams["prod"]).id - + cart.quantity = parseInt(ctx.queryParams["quantity"]) + + if cart.quantity == 0: + cart.quantity = 1 + db.addToCart(cart) ctx.redirect("/cart") +"/remove-from-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.removeFromCart(cart) + + ctx.redirect("/cart") + "/checkout" -> get: var diff --git a/src/a3a/cart.nimja b/src/a3a/cart.nimja index dfe6d3e..fc7a4cf 100644 --- a/src/a3a/cart.nimja +++ b/src/a3a/cart.nimja @@ -117,54 +117,6 @@ - {# - - Image - - -

Top Up T-Shirt

- - $49.00 - -
-
- -
- -
- -
-
- - - $49.00 - X - - - - - Image - - -

Polo Shirt

- - $49.00 - -
-
- -
- -
- -
-
- - - $49.00 - X - #} - {% for (id, product) in products.pairs() %} @@ -179,7 +131,7 @@
- +
@@ -187,7 +139,7 @@ ₹{{product.price}} - X + X {% endfor %} @@ -255,4 +207,12 @@ + {% endblock %} \ No newline at end of file diff --git a/src/a3c/cart.nim b/src/a3c/cart.nim index f6ac594..25d2b7a 100644 --- a/src/a3c/cart.nim +++ b/src/a3c/cart.nim @@ -40,4 +40,13 @@ proc getUserCart*(db: DbConn, userId: int): seq[Cart] = 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 + var cartDetails = getUserCart(db, cart.userId) + for d, f in cartDetails: + if f.userId == cart.userId and f.productId == cart.productId: + db.exec(sql"UPDATE cart SET quantity=? WHERE user_id=? AND product_id=?", f.quantity + cart.quantity, cart.userId, cart.productId) + return + + db.exec(sql"INSERT INTO cart (user_id, product_id, quantity) VALUES (?, ?, ?)", cart.userId, cart.productId, cart.quantity) + +proc removeFromCart*(db: DbConn, cart: Cart) = + db.exec(sql"DELETE FROM cart WHERE user_id=? AND product_id=?", cart.userId, cart.productId) \ No newline at end of file