13th Commit

This commit is contained in:
Aritra Banik 2024-02-06 00:36:07 +05:30
parent b1bd56841b
commit b735bdefb1
8 changed files with 113 additions and 92 deletions

BIN
src/a3

Binary file not shown.

View File

@ -12,7 +12,6 @@ import
email: string email: string
password: string password: string
products: seq[Products] products: seq[Products]
# echo ctx.cookies["email"]
try: try:
email = ctx.cookies["email"] email = ctx.cookies["email"]
@ -56,9 +55,9 @@ import
var var
email: string email: string
password: string password: string
db2 = newDatabase2() db = newDatabase()
db3 = newDatabase3() # db3 = newDatabase3()
db1 = newDatabase1() # db1 = newDatabase1()
try: try:
email = ctx.cookies["email"] email = ctx.cookies["email"]
@ -72,12 +71,12 @@ import
else: else:
var var
userId = db2.getUserId(email, password) userId = db.getUserId(email, password)
cart = db3.getUserCart(userId) cart = db.getUserCart(userId)
products: seq[Products] products: seq[Products]
for c, d in cart: for c, d in cart:
var product = db1.getProductById(d.productId) var product = db.getProductById(d.productId)
products.add(product) products.add(product)
compileTemplateFile(getScriptDir() / "a3a" / "cart.nimja") compileTemplateFile(getScriptDir() / "a3a" / "cart.nimja")
@ -87,9 +86,9 @@ import
var var
email: string email: string
password: string password: string
db2 = newDatabase2() db = newDatabase()
db3 = newDatabase3() # db3 = newDatabase3()
db1 = newDatabase1() # db1 = newDatabase1()
try: try:
email = ctx.cookies["email"] email = ctx.cookies["email"]
@ -103,12 +102,12 @@ import
else: else:
var var
userId = db2.getUserId(email, password) userId = db.getUserId(email, password)
cart = db3.getUserCart(userId) cart = db.getUserCart(userId)
products: seq[Products] products: seq[Products]
for c, d in cart: for c, d in cart:
var product = db1.getProductById(d.productId) var product = db.getProductById(d.productId)
products.add(product) products.add(product)
compileTemplateFile(getScriptDir() / "a3a" / "checkout.nimja") compileTemplateFile(getScriptDir() / "a3a" / "checkout.nimja")
@ -118,9 +117,9 @@ import
var var
email: string email: string
password: string password: string
db2 = newDatabase2() db = newDatabase()
db3 = newDatabase3() # db3 = newDatabase3()
db1 = newDatabase1() # db1 = newDatabase1()
products: seq[Products] products: seq[Products]
@ -133,11 +132,11 @@ import
if email != "" and password != "": if email != "" and password != "":
var var
userId = db2.getUserId(email, password) userId = db.getUserId(email, password)
cart = db3.getUserCart(userId) cart = db.getUserCart(userId)
for c, d in cart: for c, d in cart:
var product = db1.getProductById(d.productId) var product = db.getProductById(d.productId)
products.add(product) products.add(product)
compileTemplateFile(getScriptDir() / "a3a" / "contact.nimja") compileTemplateFile(getScriptDir() / "a3a" / "contact.nimja")
@ -147,9 +146,9 @@ import
var var
email: string email: string
password: string password: string
db1 = newDatabase1() db = newDatabase()
availableProducts = db1.availableProducts() availableProducts = db.availableProducts()
products: seq[Products] products: seq[Products]
try: try:
@ -169,11 +168,11 @@ import
var var
email: string email: string
password: string password: string
db1 = newDatabase1() db = newDatabase()
productName = ctx.queryParams["prod"] productName = ctx.queryParams["prod"]
product = db1.getProduct(productName) product = db.getProduct(productName)
products: seq[Products] products: seq[Products]
@ -226,9 +225,9 @@ import
email = ctx.urlForm["email"] email = ctx.urlForm["email"]
password = ctx.urlForm["password"] password = ctx.urlForm["password"]
db2 = newDatabase2() db = newDatabase()
user = db2.userAvailability(email, password) user = db.userAvailability(email, password)
loginError = "" loginError = ""
emailError = "" emailError = ""
@ -281,7 +280,7 @@ import
var var
form = ctx.urlForm form = ctx.urlForm
db2 = newDatabase2() db = newDatabase()
user: User user: User
firstNameError = "" firstNameError = ""
@ -309,7 +308,7 @@ import
if firstNameError == "" and lastNameError == "" and emailError == "" and passwordError == "": if firstNameError == "" and lastNameError == "" and emailError == "" and passwordError == "":
user.accessLevel = 1 user.accessLevel = 1
db2.createPost(user) db.createPost(user)
ctx.redirect("/login") ctx.redirect("/login")
compileTemplateFile(getScriptDir() / "a3a" / "signup.nimja") compileTemplateFile(getScriptDir() / "a3a" / "signup.nimja")

View File

@ -105,7 +105,7 @@
</div> </div>
<div class="row mb-5"> <div class="row mb-5">
{% for (id, product) in db1.availableProducts().pairs() %} {% for (id, product) in db.availableProducts().pairs() %}
<div class="col-sm-6 col-lg-4 mb-4" data-aos="fade-up"> <div class="col-sm-6 col-lg-4 mb-4" data-aos="fade-up">
<div class="block-4 text-center border"> <div class="block-4 text-center border">
<figure class="block-4-image"> <figure class="block-4-image">

View File

@ -2,19 +2,19 @@ import db_connector/db_sqlite, strutils
import ../a3pkg/models import ../a3pkg/models
type # type
Database = ref object # Database = ref object
db: DbConn # db: DbConn
proc newDatabase3*(filename = "db5.sqlite3"): Database = # proc newDatabase3*(filename = "db5.sqlite3"): Database =
new result # new result
result.db = open(filename, "", "", "") # result.db = open(filename, "", "", "")
proc close*(database: Database) = proc close*(db: DbConn) =
database.db.close() db.close()
proc setupCart*(database: Database) = proc setupCart*(db: DbConn) =
database.db.exec(sql""" db.exec(sql"""
CREATE TABLE IF NOT EXISTS cart ( CREATE TABLE IF NOT EXISTS cart (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE, user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE,
@ -25,15 +25,15 @@ proc setupCart*(database: Database) =
) )
""") """)
proc createPost*(database: Database, cart: Cart) = proc createPost*(db: DbConn, cart: Cart) =
database.db.exec(sql"INSERT INTO cart (?, ?, ?, ?, ?);", cart.userId, cart.productId, cart.quantity, cart.created_at, cart.updated_at) db.exec(sql"INSERT INTO cart (?, ?, ?, ?, ?);", cart.userId, cart.productId, cart.quantity, cart.created_at, cart.updated_at)
proc drop*(database: Database) = proc drop*(db: DbConn) =
database.db.exec(sql"DROP TABLE IF EXISTS cart") db.exec(sql"DROP TABLE IF EXISTS cart")
proc getUserCart*(database: Database, userId: int): seq[Cart] = proc getUserCart*(db: DbConn, userId: int): seq[Cart] =
var var
row = database.db.getAllRows(sql"SELECT * FROM cart WHERE user_id=?", userId) row = db.getAllRows(sql"SELECT * FROM cart WHERE user_id=?", userId)
cartDetails: seq[Cart] cartDetails: seq[Cart]
for b, c in row: for b, c in row:

View File

@ -10,11 +10,11 @@ proc newDatabase4*(filename = "db5.sqlite3"): Database =
new result new result
result.db = open(filename, "", "", "") result.db = open(filename, "", "", "")
proc close*(database: Database) = proc close*(db: DbConn) =
database.db.close() db.close()
proc setupOrders*(database: Database) = proc setupOrders*(db: DbConn) =
database.db.exec(sql""" db.exec(sql"""
CREATE TABLE IF NOT EXISTS orders ( CREATE TABLE IF NOT EXISTS orders (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE, user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE,
@ -27,10 +27,10 @@ proc setupOrders*(database: Database) =
); );
""") """)
proc createPost*(database: Database, order:Orders): int64 = proc createPost*(db: DbConn, order:Orders): int64 =
var newID = database.db.insertID(sql"INSERT INTO orders (user_id, product_id, quantity, order_status, order_date) VALUES (?, ?, ?, ?, ?);", var newID = db.insertID(sql"INSERT INTO orders (user_id, product_id, quantity, order_status, order_date) VALUES (?, ?, ?, ?, ?);",
order.userId, order.productId, order.quantity, order.orderStatus, order.orderDate) order.userId, order.productId, order.quantity, order.orderStatus, order.orderDate)
return newID return newID
proc drop*(database: Database) = proc drop*(db: DbConn) =
database.db.exec(sql"DROP TABLE IF EXISTS orders") db.exec(sql"DROP TABLE IF EXISTS orders")

View File

@ -10,11 +10,11 @@ proc newDatabase1*(filename = "db5.sqlite3"): Database =
new result new result
result.db = open(filename, "", "", "") result.db = open(filename, "", "", "")
proc close*(database: Database) = proc close*(db: DbConn) =
database.db.close() db.close()
proc setupProducts*(database: Database) = proc setupProducts*(db: DbConn) =
database.db.exec(sql""" db.exec(sql"""
CREATE TABLE IF NOT EXISTS products ( CREATE TABLE IF NOT EXISTS products (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL,
@ -29,18 +29,18 @@ proc setupProducts*(database: Database) =
); );
""") """)
proc setupProductsIndex*(database: Database) = proc setupProductsIndex*(db: DbConn) =
database.db.exec(sql""" db.exec(sql"""
CREATE UNIQUE INDEX IF NOT EXISTS products_name_idx ON products (name); CREATE UNIQUE INDEX IF NOT EXISTS products_name_idx ON products (name);
""") """)
proc createPost*(database: Database, product:Products): int64 = proc createPost*(db: DbConn, product:Products): int64 =
var newID = database.db.insertID(sql"INSERT INTO products (name, description, type, Page_name, price, pic_URL, quantity) VALUES (?, ?, ?, ?, ?);", var newID = db.insertID(sql"INSERT INTO products (name, description, type, Page_name, price, pic_URL, quantity) VALUES (?, ?, ?, ?, ?);",
product.name, product.description, product.productType, product.pageName, product.price, product.pic_URL, product.quantity) product.name, product.description, product.productType, product.pageName, product.price, product.pic_URL, product.quantity)
return newID return newID
proc availableProducts*(database: Database): seq[Products]= proc availableProducts*(db: DbConn): seq[Products]=
var rows = database.db.getAllRows(sql"SELECT * FROM products WHERE quantity > 0") var rows = db.getAllRows(sql"SELECT * FROM products WHERE quantity > 0")
var products: seq[Products] var products: seq[Products]
for a, b in rows: for a, b in rows:
var product: Products var product: Products
@ -57,8 +57,8 @@ proc availableProducts*(database: Database): seq[Products]=
return products return products
proc getProduct*(database: Database, name: string): Products = proc getProduct*(db: DbConn, name: string): Products =
var row = database.db.getRow(sql"SELECT * FROM products WHERE name = ?", name) var row = db.getRow(sql"SELECT * FROM products WHERE name = ?", name)
var product: Products var product: Products
product.id = parseInt(row[0]) product.id = parseInt(row[0])
product.name = row[1] product.name = row[1]
@ -71,8 +71,8 @@ proc getProduct*(database: Database, name: string): Products =
return product return product
proc getProductById*(database: Database, id: int): Products = proc getProductById*(db: DbConn, id: int): Products =
var row = database.db.getRow(sql"SELECT * FROM products WHERE id = ?", id) var row = db.getRow(sql"SELECT * FROM products WHERE id = ?", id)
var product: Products var product: Products
product.id = parseInt(row[0]) product.id = parseInt(row[0])
product.name = row[1] product.name = row[1]
@ -85,5 +85,5 @@ proc getProductById*(database: Database, id: int): Products =
return product return product
proc drop*(database: Database) = proc drop*(db: DbConn) =
database.db.exec(sql"DROP TABLE IF EXISTS products") db.exec(sql"DROP TABLE IF EXISTS products")

View File

@ -4,19 +4,19 @@ import
import ../a3pkg/models import ../a3pkg/models
type # type
Database = ref object # Database = ref object
db: DbConn # db: DbConn
proc newDatabase2*(filename = "db5.sqlite3"): Database = # proc newDatabase2*(filename = "db5.sqlite3"): Database =
new result # new result
result.db = open(filename, "", "", "") # result.db = open(filename, "", "", "")
proc close*(database: Database) = proc close*(db: DbConn) =
database.db.close() db.close()
proc setupUsers*(database: Database) = proc setupUsers*(db: DbConn) =
database.db.exec(sql""" db.exec(sql"""
CREATE TABLE IF NOT EXISTS users ( CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
first_name VARCHAR(255) NOT NULL, first_name VARCHAR(255) NOT NULL,
@ -29,21 +29,36 @@ proc setupUsers*(database: Database) =
) )
""") """)
proc createPost*(database: Database, user: User) = proc createPost*(db: DbConn, user: User) =
database.db.exec(sql"INSERT INTO users (first_name, last_name, email, password, access_level) VALUES (?, ?, ?, ?, ?);", user.firstName, user.lastName, user.email, user.password, user.accessLevel) db.exec(sql"INSERT INTO users (first_name, last_name, email, password, access_level) VALUES (?, ?, ?, ?, ?);", user.firstName, user.lastName, user.email, user.password, user.accessLevel)
proc drop*(database: Database) = proc drop*(db: DbConn) =
database.db.exec(sql"DROP TABLE IF EXISTS users") db.exec(sql"DROP TABLE IF EXISTS users")
proc userAvailability*(database: Database, user, password: string): bool = proc userAvailability*(db: DbConn, user, password: string): bool =
var row = database.db.getRow(sql"SELECT * FROM users WHERE email = ? and password = ?;", user, password) var row = db.getRow(sql"SELECT * FROM users WHERE email = ? and password = ?;", user, password)
if row[0] != "": if row[0] != "":
return true return true
else: else:
return false return false
proc getUserId*(database: Database, user, password: string): int = proc getUserId*(db: DbConn, user, password: string): int =
var row = database.db.getRow(sql"SELECT * FROM users WHERE email = ? and password = ?;", user, password) var row = db.getRow(sql"SELECT * FROM users WHERE email = ? and password = ?;", user, password)
result = parseInt(row[0]) result = parseInt(row[0])
proc getUser*(db: DbConn, email, password: string): User =
var
row = db.getRow(sql"SELECT * FROM users WHERE email = ? and password = ?;", email, password)
user: User
user.id = parseInt(row[0])
user.firstName = row[1]
user.lastName = row[2]
user.email = row[3]
user.password = row[4]
user.accessLevel = parseInt(row[7])
return user

View File

@ -1,19 +1,26 @@
import import
db_connector/db_sqlite,
./models, ./models,
../a3c/[cart, products, users] ../a3c/[cart, products, users]
proc newDatabase*(filename = "db5.sqlite3"): DbConn =
result = open(filename, "", "", "")
# proc newDatabase1*(filename = "db5.sqlite3"): DbConn =
# result = open(filename, "", "", "")
proc micsGetProducts*(email, password: string): seq[Products]= proc micsGetProducts*(email, password: string): seq[Products]=
var var
db1 = newDatabase1() # db1 = newDatabase1()
db2 = newDatabase2() db = newDatabase()
db3 = newDatabase3() # db3 = newDatabase3()
userId = db2.getUserId(email, password) userId = db.getUserId(email, password)
cart = db3.getUserCart(userId) cart = db.getUserCart(userId)
products: seq[Products] products: seq[Products]
for c, d in cart: for c, d in cart:
var product = db1.getProductById(d.productId) var product = db.getProductById(d.productId)
products.add(product) products.add(product)
return products return products