15th Commit
This commit is contained in:
parent
c02afc5321
commit
83aa2b543d
BIN
db5.sqlite3
BIN
db5.sqlite3
Binary file not shown.
30
src/a3.nim
30
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]
|
||||
|
||||
|
@ -99,14 +99,14 @@
|
||||
<div class="input-group-prepend">
|
||||
<button class="btn btn-outline-primary js-btn-minus" type="button">−</button>
|
||||
</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="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">+</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<p><a href="/cart" class="buy-now btn btn-sm btn-primary">Add To Cart</a></p>
|
||||
<p><a href="#" class="buy-now btn btn-sm btn-primary" onclick="addToCart()">Add To Cart</a></p>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
@ -188,4 +188,12 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
function addToCart() {
|
||||
var quantity = document.getElementById("quantity").value;
|
||||
window.location.href = '/add-to-cart'
|
||||
+ '?prod={{productName}}&quantity='
|
||||
+ quantity;
|
||||
}
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
@ -37,4 +37,7 @@ proc getUserCart*(db: DbConn, userId: int): seq[Cart] =
|
||||
|
||||
cartDetails.add(product)
|
||||
|
||||
return cartDetails
|
||||
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)
|
@ -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])
|
||||
|
Loading…
Reference in New Issue
Block a user