076
This commit is contained in:
parent
e9d4f78f22
commit
927a5a5e96
100
src/a3.nim
100
src/a3.nim
@ -5,8 +5,8 @@ import
|
||||
nimja/parser,
|
||||
strutils,
|
||||
./a3pkg/[models, mics, htmx],
|
||||
./a3c/[products, users, cart, orders],
|
||||
./[admin, cart, about, index, checkout]
|
||||
./a3c/[products, users, cart],
|
||||
./[admin, cart, about, index, checkout, validation]
|
||||
|
||||
"/" -> [get, post]: ctx.index()
|
||||
|
||||
@ -18,94 +18,16 @@ import
|
||||
"/remove-from-cart" -> get: ctx.removeFromCart()
|
||||
|
||||
"/cart/checkout" -> [get, post]: ctx.checkOut()
|
||||
|
||||
"/validation/checkout" -> post: ctx.validationCheckOut()
|
||||
"/validation/lname" -> post:
|
||||
var lname = ctx.urlForm["c_lname"]
|
||||
var val: Validity
|
||||
if lname == "":
|
||||
val.message = "Last Name is Required"
|
||||
val.mark = "is-invalid"
|
||||
else:
|
||||
val.message = ""
|
||||
val.mark = ""
|
||||
ctx.send sendLastName(lname, val)
|
||||
|
||||
"/validation/fname" -> post:
|
||||
var fname = ctx.urlForm["c_fname"]
|
||||
var val: Validity
|
||||
if fname == "":
|
||||
val.message = "First Name is Required"
|
||||
val.mark = "is-invalid"
|
||||
else:
|
||||
val.message = ""
|
||||
val.mark = ""
|
||||
ctx.send sendFirstName(fname, val)
|
||||
|
||||
"/validation/address" -> post:
|
||||
var address = ctx.urlForm["c_address"]
|
||||
var val: Validity
|
||||
if address == "":
|
||||
val.message = "Address is Required"
|
||||
val.mark = "is-invalid"
|
||||
else:
|
||||
val.message = ""
|
||||
val.mark = ""
|
||||
ctx.send sendAddress(address, val)
|
||||
|
||||
"/validation/state" -> post:
|
||||
var state = ctx.urlForm["c_state_country"]
|
||||
var val: Validity
|
||||
if state == "":
|
||||
val.message = "State is Required"
|
||||
val.mark = "is-invalid"
|
||||
else:
|
||||
val.message = ""
|
||||
val.mark = ""
|
||||
ctx.send sendState(state, val)
|
||||
|
||||
"/validation/zip" -> post:
|
||||
var zip = ctx.urlForm["c_postal_zip"]
|
||||
var val: Validity
|
||||
if zip == "":
|
||||
val.message = "Zip is Required"
|
||||
val.mark = "is-invalid"
|
||||
else:
|
||||
val.message = ""
|
||||
val.mark = ""
|
||||
ctx.send sendZip(zip, val)
|
||||
|
||||
"/validation/email" -> post:
|
||||
var email = ctx.urlForm["c_email_address"]
|
||||
var val: Validity
|
||||
if email == "":
|
||||
val.message = "Email is Required"
|
||||
val.mark = "is-invalid"
|
||||
else:
|
||||
val.message = ""
|
||||
val.mark = ""
|
||||
ctx.send sendEmail(email, val)
|
||||
|
||||
"/validation/phone" -> post:
|
||||
var phone = ctx.urlForm["c_phone"]
|
||||
var val: Validity
|
||||
if phone == "":
|
||||
val.message = "Phone is Required"
|
||||
val.mark = "is-invalid"
|
||||
else:
|
||||
val.message = ""
|
||||
val.mark = ""
|
||||
ctx.send sendPhone(phone, val)
|
||||
|
||||
"/validation/country" -> post:
|
||||
var country = ctx.urlForm["c_country"]
|
||||
var val: Validity
|
||||
if country == "1":
|
||||
val.message = ""
|
||||
val.mark = "is-invalid"
|
||||
else:
|
||||
val.message = ""
|
||||
val.mark = ""
|
||||
ctx.send sendCountry(country, val)
|
||||
"/validation/lname" -> post: ctx.validationLName()
|
||||
"/validation/fname" -> post: ctx.validationFName()
|
||||
"/validation/address" -> post: ctx.validationAddress()
|
||||
"/validation/state" -> post: ctx.validationState()
|
||||
"/validation/zip" -> post: ctx.validationZip()
|
||||
"/validation/email" -> post: ctx.validationEmail()
|
||||
"/validation/phone" -> post: ctx.validationPhone()
|
||||
"/validation/country" -> post: ctx.validationCountry()
|
||||
|
||||
"/contact" -> get:
|
||||
|
||||
|
110
src/checkout.nim
110
src/checkout.nim
@ -4,8 +4,8 @@ import
|
||||
os,
|
||||
nimja/parser,
|
||||
strutils,
|
||||
./a3pkg/[models, mics, htmx],
|
||||
./a3c/[products, users, cart, orders]
|
||||
./a3pkg/[models, mics],
|
||||
./a3c/[products, users, cart]
|
||||
|
||||
proc checkOut*(ctx: Context): string=
|
||||
var
|
||||
@ -51,108 +51,4 @@ proc checkOut*(ctx: Context): string=
|
||||
var product = db.getProductById(d.productId)
|
||||
products.add(product)
|
||||
|
||||
compileTemplateFile(getScriptDir() / "a3a" / "checkout.nimja")
|
||||
|
||||
proc validationCheckOut*(ctx: Context) =
|
||||
var
|
||||
db = newDatabase()
|
||||
cart: seq[Cart]
|
||||
products: seq[Products]
|
||||
productCount = 0
|
||||
form = ctx.urlForm
|
||||
val: Validity
|
||||
validity = initTable[string, Validity]()
|
||||
cookies = ctx.cookies
|
||||
|
||||
email = cookies.getOrDefault("email", "")
|
||||
password = cookies.getOrDefault("password", "")
|
||||
|
||||
productName = form.getOrDefault("prod", "")
|
||||
quantity = parseInt(form.getOrDefault("quantity", "0"))
|
||||
|
||||
var
|
||||
country = form["c_country"]
|
||||
firstName = form["c_fname"]
|
||||
lastName = form["c_lname"]
|
||||
address = form["c_address"]
|
||||
state = form["c_state_country"]
|
||||
zip = form["c_postal_zip"]
|
||||
email1 = form["c_email_address"]
|
||||
phone = form["c_phone"]
|
||||
password1: string
|
||||
|
||||
password1 = form.getOrDefault("password", "")
|
||||
|
||||
if email != "":
|
||||
productCount = micsCartProductCount(email, password)
|
||||
|
||||
if country != "" and firstName != "" and lastName != "" and address != "" and state != "" and zip != "" and email != "" and phone != "":
|
||||
var
|
||||
userId = db.getUserId(email, password)
|
||||
cart = db.getUserCart(userId)
|
||||
order: Orders
|
||||
user: User
|
||||
|
||||
order.userId = userId
|
||||
order.country = country
|
||||
order.address = address
|
||||
order.state = state
|
||||
order.postalCode = zip
|
||||
order.phoneNumber = phone
|
||||
|
||||
user.firstName = firstName
|
||||
user.lastName = lastName
|
||||
user.email = email1
|
||||
|
||||
if email == "":
|
||||
user.password = password1
|
||||
user.accessLevel = 1
|
||||
db.createPost(user)
|
||||
db.clearCart(userId)
|
||||
|
||||
var _ = db.createOrder(order)
|
||||
|
||||
for c, d in cart:
|
||||
var product = db.getProductById(d.productId)
|
||||
products.add(product)
|
||||
|
||||
ctx.send(sendThankYou())
|
||||
|
||||
else:
|
||||
|
||||
for a, b in form:
|
||||
if form[a] == "":
|
||||
val.name = ""
|
||||
val.message = "Field is Required"
|
||||
val.mark = "is-invalid"
|
||||
validity[a] = val
|
||||
else:
|
||||
val.name = b
|
||||
val.message = ""
|
||||
val.mark = ""
|
||||
if form[a] == "1": # This checks if the country is selected or not
|
||||
val.mark = "is-invalid"
|
||||
validity[a] = val
|
||||
|
||||
if productName == "":
|
||||
var
|
||||
userId = db.getUserId(email, password)
|
||||
cart = db.getUserCart(userId)
|
||||
|
||||
for c, d in cart:
|
||||
var product = db.getProductById(d.productId)
|
||||
products.add(product)
|
||||
|
||||
else:
|
||||
var
|
||||
product: Products
|
||||
ca: Cart
|
||||
|
||||
product.id = 1
|
||||
product.name = productName
|
||||
product.price = db.getPriceByProductName(productName)
|
||||
ca.quantity = quantity
|
||||
products.add(product)
|
||||
cart.add(ca)
|
||||
|
||||
ctx.send(sendCheckOut(validity, totalPriceHTML(products, cart)))
|
||||
compileTemplateFile(getScriptDir() / "a3a" / "checkout.nimja")
|
198
src/validation.nim
Normal file
198
src/validation.nim
Normal file
@ -0,0 +1,198 @@
|
||||
import
|
||||
mike,
|
||||
segfaults,
|
||||
strutils,
|
||||
./a3pkg/[models, mics, htmx],
|
||||
./a3c/[products, users, cart, orders]
|
||||
|
||||
proc validationCheckOut*(ctx: Context) =
|
||||
var
|
||||
db = newDatabase()
|
||||
cart: seq[Cart]
|
||||
products: seq[Products]
|
||||
productCount = 0
|
||||
form = ctx.urlForm
|
||||
val: Validity
|
||||
validity = initTable[string, Validity]()
|
||||
cookies = ctx.cookies
|
||||
|
||||
email = cookies.getOrDefault("email", "")
|
||||
password = cookies.getOrDefault("password", "")
|
||||
|
||||
productName = form.getOrDefault("prod", "")
|
||||
quantity = parseInt(form.getOrDefault("quantity", "0"))
|
||||
|
||||
var
|
||||
country = form["c_country"]
|
||||
firstName = form["c_fname"]
|
||||
lastName = form["c_lname"]
|
||||
address = form["c_address"]
|
||||
state = form["c_state_country"]
|
||||
zip = form["c_postal_zip"]
|
||||
email1 = form["c_email_address"]
|
||||
phone = form["c_phone"]
|
||||
password1: string
|
||||
|
||||
password1 = form.getOrDefault("password", "")
|
||||
|
||||
if email != "":
|
||||
productCount = micsCartProductCount(email, password)
|
||||
|
||||
if country != "" and firstName != "" and lastName != "" and address != "" and state != "" and zip != "" and email != "" and phone != "":
|
||||
var
|
||||
userId = db.getUserId(email, password)
|
||||
cart = db.getUserCart(userId)
|
||||
order: Orders
|
||||
user: User
|
||||
|
||||
order.userId = userId
|
||||
order.country = country
|
||||
order.address = address
|
||||
order.state = state
|
||||
order.postalCode = zip
|
||||
order.phoneNumber = phone
|
||||
|
||||
user.firstName = firstName
|
||||
user.lastName = lastName
|
||||
user.email = email1
|
||||
|
||||
if email == "":
|
||||
user.password = password1
|
||||
user.accessLevel = 1
|
||||
db.createPost(user)
|
||||
db.clearCart(userId)
|
||||
|
||||
var _ = db.createOrder(order)
|
||||
|
||||
for c, d in cart:
|
||||
var product = db.getProductById(d.productId)
|
||||
products.add(product)
|
||||
|
||||
ctx.send(sendThankYou())
|
||||
|
||||
else:
|
||||
|
||||
for a, b in form:
|
||||
if form[a] == "":
|
||||
val.name = ""
|
||||
val.message = "Field is Required"
|
||||
val.mark = "is-invalid"
|
||||
validity[a] = val
|
||||
else:
|
||||
val.name = b
|
||||
val.message = ""
|
||||
val.mark = ""
|
||||
if form[a] == "1": # This checks if the country is selected or not
|
||||
val.mark = "is-invalid"
|
||||
validity[a] = val
|
||||
|
||||
if productName == "":
|
||||
var
|
||||
userId = db.getUserId(email, password)
|
||||
cart = db.getUserCart(userId)
|
||||
|
||||
for c, d in cart:
|
||||
var product = db.getProductById(d.productId)
|
||||
products.add(product)
|
||||
|
||||
else:
|
||||
var
|
||||
product: Products
|
||||
ca: Cart
|
||||
|
||||
product.id = 1
|
||||
product.name = productName
|
||||
product.price = db.getPriceByProductName(productName)
|
||||
ca.quantity = quantity
|
||||
products.add(product)
|
||||
cart.add(ca)
|
||||
|
||||
ctx.send(sendCheckOut(validity, totalPriceHTML(products, cart)))
|
||||
|
||||
proc validationLName*(ctx: Context) =
|
||||
var lname = ctx.urlForm["c_lname"]
|
||||
var val: Validity
|
||||
if lname == "":
|
||||
val.message = "Last Name is Required"
|
||||
val.mark = "is-invalid"
|
||||
else:
|
||||
val.message = ""
|
||||
val.mark = ""
|
||||
ctx.send sendLastName(lname, val)
|
||||
|
||||
proc validationFName*(ctx: Context) =
|
||||
var fname = ctx.urlForm["c_fname"]
|
||||
var val: Validity
|
||||
if fname == "":
|
||||
val.message = "First Name is Required"
|
||||
val.mark = "is-invalid"
|
||||
else:
|
||||
val.message = ""
|
||||
val.mark = ""
|
||||
ctx.send sendFirstName(fname, val)
|
||||
|
||||
proc validationAddress*(ctx: Context) =
|
||||
var address = ctx.urlForm["c_address"]
|
||||
var val: Validity
|
||||
if address == "":
|
||||
val.message = "Address is Required"
|
||||
val.mark = "is-invalid"
|
||||
else:
|
||||
val.message = ""
|
||||
val.mark = ""
|
||||
ctx.send sendAddress(address, val)
|
||||
|
||||
proc validationState*(ctx: Context) =
|
||||
var state = ctx.urlForm["c_state_country"]
|
||||
var val: Validity
|
||||
if state == "":
|
||||
val.message = "State is Required"
|
||||
val.mark = "is-invalid"
|
||||
else:
|
||||
val.message = ""
|
||||
val.mark = ""
|
||||
ctx.send sendState(state, val)
|
||||
|
||||
proc validationZip*(ctx: Context) =
|
||||
var zip = ctx.urlForm["c_postal_zip"]
|
||||
var val: Validity
|
||||
if zip == "":
|
||||
val.message = "Zip is Required"
|
||||
val.mark = "is-invalid"
|
||||
else:
|
||||
val.message = ""
|
||||
val.mark = ""
|
||||
ctx.send sendZip(zip, val)
|
||||
|
||||
proc validationEmail*(ctx: Context) =
|
||||
var email = ctx.urlForm["c_email_address"]
|
||||
var val: Validity
|
||||
if email == "":
|
||||
val.message = "Email is Required"
|
||||
val.mark = "is-invalid"
|
||||
else:
|
||||
val.message = ""
|
||||
val.mark = ""
|
||||
ctx.send sendEmail(email, val)
|
||||
|
||||
proc validationPhone*(ctx: Context) =
|
||||
var phone = ctx.urlForm["c_phone"]
|
||||
var val: Validity
|
||||
if phone == "":
|
||||
val.message = "Phone is Required"
|
||||
val.mark = "is-invalid"
|
||||
else:
|
||||
val.message = ""
|
||||
val.mark = ""
|
||||
ctx.send sendPhone(phone, val)
|
||||
|
||||
proc validationCountry*(ctx: Context) =
|
||||
var country = ctx.urlForm["c_country"]
|
||||
var val: Validity
|
||||
if country == "1":
|
||||
val.message = ""
|
||||
val.mark = "is-invalid"
|
||||
else:
|
||||
val.message = ""
|
||||
val.mark = ""
|
||||
ctx.send sendCountry(country, val)
|
Loading…
Reference in New Issue
Block a user