diff --git a/src/a3 b/src/a3 index 6ba1551..bbc49df 100755 Binary files a/src/a3 and b/src/a3 differ diff --git a/src/a3.nim b/src/a3.nim index 2c538f6..32fbcee 100644 --- a/src/a3.nim +++ b/src/a3.nim @@ -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") diff --git a/src/a3a/shop.nimja b/src/a3a/shop.nimja index 87deb1f..4db86e0 100644 --- a/src/a3a/shop.nimja +++ b/src/a3a/shop.nimja @@ -105,7 +105,7 @@
- {% for (id, product) in db1.availableProducts().pairs() %} + {% for (id, product) in db.availableProducts().pairs() %}
diff --git a/src/a3c/cart.nim b/src/a3c/cart.nim index b09a0ee..6040461 100644 --- a/src/a3c/cart.nim +++ b/src/a3c/cart.nim @@ -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: diff --git a/src/a3c/orders.nim b/src/a3c/orders.nim index a69887c..e99714c 100644 --- a/src/a3c/orders.nim +++ b/src/a3c/orders.nim @@ -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") \ No newline at end of file +proc drop*(db: DbConn) = + db.exec(sql"DROP TABLE IF EXISTS orders") \ No newline at end of file diff --git a/src/a3c/products.nim b/src/a3c/products.nim index 315f7e6..64ab99d 100644 --- a/src/a3c/products.nim +++ b/src/a3c/products.nim @@ -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") \ No newline at end of file +proc drop*(db: DbConn) = + db.exec(sql"DROP TABLE IF EXISTS products") \ No newline at end of file diff --git a/src/a3c/users.nim b/src/a3c/users.nim index c9fcb5e..e648fca 100644 --- a/src/a3c/users.nim +++ b/src/a3c/users.nim @@ -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 \ No newline at end of file diff --git a/src/a3pkg/mics.nim b/src/a3pkg/mics.nim index 7e261f7..a296ec3 100644 --- a/src/a3pkg/mics.nim +++ b/src/a3pkg/mics.nim @@ -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