16th Commit

This commit is contained in:
Aritra Banik 2024-02-06 04:40:31 +05:30
parent 83aa2b543d
commit ae020c1968
5 changed files with 54 additions and 52 deletions

Binary file not shown.

BIN
src/a3

Binary file not shown.

View File

@ -3,6 +3,7 @@ import
segfaults, segfaults,
os, os,
nimja/parser, nimja/parser,
strutils,
./a3pkg/[models, mics], ./a3pkg/[models, mics],
./a3c/[products, users, cart] ./a3c/[products, users, cart]
@ -102,11 +103,43 @@ import
cart.userId = db.getUserId(email, password) cart.userId = db.getUserId(email, password)
cart.productId = db.getProductByName(ctx.queryParams["prod"]).id cart.productId = db.getProductByName(ctx.queryParams["prod"]).id
cart.quantity = parseInt(ctx.queryParams["quantity"])
if cart.quantity == 0:
cart.quantity = 1
db.addToCart(cart) db.addToCart(cart)
ctx.redirect("/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: "/checkout" -> get:
var var

View File

@ -117,54 +117,6 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{# <tr>
<td class="product-thumbnail">
<img src="/a3b/images/cloth_1.jpg" alt="Image" class="img-fluid">
</td>
<td class="product-name">
<h2 class="h5 text-black">Top Up T-Shirt</h2>
</td>
<td>$49.00</td>
<td>
<div class="input-group mb-3" style="max-width: 120px;">
<div class="input-group-prepend">
<button class="btn btn-outline-primary js-btn-minus" type="button">&minus;</button>
</div>
<input type="text" class="form-control text-center" value="1" placeholder="" aria-label="Example text with button addon" aria-describedby="button-addon1">
<div class="input-group-append">
<button class="btn btn-outline-primary js-btn-plus" type="button">&plus;</button>
</div>
</div>
</td>
<td>$49.00</td>
<td><a href="#" class="btn btn-primary btn-sm">X</a></td>
</tr>
<tr>
<td class="product-thumbnail">
<img src="/a3b/images/cloth_2.jpg" alt="Image" class="img-fluid">
</td>
<td class="product-name">
<h2 class="h5 text-black">Polo Shirt</h2>
</td>
<td>$49.00</td>
<td>
<div class="input-group mb-3" style="max-width: 120px;">
<div class="input-group-prepend">
<button class="btn btn-outline-primary js-btn-minus" type="button">&minus;</button>
</div>
<input type="text" class="form-control text-center" value="1" placeholder="" aria-label="Example text with button addon" aria-describedby="button-addon1">
<div class="input-group-append">
<button class="btn btn-outline-primary js-btn-plus" type="button">&plus;</button>
</div>
</div>
</td>
<td>$49.00</td>
<td><a href="#" class="btn btn-primary btn-sm">X</a></td>
</tr> #}
{% for (id, product) in products.pairs() %} {% for (id, product) in products.pairs() %}
<tr> <tr>
<td class="product-thumbnail"> <td class="product-thumbnail">
@ -179,7 +131,7 @@
<div class="input-group-prepend"> <div class="input-group-prepend">
<button class="btn btn-outline-primary js-btn-minus" type="button">&minus;</button> <button class="btn btn-outline-primary js-btn-minus" type="button">&minus;</button>
</div> </div>
<input type="text" class="form-control text-center" value="1" placeholder="" aria-label="Example text with button addon" aria-describedby="button-addon1"> <input type="text" id="quantity" class="form-control text-center" value="{{cart[id].quantity}}" placeholder="" aria-label="Example text with button addon" aria-describedby="button-addon1">
<div class="input-group-append"> <div class="input-group-append">
<button class="btn btn-outline-primary js-btn-plus" type="button">&plus;</button> <button class="btn btn-outline-primary js-btn-plus" type="button">&plus;</button>
</div> </div>
@ -187,7 +139,7 @@
</td> </td>
<td>₹{{product.price}}</td> <td>₹{{product.price}}</td>
<td><a href="#" class="btn btn-primary btn-sm">X</a></td> <td><a href="/remove-from-cart?prod={{product.name}}" class="btn btn-primary btn-sm">X</a></td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
@ -255,4 +207,12 @@
</div> </div>
</div> </div>
</div> </div>
<script>
function removeFromCart(productName) {
var quantity = document.getElementById("quantity").value;
window.location.href = '/remove-from-cart'
+ '?prod='
+ productName;
}
</script>
{% endblock %} {% endblock %}

View File

@ -40,4 +40,13 @@ proc getUserCart*(db: DbConn, userId: int): seq[Cart] =
return cartDetails return cartDetails
proc addToCart*(db: DbConn, cart: Cart) = proc addToCart*(db: DbConn, cart: Cart) =
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) 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)