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")
proc getUserCart*(db: DbConn, userId: int): seq[Cart] =
## get the cart details of the user
var
row = db.getAllRows(sql"SELECT * FROM cart WHERE user_id=?", userId)
cartDetails: seq[Cart]
@ -40,6 +41,7 @@ proc getUserCart*(db: DbConn, userId: int): seq[Cart] =
return cartDetails
proc addToCart*(db: DbConn, cart: Cart) =
## add the product to the cart
var cartDetails = getUserCart(db, cart.userId)
for d, f in cartDetails:
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)
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)
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)

View File

@ -6,6 +6,7 @@ proc close*(db: DbConn) =
db.close()
proc setupOrders*(db: DbConn) =
## setupOrders creates the orders table if it does not exist
db.exec(sql"""
CREATE TABLE IF NOT EXISTS orders (
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 =
## 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)
proc drop*(db: DbConn) =

View File

@ -27,11 +27,13 @@ proc setupProductsIndex*(db: DbConn) =
""")
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 (?, ?, ?, ?, ?);",
product.name, product.description, product.productType, product.pageName, product.price, product.pic_URL, product.quantity)
return newID
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 products: seq[Products]
for a, b in rows:
@ -50,6 +52,7 @@ proc availableProducts*(db: DbConn): seq[Products]=
return 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 product: Products
product.id = parseInt(row[0])
@ -64,6 +67,7 @@ proc getProductByName*(db: DbConn, name: string): Products =
return product
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 product: Products
product.id = parseInt(row[0])
@ -81,5 +85,6 @@ proc drop*(db: DbConn) =
db.exec(sql"DROP TABLE IF EXISTS products")
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)
return parseFloat(row[0])

View File

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