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

View File

@ -105,7 +105,7 @@
</div>
<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="block-4 text-center border">
<figure class="block-4-image">

View File

@ -2,19 +2,19 @@ import db_connector/db_sqlite, strutils
import ../a3pkg/models
type
Database = ref object
db: DbConn
# type
# Database = ref object
# db: DbConn
proc newDatabase3*(filename = "db5.sqlite3"): Database =
new result
result.db = open(filename, "", "", "")
# proc newDatabase3*(filename = "db5.sqlite3"): Database =
# new result
# result.db = open(filename, "", "", "")
proc close*(database: Database) =
database.db.close()
proc close*(db: DbConn) =
db.close()
proc setupCart*(database: Database) =
database.db.exec(sql"""
proc setupCart*(db: DbConn) =
db.exec(sql"""
CREATE TABLE IF NOT EXISTS cart (
id INTEGER PRIMARY KEY AUTOINCREMENT,
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) =
database.db.exec(sql"INSERT INTO cart (?, ?, ?, ?, ?);", cart.userId, cart.productId, cart.quantity, cart.created_at, cart.updated_at)
proc createPost*(db: DbConn, cart: Cart) =
db.exec(sql"INSERT INTO cart (?, ?, ?, ?, ?);", cart.userId, cart.productId, cart.quantity, cart.created_at, cart.updated_at)
proc drop*(database: Database) =
database.db.exec(sql"DROP TABLE IF EXISTS cart")
proc drop*(db: DbConn) =
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
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]
for b, c in row:

View File

@ -10,11 +10,11 @@ proc newDatabase4*(filename = "db5.sqlite3"): Database =
new result
result.db = open(filename, "", "", "")
proc close*(database: Database) =
database.db.close()
proc close*(db: DbConn) =
db.close()
proc setupOrders*(database: Database) =
database.db.exec(sql"""
proc setupOrders*(db: DbConn) =
db.exec(sql"""
CREATE TABLE IF NOT EXISTS orders (
id INTEGER PRIMARY KEY AUTOINCREMENT,
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 =
var newID = database.db.insertID(sql"INSERT INTO orders (user_id, product_id, quantity, order_status, order_date) VALUES (?, ?, ?, ?, ?);",
proc createPost*(db: DbConn, order:Orders): int64 =
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)
return newID
proc drop*(database: Database) =
database.db.exec(sql"DROP TABLE IF EXISTS orders")
proc drop*(db: DbConn) =
db.exec(sql"DROP TABLE IF EXISTS orders")

View File

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

View File

@ -4,19 +4,19 @@ import
import ../a3pkg/models
type
Database = ref object
db: DbConn
# type
# Database = ref object
# db: DbConn
proc newDatabase2*(filename = "db5.sqlite3"): Database =
new result
result.db = open(filename, "", "", "")
# proc newDatabase2*(filename = "db5.sqlite3"): Database =
# new result
# result.db = open(filename, "", "", "")
proc close*(database: Database) =
database.db.close()
proc close*(db: DbConn) =
db.close()
proc setupUsers*(database: Database) =
database.db.exec(sql"""
proc setupUsers*(db: DbConn) =
db.exec(sql"""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
first_name VARCHAR(255) NOT NULL,
@ -29,21 +29,36 @@ proc setupUsers*(database: Database) =
)
""")
proc createPost*(database: Database, 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)
proc createPost*(db: DbConn, user: User) =
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) =
database.db.exec(sql"DROP TABLE IF EXISTS users")
proc drop*(db: DbConn) =
db.exec(sql"DROP TABLE IF EXISTS users")
proc userAvailability*(database: Database, user, password: string): bool =
var row = database.db.getRow(sql"SELECT * FROM users WHERE email = ? and password = ?;", user, password)
proc userAvailability*(db: DbConn, user, password: string): bool =
var row = db.getRow(sql"SELECT * FROM users WHERE email = ? and password = ?;", user, password)
if row[0] != "":
return true
else:
return false
proc getUserId*(database: Database, user, password: string): int =
var row = database.db.getRow(sql"SELECT * FROM users WHERE email = ? and password = ?;", user, password)
proc getUserId*(db: DbConn, user, password: string): int =
var row = db.getRow(sql"SELECT * FROM users WHERE email = ? and password = ?;", user, password)
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
db_connector/db_sqlite,
./models,
../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]=
var
db1 = newDatabase1()
db2 = newDatabase2()
db3 = newDatabase3()
# db1 = newDatabase1()
db = newDatabase()
# db3 = newDatabase3()
userId = db2.getUserId(email, password)
cart = db3.getUserCart(userId)
userId = db.getUserId(email, password)
cart = db.getUserCart(userId)
products: seq[Products]
for c, d in cart:
var product = db1.getProductById(d.productId)
var product = db.getProductById(d.productId)
products.add(product)
return products