This commit is contained in:
Aritra Banik 2024-03-10 14:28:28 +05:30
parent e929323f1f
commit 26109ca6f8
4 changed files with 113 additions and 100 deletions

Binary file not shown.

BIN
src/a3

Binary file not shown.

View File

@ -6,7 +6,7 @@ import
strutils,
./a3pkg/[models, mics, htmx],
./a3c/[products, users, cart, orders],
./admin
./[admin, cart]
"/" -> [get, post]:
@ -41,109 +41,14 @@ import
compileTemplateFile(getScriptDir() / "a3a" / "about.nimja")
"/cart" -> get:
"/cart" -> get: ctx.cart()
var
db = newDatabase()
cookies = ctx.cookies
email = cookies.getOrDefault("email", "")
password = cookies.getOrDefault("password", "")
if email == "":
ctx.redirect("/login")
else:
var
userId = db.getUserId(email, password)
cart = db.getUserCart(userId)
products: seq[Products]
for c, d in cart:
var product = db.getProductById(d.productId)
products.add(product)
compileTemplateFile(getScriptDir() / "a3a" / "cart.nimja")
"/update-cart" -> get:
"/update-cart" -> get: ctx.updateCart()
var
db = newDatabase()
products: seq[Products]
cookies = ctx.cookies
email = cookies.getOrDefault("email", "")
password = cookies.getOrDefault("password", "")
if email == "":
ctx.redirect("/login")
else:
products = micsGetProducts(email, password)
var
userId = db.getUserId(email, password)
cart = db.getUserCart(userId)
cook = ctx.cookies
for d, e in cook:
if d.contains("_quantity") == true:
var h = d.split("_")
for i, j in cart:
if j.productId == parseInt(h[0]):
db.updateCart(e, j.id)
ctx.redirect("/cart")
"/add-to-cart" -> get:
"/add-to-cart" -> get: ctx.addToCart()
var
db = newDatabase()
cookies = ctx.cookies
email = cookies.getOrDefault("email", "")
password = cookies.getOrDefault("password", "")
if email == "":
ctx.redirect("/login")
else:
var
cart: Cart
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:
"/remove-from-cart" -> get: ctx.removeFromCart()
var
db = newDatabase()
cookies = ctx.cookies
email = cookies.getOrDefault("email", "")
password = cookies.getOrDefault("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")
"/cart/checkout" -> [get, post]:
var
db = newDatabase()

108
src/cart.nim Normal file
View File

@ -0,0 +1,108 @@
import
mike,
segfaults,
os,
nimja/parser,
strutils,
./a3pkg/[models, mics, htmx],
./a3c/[products, users, cart, orders],
./admin
proc cart*(ctx: Context): string=
var
db = newDatabase()
cookies = ctx.cookies
email = cookies.getOrDefault("email", "")
password = cookies.getOrDefault("password", "")
if email == "":
ctx.redirect("/login")
else:
var
userId = db.getUserId(email, password)
cart = db.getUserCart(userId)
products: seq[Products]
for c, d in cart:
var product = db.getProductById(d.productId)
products.add(product)
compileTemplateFile(getScriptDir() / "a3a" / "cart.nimja")
proc updateCart*(ctx: Context)=
var
db = newDatabase()
products: seq[Products]
cookies = ctx.cookies
email = cookies.getOrDefault("email", "")
password = cookies.getOrDefault("password", "")
if email == "":
ctx.redirect("/login")
else:
products = micsGetProducts(email, password)
var
userId = db.getUserId(email, password)
cart = db.getUserCart(userId)
cook = ctx.cookies
for d, e in cook:
if d.contains("_quantity") == true:
var h = d.split("_")
for i, j in cart:
if j.productId == parseInt(h[0]):
db.updateCart(e, j.id)
ctx.redirect("/cart")
proc addToCart*(ctx: Context)=
var
db = newDatabase()
cookies = ctx.cookies
email = cookies.getOrDefault("email", "")
password = cookies.getOrDefault("password", "")
if email == "":
ctx.redirect("/login")
else:
var
cart: Cart
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")
proc removeFromCart*(ctx: Context)=
var
db = newDatabase()
cookies = ctx.cookies
email = cookies.getOrDefault("email", "")
password = cookies.getOrDefault("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")