19th Commit
This commit is contained in:
parent
c2ef8d11be
commit
bb7cf556b3
BIN
db5.sqlite3
BIN
db5.sqlite3
Binary file not shown.
44
src/a3.nim
44
src/a3.nim
@ -4,6 +4,7 @@ import
|
||||
os,
|
||||
nimja/parser,
|
||||
strutils,
|
||||
strformat,
|
||||
./a3pkg/[models, mics],
|
||||
./a3c/[products, users, cart]
|
||||
|
||||
@ -80,6 +81,49 @@ import
|
||||
|
||||
compileTemplateFile(getScriptDir() / "a3a" / "cart.nimja")
|
||||
|
||||
"/update-cart" -> get:
|
||||
|
||||
var
|
||||
email: string
|
||||
password: string
|
||||
db = newDatabase()
|
||||
products: seq[Products]
|
||||
form = ctx.urlForm
|
||||
echo form
|
||||
|
||||
try:
|
||||
email = ctx.cookies["email"]
|
||||
password = ctx.cookies["password"]
|
||||
except:
|
||||
email = ""
|
||||
password = ""
|
||||
|
||||
if email == "":
|
||||
ctx.redirect("/login")
|
||||
|
||||
else:
|
||||
products = micsGetProducts(email, password)
|
||||
var
|
||||
userId = db.getUserId(email, password)
|
||||
cart = db.getUserCart(userId)
|
||||
cook = ctx.cookies
|
||||
|
||||
# echo cart
|
||||
# echo cook
|
||||
# db.updateCart(cook)
|
||||
|
||||
for d, e in cook:
|
||||
if d.contains("_quantity") == true:
|
||||
var h = d.split("_")
|
||||
echo h
|
||||
for i, j in cart:
|
||||
# echo i
|
||||
# echo j
|
||||
if j.productId == parseInt(h[0]):
|
||||
db.updateCart(e, j.id)
|
||||
|
||||
ctx.redirect("/cart")
|
||||
|
||||
"/add-to-cart" -> get:
|
||||
|
||||
var
|
||||
|
@ -103,7 +103,7 @@
|
||||
<div class="site-section">
|
||||
<div class="container">
|
||||
<div class="row mb-5">
|
||||
<form class="col-md-12" method="post">
|
||||
<form class="col-md-12" method="post" id="f1">
|
||||
<div class="site-blocks-table">
|
||||
<table class="table table-bordered">
|
||||
<thead>
|
||||
@ -152,7 +152,7 @@
|
||||
<div class="col-md-6">
|
||||
<div class="row mb-5">
|
||||
<div class="col-md-6 mb-3 mb-md-0">
|
||||
<button class="btn btn-primary btn-sm btn-block">Update Cart</button>
|
||||
<button id="b2" class="btn btn-primary btn-sm btn-block">Update Cart</button>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<button class="btn btn-outline-primary btn-sm btn-block">Continue Shopping</button>
|
||||
@ -181,22 +181,6 @@
|
||||
</div>
|
||||
{% var total = 0.0 %}
|
||||
{% for (id, product) in products.pairs() %}
|
||||
{# <div class="row mb-3">
|
||||
<div class="col-md-6">
|
||||
<span class="text-black">Subtotal</span>
|
||||
</div>
|
||||
<div class="col-md-6 text-right">
|
||||
<strong class="text-black">$230.00</strong>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row mb-5">
|
||||
<div class="col-md-6">
|
||||
<span class="text-black">Total</span>
|
||||
</div>
|
||||
<div class="col-md-6 text-right">
|
||||
<strong class="text-black">$230.00</strong>
|
||||
</div>
|
||||
</div> #}
|
||||
|
||||
<div class="row mb-3">
|
||||
<div class="col-md-6">
|
||||
@ -229,6 +213,28 @@
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
|
||||
document.getElementById("b2").onclick = function() {
|
||||
const form = document.getElementById('f1');
|
||||
const formData = new FormData(form);
|
||||
|
||||
{% for (id, product) in products.pairs() %}
|
||||
formData.append("{{product.id}}_quantity", document.getElementById("{{product.id}}_quantity").value);
|
||||
document.cookie = "{{product.id}}_quantity="+document.getElementById("{{product.id}}_quantity").value;
|
||||
{% endfor %}
|
||||
|
||||
{# document.getElementById("f1").submit(); #}
|
||||
|
||||
fetch('/update-cart', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
})
|
||||
{# location.reload(); #}
|
||||
window.location.href = '/update-cart';
|
||||
{# document.getElementById("f1").submit(); #}
|
||||
{# .then(response => response.json())
|
||||
.then(data => {
|
||||
console.log(data);
|
||||
}) #}
|
||||
}
|
||||
</script>
|
||||
{% endblock %}
|
@ -1,4 +1,4 @@
|
||||
import db_connector/db_sqlite, strutils
|
||||
import db_connector/db_sqlite, strutils, strtabs
|
||||
|
||||
import ../a3pkg/models
|
||||
|
||||
@ -49,4 +49,23 @@ proc addToCart*(db: DbConn, cart: Cart) =
|
||||
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)
|
||||
db.exec(sql"DELETE FROM cart WHERE user_id=? AND product_id=?", cart.userId, cart.productId)
|
||||
|
||||
proc updateCart*(db: DbConn, quantity: string, id: int) =
|
||||
# echo id
|
||||
# echo quantity
|
||||
# echo userId
|
||||
# echo db.getAllRows(sql"SELECT * FROM cart WHERE user_id=? AND product_id=?;", userId, parseInt(productId))
|
||||
db.exec(sql"UPDATE cart SET quantity=? WHERE id=?", quantity, id)
|
||||
|
||||
# proc updateCart*(db: DbConn, cookies: StringTableRef) =
|
||||
# echo cookies
|
||||
|
||||
# for d, e in cookies:
|
||||
# if d.contains("_quantity") == true:
|
||||
# var h = d.split("_")
|
||||
# echo h
|
||||
|
||||
# db.exec(sql"UPDATE cart SET quantity=? WHERE id=?", e, h[0])
|
||||
# echo parseInt(h[0])
|
||||
# echo parseInt(e)
|
6
src/dd.nim
Normal file
6
src/dd.nim
Normal file
@ -0,0 +1,6 @@
|
||||
import strformat
|
||||
|
||||
var i = @['o', 'p', 'r']
|
||||
echo i
|
||||
for q, w in i:
|
||||
echo fmt"q: {q}, w: {w}"
|
Loading…
Reference in New Issue
Block a user