diff --git a/db5.sqlite3 b/db5.sqlite3 index c555218..d63e862 100644 Binary files a/db5.sqlite3 and b/db5.sqlite3 differ diff --git a/src/a3 b/src/a3 index 2c691e3..841f694 100755 Binary files a/src/a3 and b/src/a3 differ diff --git a/src/a3.nim b/src/a3.nim index 6b18dd6..ea8270c 100644 --- a/src/a3.nim +++ b/src/a3.nim @@ -6,7 +6,7 @@ import strutils, ./a3pkg/[models, mics, htmx], ./a3c/[products, users, cart, orders], - ./[admin, cart, about, index] + ./[admin, cart, about, index, checkout] "/" -> [get, post]: ctx.index() @@ -17,157 +17,8 @@ import "/add-to-cart" -> get: ctx.addToCart() "/remove-from-cart" -> get: ctx.removeFromCart() -"/cart/checkout" -> [get, post]: - var - db = newDatabase() - cart: seq[Cart] = newSeq[Cart]() # Initialize empty cart - products: seq[Products] - productCount = 0 - ch = "" - cookies = ctx.cookies - qParams = ctx.queryParams - - email = cookies.getOrDefault("email", "") - password = cookies.getOrDefault("password", "") - - productName = qParams.getOrDefault("prod", "") - quantity = parseInt(qParams.getOrDefault("quantity", "0")) - - if email != "": - productCount = micsCartProductCount(email, password) - - if productName == "" and email == "": - ctx.redirect("/login") - - if productName != "": - 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) - ch = "d" - - 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") - -"/validation/checkout" -> post: - echo "post" - 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))) - +"/cart/checkout" -> [get, post]: ctx.checkOut() +"/validation/checkout" -> post: ctx.validationCheckOut() "/validation/lname" -> post: var lname = ctx.urlForm["c_lname"] var val: Validity @@ -315,30 +166,6 @@ import compileTemplateFile(getScriptDir() / "a3a" / "shop-single.nimja") -"/thankyou" -> get: - var - products: seq[Products] - cookies = ctx.cookies - - email = cookies.getOrDefault("email", "") - password = cookies.getOrDefault("password", "") - - if email == "": - ctx.redirect("/login") - else: - products = micsGetProducts(email, password) - - 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", "") - - compileTemplateFile(getScriptDir() / "a3a" / "thankyou.nimja") - "/login" -> get: var loginError = "" @@ -351,7 +178,6 @@ import compileTemplateFile(getScriptDir() / "a3a" / "login.nimja") "/login" -> post: - echo ctx.queryParams var email = ctx.urlForm["email"] password = ctx.urlForm["password"] diff --git a/src/admin.nim b/src/admin.nim index c123f90..492c47f 100644 --- a/src/admin.nim +++ b/src/admin.nim @@ -4,8 +4,4 @@ import proc admin*(ctx: Context): string= - # var ctx: Context # Replace YourContextType with the actual type of ctx - - # ctx.send "Namaste! I am Mike, your personal assistant. How can I help you today?" - compileTemplateFile(getScriptDir() / "a3a" / "admin" / "index.html") \ No newline at end of file diff --git a/src/checkout.nim b/src/checkout.nim new file mode 100644 index 0000000..6a3929b --- /dev/null +++ b/src/checkout.nim @@ -0,0 +1,158 @@ +import + mike, + segfaults, + os, + nimja/parser, + strutils, + ./a3pkg/[models, mics, htmx], + ./a3c/[products, users, cart, orders] + +proc checkOut*(ctx: Context): string= + var + db = newDatabase() + cart: seq[Cart] = newSeq[Cart]() # Initialize empty cart + products: seq[Products] + productCount = 0 + ch = "" + cookies = ctx.cookies + qParams = ctx.queryParams + + email = cookies.getOrDefault("email", "") + password = cookies.getOrDefault("password", "") + + productName = qParams.getOrDefault("prod", "") + quantity = parseInt(qParams.getOrDefault("quantity", "0")) + + if email != "": + productCount = micsCartProductCount(email, password) + + if productName == "" and email == "": + ctx.redirect("/login") + + if productName != "": + 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) + ch = "d" + + 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") + +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