26th Commit

This commit is contained in:
Aritra Banik 2024-02-09 03:01:11 +05:30
parent 48a15d0d1c
commit 23f8f7b0ba
4 changed files with 14 additions and 5 deletions

View File

@ -24,6 +24,7 @@ proc drop*(db: DbConn) =
db.exec(sql"DROP TABLE IF EXISTS cart") db.exec(sql"DROP TABLE IF EXISTS cart")
proc getUserCart*(db: DbConn, userId: int): seq[Cart] = proc getUserCart*(db: DbConn, userId: int): seq[Cart] =
## get the cart details of the user
var var
row = 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]
@ -40,6 +41,7 @@ proc getUserCart*(db: DbConn, userId: int): seq[Cart] =
return cartDetails return cartDetails
proc addToCart*(db: DbConn, cart: Cart) = proc addToCart*(db: DbConn, cart: Cart) =
## add the product to the cart
var cartDetails = getUserCart(db, cart.userId) var cartDetails = getUserCart(db, cart.userId)
for d, f in cartDetails: for d, f in cartDetails:
if f.userId == cart.userId and f.productId == cart.productId: if f.userId == cart.userId and f.productId == cart.productId:
@ -49,7 +51,9 @@ proc addToCart*(db: DbConn, cart: Cart) =
db.exec(sql"INSERT INTO cart (user_id, product_id, quantity) VALUES (?, ?, ?)", cart.userId, cart.productId, cart.quantity) db.exec(sql"INSERT INTO cart (user_id, product_id, quantity) VALUES (?, ?, ?)", cart.userId, cart.productId, cart.quantity)
proc removeFromCart*(db: DbConn, cart: Cart) = proc removeFromCart*(db: DbConn, cart: Cart) =
## remove the product from the cart
db.exec(sql"DELETE FROM cart WHERE user_id=? AND product_id=?", cart.userId, cart.productId) db.exec(sql"DELETE FROM cart WHERE user_id=? AND product_id=?", cart.userId, cart.productId)
proc updateCart*(db: DbConn, quantity: string, id: int) = proc updateCart*(db: DbConn, quantity: string, id: int) =
## update the quantity of the product in the cart
db.exec(sql"UPDATE cart SET quantity=? WHERE id=?", quantity, id) db.exec(sql"UPDATE cart SET quantity=? WHERE id=?", quantity, id)

View File

@ -6,6 +6,7 @@ proc close*(db: DbConn) =
db.close() db.close()
proc setupOrders*(db: DbConn) = proc setupOrders*(db: DbConn) =
## setupOrders creates the orders table if it does not exist
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,
@ -24,12 +25,8 @@ proc setupOrders*(db: DbConn) =
); );
""") """)
# 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 createPost*(db: DbConn, order:Orders): int64 = proc createPost*(db: DbConn, order:Orders): int64 =
## createPost creates a new post and returns the id of the new post
result = db.insertID(sql"INSERT INTO orders (user_id, country, address, state, postal_code, phone_number, product_id, quantity, order_status, order_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", order.userId, order.country, order.address, order.state, order.postalCode, order.phoneNumber, order.productId, order.quantity, order.orderStatus, order.orderDate) result = db.insertID(sql"INSERT INTO orders (user_id, country, address, state, postal_code, phone_number, product_id, quantity, order_status, order_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", order.userId, order.country, order.address, order.state, order.postalCode, order.phoneNumber, order.productId, order.quantity, order.orderStatus, order.orderDate)
proc drop*(db: DbConn) = proc drop*(db: DbConn) =

View File

@ -27,11 +27,13 @@ proc setupProductsIndex*(db: DbConn) =
""") """)
proc createPost*(db: DbConn, product:Products): int64 = proc createPost*(db: DbConn, product:Products): int64 =
## createPost creates a new product and returns its id
var newID = 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*(db: DbConn): seq[Products]= proc availableProducts*(db: DbConn): seq[Products]=
## availableProducts returns all products with quantity > 0
var rows = 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:
@ -50,6 +52,7 @@ proc availableProducts*(db: DbConn): seq[Products]=
return products return products
proc getProductByName*(db: DbConn, name: string): Products = proc getProductByName*(db: DbConn, name: string): Products =
## getProductByName returns a product by its name
var row = 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])
@ -64,6 +67,7 @@ proc getProductByName*(db: DbConn, name: string): Products =
return product return product
proc getProductById*(db: DbConn, id: int): Products = proc getProductById*(db: DbConn, id: int): Products =
## getProductById returns a product by its id
var row = 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])
@ -81,5 +85,6 @@ proc drop*(db: DbConn) =
db.exec(sql"DROP TABLE IF EXISTS products") db.exec(sql"DROP TABLE IF EXISTS products")
proc getPriceByProductName*(db: DbConn, name: string): float = proc getPriceByProductName*(db: DbConn, name: string): float =
## getPriceByProductName returns the price of a product by its name
var row = db.getRow(sql"SELECT price FROM products WHERE name = ?", name) var row = db.getRow(sql"SELECT price FROM products WHERE name = ?", name)
return parseFloat(row[0]) return parseFloat(row[0])

View File

@ -28,6 +28,7 @@ proc drop*(db: DbConn) =
db.exec(sql"DROP TABLE IF EXISTS users") db.exec(sql"DROP TABLE IF EXISTS users")
proc userAvailability*(db: DbConn, user, password: string): bool = proc userAvailability*(db: DbConn, user, password: string): bool =
## Check if user exists in db
var row = 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] != "":
@ -36,11 +37,13 @@ proc userAvailability*(db: DbConn, user, password: string): bool =
return false return false
proc getUserId*(db: DbConn, user, password: string): int = proc getUserId*(db: DbConn, user, password: string): int =
## Get user id from db
var row = 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 = proc getUser*(db: DbConn, email, password: string): User =
## Get user from db
var var
row = db.getRow(sql"SELECT * FROM users WHERE email = ? and password = ?;", email, password) row = db.getRow(sql"SELECT * FROM users WHERE email = ? and password = ?;", email, password)