Shoppers/src/a3.nim

605 lines
12 KiB
Nim
Raw Normal View History

2024-02-03 16:37:29 +05:30
import
mike,
segfaults,
os,
nimja/parser,
2024-02-06 04:40:31 +05:30
strutils,
2024-02-08 03:02:04 +05:30
strformat,
2024-02-25 02:42:40 +05:30
./a3pkg/[models, mics, htmx],
2024-02-13 00:24:18 +05:30
./a3c/[products, users, cart, orders]
2024-02-03 16:37:29 +05:30
"/" -> [get, post]:
var
email: string
password: string
2024-02-03 21:00:30 +05:30
products: seq[Products]
2024-02-03 16:37:29 +05:30
try:
email = ctx.cookies["email"]
password = ctx.cookies["password"]
except:
email = ""
password = ""
if email == "":
echo "No cookie found."
else:
2024-02-03 21:00:30 +05:30
products = micsGetProducts(email, password)
2024-02-03 16:37:29 +05:30
compileTemplateFile(getScriptDir() / "a3a" / "index.nimja")
"/about" -> get:
2024-02-03 23:15:58 +05:30
var
email: string
password: string
products: seq[Products]
try:
email = ctx.cookies["email"]
password = ctx.cookies["password"]
except:
email = ""
password = ""
if email == "":
echo "No cookie found."
else:
products = micsGetProducts(email, password)
echo "Cookie found."
2024-02-03 16:37:29 +05:30
compileTemplateFile(getScriptDir() / "a3a" / "about.nimja")
"/cart" -> get:
var
email: string
password: string
2024-02-06 00:36:07 +05:30
db = newDatabase()
2024-02-03 16:37:29 +05:30
try:
email = ctx.cookies["email"]
password = ctx.cookies["password"]
except:
email = ""
password = ""
if email == "":
ctx.redirect("/login")
else:
var
2024-02-06 00:36:07 +05:30
userId = db.getUserId(email, password)
cart = db.getUserCart(userId)
2024-02-03 16:37:29 +05:30
products: seq[Products]
for c, d in cart:
2024-02-06 00:36:07 +05:30
var product = db.getProductById(d.productId)
2024-02-03 16:37:29 +05:30
products.add(product)
compileTemplateFile(getScriptDir() / "a3a" / "cart.nimja")
2024-02-08 03:02:04 +05:30
"/update-cart" -> get:
var
email: string
password: string
db = newDatabase()
products: seq[Products]
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
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")
2024-02-06 03:09:34 +05:30
"/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
2024-02-06 04:40:31 +05:30
cart.quantity = parseInt(ctx.queryParams["quantity"])
if cart.quantity == 0:
cart.quantity = 1
2024-02-06 03:09:34 +05:30
db.addToCart(cart)
ctx.redirect("/cart")
2024-02-06 04:40:31 +05:30
"/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")
2024-02-12 01:32:51 +05:30
"/checkout" -> get:
2024-02-03 23:15:58 +05:30
var
email: string
password: string
2024-02-06 00:36:07 +05:30
db = newDatabase()
2024-02-08 22:35:58 +05:30
productName= ""
quantity = 0
cart: seq[Cart]
products: seq[Products]
2024-02-11 02:53:43 +05:30
productCount = 0
2024-02-12 01:32:51 +05:30
countryError = ""
firstNameError = ""
lastNameError = ""
addressError = ""
stateError = ""
zipError = ""
emailError = ""
phoneError = ""
2024-02-13 00:24:18 +05:30
ch = ""
2024-02-03 23:15:58 +05:30
try:
email = ctx.cookies["email"]
password = ctx.cookies["password"]
except:
email = ""
password = ""
2024-02-11 01:32:09 +05:30
try:
productName = ctx.queryParams["prod"]
quantity = parseInt(ctx.queryParams["quantity"])
except:
productName = ""
quantity = 0
2024-02-08 22:35:58 +05:30
2024-02-11 01:32:09 +05:30
if email != "":
2024-02-11 02:53:43 +05:30
productCount = micsCartProductCount(email, password)
2024-02-10 21:58:50 +05:30
2024-02-11 02:53:43 +05:30
if productName == "" and email == "":
ctx.redirect("/login")
2024-02-10 21:58:50 +05:30
2024-02-11 02:53:43 +05:30
elif productName != "":
var
product: Products
ca: Cart
2024-02-10 21:58:50 +05:30
2024-02-11 02:53:43 +05:30
product.id = 1
product.name = productName
product.price = db.getPriceByProductName(productName)
ca.quantity = quantity
products.add(product)
cart.add(ca)
2024-02-13 00:24:18 +05:30
ch = "d"
2024-02-11 01:32:09 +05:30
2024-02-03 23:15:58 +05:30
else:
var
2024-02-06 00:36:07 +05:30
userId = db.getUserId(email, password)
2024-02-08 22:35:58 +05:30
cart = db.getUserCart(userId)
2024-02-03 23:15:58 +05:30
for c, d in cart:
2024-02-06 00:36:07 +05:30
var product = db.getProductById(d.productId)
2024-02-11 02:53:43 +05:30
echo product
2024-02-03 23:15:58 +05:30
products.add(product)
2024-02-08 22:35:58 +05:30
compileTemplateFile(getScriptDir() / "a3a" / "checkout.nimja")
2024-02-03 16:37:29 +05:30
2024-02-12 01:32:51 +05:30
"/checkout" -> post:
var
2024-02-23 02:15:28 +05:30
# res = Response()
2024-02-12 01:32:51 +05:30
email: string
password: string
db = newDatabase()
productName= ""
quantity = 0
cart: seq[Cart]
products: seq[Products]
productCount = 0
cookies = ctx.cookies
countryError = ""
firstNameError = ""
lastNameError = ""
addressError = ""
stateError = ""
zipError = ""
emailError = ""
phoneError = ""
2024-02-13 00:24:18 +05:30
passwordError = ""
ch = ""
2024-02-18 16:43:23 +05:30
echo "hi"
echo ctx.urlForm
echo "bye"
2024-02-12 01:32:51 +05:30
try:
email = ctx.cookies["email"]
password = ctx.cookies["password"]
except:
email = ""
password = ""
try:
productName = ctx.queryParams["prod"]
quantity = parseInt(ctx.queryParams["quantity"])
except:
productName = ""
quantity = 0
var
country = cookies["c_country"]
firstName = cookies["c_fname"]
lastName = cookies["c_lname"]
address = cookies["c_address"]
state = cookies["c_state_country"]
zip = cookies["c_postal_zip"]
email1 = cookies["c_email_address"]
phone = cookies["c_phone"]
2024-02-13 00:24:18 +05:30
password1: string
2024-02-12 01:32:51 +05:30
2024-02-13 00:24:18 +05:30
echo cookies
2024-02-12 01:32:51 +05:30
2024-02-13 00:24:18 +05:30
try:
password1 = cookies["password"]
except:
password1 = ""
2024-02-12 01:32:51 +05:30
2024-02-13 00:24:18 +05:30
if country == "": countryError = "Country is Required"
if firstName == "": firstNameError = "First Name is Required"
2024-02-12 01:32:51 +05:30
if lastName == "": lastNameError = "Last Name is Required"
if address == "": addressError = "Address is Required"
if state == "": stateError = "State is Required"
if zip == "": zipError = "Zip is Required"
if email1 == "": emailError = "Email is Required"
if phone == "": phoneError = "Phone is Required"
2024-02-13 00:24:18 +05:30
if email != "":
productCount = micsCartProductCount(email, password)
2024-02-12 01:32:51 +05:30
2024-02-13 00:24:18 +05:30
if countryError == "" and firstNameError == "" and lastNameError == "" and addressError == "" and stateError == "" and zipError == "" and emailError == "" and phoneError == "":
var
userId = db.getUserId(email, password)
cart = db.getUserCart(userId)
order: Orders
user: User
2024-02-12 01:32:51 +05:30
2024-02-13 00:24:18 +05:30
order.userId = userId
order.country = country
order.address = address
order.state = state
order.postalCode = zip
order.phoneNumber = phone
2024-02-12 01:32:51 +05:30
2024-02-13 00:24:18 +05:30
user.firstName = firstName
user.lastName = lastName
user.email = email1
2024-02-12 01:32:51 +05:30
2024-02-13 00:24:18 +05:30
if email == "":
user.password = password1
user.accessLevel = 1
db.createPost(user)
db.clearCart(userId)
2024-02-12 01:32:51 +05:30
2024-02-13 00:24:18 +05:30
var _ = db.createOrder(order)
2024-02-12 01:32:51 +05:30
2024-02-13 00:24:18 +05:30
for c, d in cart:
var product = db.getProductById(d.productId)
products.add(product)
2024-02-12 01:32:51 +05:30
2024-02-23 02:15:28 +05:30
compileTemplateFile(getScriptDir() / "a3a" / "checkout.nimja")
2024-02-12 01:32:51 +05:30
else:
2024-02-13 00:24:18 +05:30
if productName != "":
var
product: Products
ca: Cart
product.id = 1
product.name = productName
product.price = db.getPriceByProductName(productName)
ca.quantity = quantity
2024-02-12 01:32:51 +05:30
products.add(product)
2024-02-13 00:24:18 +05:30
cart.add(ca)
2024-02-11 02:53:43 +05:30
2024-02-13 00:24:18 +05:30
else:
var
userId = db.getUserId(email, password)
cart = db.getUserCart(userId)
for c, d in cart:
var product = db.getProductById(d.productId)
products.add(product)
compileTemplateFile(getScriptDir() / "a3a" / "checkout.nimja")
2024-02-11 02:53:43 +05:30
2024-02-25 02:42:40 +05:30
"/lname" -> post:
var lname = ctx.urlForm["c_lname"]
echo lname
var val: Validity
if lname == "":
val.message = "Last Name is Required"
val.class = "text-danger"
else:
val.message = ""
val.class = "text-success"
ctx.send sendLastName(lname, val)
2024-02-03 16:37:29 +05:30
"/contact" -> get:
2024-02-03 23:15:58 +05:30
var
email: string
password: string
2024-02-06 00:36:07 +05:30
db = newDatabase()
2024-02-03 23:15:58 +05:30
2024-02-03 23:45:39 +05:30
products: seq[Products]
2024-02-03 23:15:58 +05:30
try:
email = ctx.cookies["email"]
password = ctx.cookies["password"]
except:
email = ""
password = ""
if email != "" and password != "":
var
2024-02-06 00:36:07 +05:30
userId = db.getUserId(email, password)
cart = db.getUserCart(userId)
2024-02-03 23:15:58 +05:30
for c, d in cart:
2024-02-06 00:36:07 +05:30
var product = db.getProductById(d.productId)
2024-02-03 23:15:58 +05:30
products.add(product)
2024-02-03 16:37:29 +05:30
compileTemplateFile(getScriptDir() / "a3a" / "contact.nimja")
"/shop" -> get:
2024-02-03 23:15:58 +05:30
2024-02-03 16:37:29 +05:30
var
2024-02-03 23:15:58 +05:30
email: string
password: string
2024-02-06 00:36:07 +05:30
db = newDatabase()
2024-02-03 16:37:29 +05:30
2024-02-06 00:36:07 +05:30
availableProducts = db.availableProducts()
2024-02-03 23:45:39 +05:30
products: seq[Products]
2024-02-03 23:15:58 +05:30
try:
email = ctx.cookies["email"]
password = ctx.cookies["password"]
except:
email = ""
password = ""
if email != "" and password != "":
2024-02-04 00:07:01 +05:30
products = micsGetProducts(email, password)
2024-02-03 16:37:29 +05:30
compileTemplateFile(getScriptDir() / "a3a" / "shop.nimja")
"/shop-single" -> get:
2024-02-03 23:45:39 +05:30
2024-02-03 16:37:29 +05:30
var
2024-02-03 23:45:39 +05:30
email: string
password: string
2024-02-06 00:36:07 +05:30
db = newDatabase()
2024-02-03 16:37:29 +05:30
productName = ctx.queryParams["prod"]
2024-02-06 03:09:34 +05:30
product = db.getProductByName(productName)
2024-02-03 16:37:29 +05:30
2024-02-03 23:45:39 +05:30
products: seq[Products]
try:
email = ctx.cookies["email"]
password = ctx.cookies["password"]
except:
email = ""
password = ""
if email != "" and password != "":
2024-02-04 00:07:01 +05:30
products = micsGetProducts(email, password)
2024-02-03 23:45:39 +05:30
2024-02-03 16:37:29 +05:30
compileTemplateFile(getScriptDir() / "a3a" / "shop-single.nimja")
2024-02-25 02:42:40 +05:30
"/thankyou" -> get:
var
email: string
password: string
products: seq[Products]
try:
email = ctx.cookies["email"]
password = ctx.cookies["password"]
except:
email = ""
password = ""
if email == "":
ctx.redirect("/login")
else:
products = micsGetProducts(email, password)
2024-02-18 16:43:23 +05:30
2024-02-25 02:42:40 +05:30
ctx &= initCookie("c_country", "")
ctx &= initCookie("c_fname", "")
ctx &= initCookie("c_lname", "")
ctx &= initCookie("c_address", "")
ctx &= initCookie("c_state_country", "")
ctx &= initCookie("c_postal_zip", "")
ctx &= initCookie("c_email_address", "")
ctx &= initCookie("c_phone", "")
2024-02-04 02:09:46 +05:30
2024-02-25 02:42:40 +05:30
compileTemplateFile(getScriptDir() / "a3a" / "thankyou.nimja")
2024-02-03 16:37:29 +05:30
"/login" -> get:
var
loginError = ""
emailError = ""
passwordError = ""
email = ""
password = ""
compileTemplateFile(getScriptDir() / "a3a" / "login.nimja")
"/login" -> post:
var
email = ctx.urlForm["email"]
password = ctx.urlForm["password"]
2024-02-06 00:36:07 +05:30
db = newDatabase()
2024-02-03 16:37:29 +05:30
2024-02-06 00:36:07 +05:30
user = db.userAvailability(email, password)
2024-02-03 16:37:29 +05:30
loginError = ""
emailError = ""
passwordError = ""
2024-02-11 02:53:43 +05:30
productName: string
quantity: int
try:
2024-02-11 01:32:09 +05:30
productName = ctx.queryParams["prod"]
quantity = parseInt(ctx.queryParams["quantity"])
2024-02-11 02:53:43 +05:30
except:
productName = ""
quantity = 0
2024-02-03 16:37:29 +05:30
if user == true:
ctx &= initCookie("email", email)
ctx &= initCookie("password", password)
2024-02-11 01:32:09 +05:30
if quantity != 0:
2024-02-23 02:15:28 +05:30
# ctx.redirect("/checkout?prod=" & productName & "&quantity=" & $quantity)
ctx.redirect(fmt"/checkout?prod={productName}&quantity={quantity}")
2024-02-11 01:32:09 +05:30
else:
ctx.redirect("/")
2024-02-03 16:37:29 +05:30
else:
if email == "":
emailError = "Email is Required"
if password == "":
passwordError = "Password is Required"
if user == false:
loginError = "Invalid Login or Password"
compileTemplateFile(getScriptDir() / "a3a" / "login.nimja")
"/logout" -> get:
ctx &= initCookie("email", "")
ctx &= initCookie("password", "")
ctx.redirect("/login")
"/signup" -> get:
var
firstNameError = ""
lastNameError = ""
emailError = ""
passwordError = ""
user: User
user.firstName = ""
user.lastName = ""
user.email = ""
user.password = ""
compileTemplateFile(getScriptDir() / "a3a" / "signup.nimja")
"/signup" -> post:
var
form = ctx.urlForm
2024-02-06 00:36:07 +05:30
db = newDatabase()
2024-02-03 16:37:29 +05:30
user: User
firstNameError = ""
lastNameError = ""
emailError = ""
passwordError = ""
user.firstName = form["firstName"]
user.lastName = form["lastName"]
user.email = form["email"]
user.password = form["password"]
if user.firstName == "":
firstNameError = "First Name is Required"
if user.lastName == "":
lastNameError = "Last Name is Required"
if user.email == "":
emailError = "Email is Required"
if user.password == "":
passwordError = "Password is Required"
if firstNameError == "" and lastNameError == "" and emailError == "" and passwordError == "":
user.accessLevel = 1
2024-02-06 00:36:07 +05:30
db.createPost(user)
2024-02-03 16:37:29 +05:30
ctx.redirect("/login")
compileTemplateFile(getScriptDir() / "a3a" / "signup.nimja")
servePublic("src/a3b", "/a3b")
run()