diff --git a/src/a3 b/src/a3 index 841f694..f084521 100755 Binary files a/src/a3 and b/src/a3 differ diff --git a/src/a3.nim b/src/a3.nim index ea8270c..011f819 100644 --- a/src/a3.nim +++ b/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: diff --git a/src/checkout.nim b/src/checkout.nim index 6a3929b..8cfb212 100644 --- a/src/checkout.nim +++ b/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))) \ No newline at end of file + compileTemplateFile(getScriptDir() / "a3a" / "checkout.nimja") \ No newline at end of file diff --git a/src/validation.nim b/src/validation.nim new file mode 100644 index 0000000..254bddb --- /dev/null +++ b/src/validation.nim @@ -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) \ No newline at end of file