23rd Commit

This commit is contained in:
Aritra Banik 2024-02-08 22:35:58 +05:30
parent ee0d6ba78d
commit c4354da63e
5 changed files with 46 additions and 24 deletions

BIN
src/a3

Binary file not shown.

View File

@ -181,6 +181,11 @@ import
email: string email: string
password: string password: string
db = newDatabase() db = newDatabase()
productName= ""
quantity = 0
cart: seq[Cart]
products: seq[Products]
price = 0.0
try: try:
email = ctx.cookies["email"] email = ctx.cookies["email"]
@ -190,19 +195,28 @@ import
password = "" password = ""
if email == "": if email == "":
ctx.redirect("/login") try:
productName = ctx.queryParams["prod"]
quantity = parseInt(ctx.queryParams["quantity"])
except:
productName = ""
quantity = 0
if productName == "":
ctx.redirect("/login")
price = db.getPriceByProductName(productName)
else: else:
var var
userId = db.getUserId(email, password) userId = db.getUserId(email, password)
cart = db.getUserCart(userId) cart = db.getUserCart(userId)
products: seq[Products]
for c, d in cart: for c, d in cart:
var product = db.getProductById(d.productId) var product = db.getProductById(d.productId)
products.add(product) products.add(product)
compileTemplateFile(getScriptDir() / "a3a" / "checkout.nimja") compileTemplateFile(getScriptDir() / "a3a" / "checkout.nimja")
"/contact" -> get: "/contact" -> get:

View File

@ -68,7 +68,7 @@
<div class="row mb-5"> <div class="row mb-5">
<div class="col-md-12"> <div class="col-md-12">
<div class="border p-4 rounded" role="alert"> <div class="border p-4 rounded" role="alert">
Returning customer? <a href="#">Click here</a> to login Returning customer? <a href="/login">Click here</a> to login
</div> </div>
</div> </div>
</div> </div>
@ -88,6 +88,7 @@
<option value="7">Bahrain</option> <option value="7">Bahrain</option>
<option value="8">Colombia</option> <option value="8">Colombia</option>
<option value="9">Dominican Republic</option> <option value="9">Dominican Republic</option>
<option value="10">India</option>
</select> </select>
</div> </div>
<div class="form-group row"> <div class="form-group row">
@ -172,6 +173,7 @@
<option value="7">Bahrain</option> <option value="7">Bahrain</option>
<option value="8">Colombia</option> <option value="8">Colombia</option>
<option value="9">Dominican Republic</option> <option value="9">Dominican Republic</option>
<option value="10">India</option>
</select> </select>
</div> </div>
@ -268,24 +270,14 @@
<th>Total</th> <th>Total</th>
</thead> </thead>
<tbody> <tbody>
{# <tr>
<td>Top Up T-Shirt <strong class="mx-2">x</strong> 1</td>
<td>$250.00</td>
</tr>
<tr>
<td>Polo Shirt <strong class="mx-2">x</strong> 1</td>
<td>$100.00</td>
</tr>
<tr>
<td class="text-black font-weight-bold"><strong>Cart Subtotal</strong></td>
<td class="text-black">$350.00</td>
</tr>
<tr>
<td class="text-black font-weight-bold"><strong>Order Total</strong></td>
<td class="text-black font-weight-bold"><strong>$350.00</strong></td>
</tr> #}
{% var total = 0.0 %} {% var total = 0.0 %}
{% if email == "" %}
<tr>
<td>{{productName}} <strong class="mx-2">x</strong> {{quantity}}</td>
<td>₹{{toFloat(quantity)*price}}</td>
{% total = total + toFloat(quantity)*price %}
</tr>
{% else %}
{% for (id, product) in products.pairs() %} {% for (id, product) in products.pairs() %}
<tr> <tr>
<td>{{product.name}} <strong class="mx-2">x</strong> {{toFloat(cart[id].quantity)}}</td> <td>{{product.name}} <strong class="mx-2">x</strong> {{toFloat(cart[id].quantity)}}</td>
@ -293,6 +285,7 @@
</tr> </tr>
{% total = total + toFloat(cart[id].quantity)*product.price %} {% total = total + toFloat(cart[id].quantity)*product.price %}
{% endfor %} {% endfor %}
{% endif %}
<tr> <tr>
<td class="text-black font-weight-bold"><strong>Cart Subtotal</strong></td> <td class="text-black font-weight-bold"><strong>Cart Subtotal</strong></td>
<td class="text-black">₹{{total}}</td> <td class="text-black">₹{{total}}</td>

View File

@ -106,7 +106,11 @@
</div> </div>
</div> </div>
{% if email != "" %}
<p><a href="#" class="buy-now btn btn-sm btn-primary" onclick="addToCart()">Add To Cart</a></p> <p><a href="#" class="buy-now btn btn-sm btn-primary" onclick="addToCart()">Add To Cart</a></p>
{% else %}
<p><a href="#" class="buy-now btn btn-sm btn-primary" onclick="checkOut()">Check Out</a></p>
{% endif %}
</div> </div>
</div> </div>
@ -195,5 +199,12 @@
+ '?prod={{productName}}&quantity=' + '?prod={{productName}}&quantity='
+ quantity; + quantity;
} }
function checkOut() {
var quantity = document.getElementById("quantity").value;
window.location.href = '/checkout'
+ '?prod={{productName}}&quantity='
+ quantity;
}
</script> </script>
{% endblock %} {% endblock %}

View File

@ -79,3 +79,7 @@ proc getProductById*(db: DbConn, id: int): Products =
proc drop*(db: DbConn) = proc drop*(db: DbConn) =
db.exec(sql"DROP TABLE IF EXISTS products") db.exec(sql"DROP TABLE IF EXISTS products")
proc getPriceByProductName*(db: DbConn, name: string): float =
var row = db.getRow(sql"SELECT price FROM products WHERE name = ?", name)
return parseFloat(row[0])