This commit is contained in:
Aritra Banik 2024-03-26 02:53:41 +05:30
parent 53dfa0f257
commit fd4d6df422
6 changed files with 40 additions and 12 deletions

Binary file not shown.

BIN
src/a3

Binary file not shown.

View File

@ -1,6 +1,9 @@
import db_connector/db_sqlite import db_connector/db_sqlite
import ../a3pkg/models import
strutils,
../a3pkg/models,
./users
proc close*(db: DbConn) = proc close*(db: DbConn) =
db.close() db.close()
@ -30,4 +33,25 @@ proc createOrder*(db: DbConn, order:Orders): int64 =
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) =
db.exec(sql"DROP TABLE IF EXISTS orders") db.exec(sql"DROP TABLE IF EXISTS orders")
proc getOrderAdmin*(db: DbConn): seq[User]=
var
row = db.getAllRows(sql"SELECT DISTINCT user_id FROM orders;")
users: seq[User]
for a, b in row:
var
u = db.getRow(sql"SELECT * FROM users WHERE id=?", b[0])
user: User
c = db.getUserOrdersAmount(parseInt(b[0]))
user.id = parseInt(u[0])
user.firstName = u[1]
user.lastName = u[2]
user.email = u[3]
user.totalQuantity = c[0]
user.totalPrice = c[1]
users.add(user)
return users

View File

@ -58,20 +58,20 @@ proc getUser*(db: DbConn, email, password: string): User =
return user return user
proc getUserOrdersAmount*(db: DbConn, userId: int): (int, int)= proc getUserOrdersAmount*(db: DbConn, userId: int): (int, float)=
echo userId echo userId
var var
row = db.getAllRows(sql"SELECT (product_id, quantity) FROM orders WHERE user_id=?", userId) row = db.getAllRows(sql"SELECT * FROM orders WHERE user_id=?", userId)
totalQuantity = 0 totalQuantity = 0
totalPrice = 0 totalPrice = 0.0
echo row echo row
for b, c in row: for b, c in row:
var var
quantity = c[1] quantity = c[8]
row1 = db.getValue(sql"SELECT price FROM products WHERE id=?", c[0]) row1 = db.getValue(sql"SELECT price FROM products WHERE id=?", c[7])
totalQuantity = totalQuantity + parseInt(quantity) totalQuantity = totalQuantity + parseInt(quantity)
totalPrice = totalPrice + parseInt(quantity) * parseInt(row1) totalPrice = totalPrice + parseFloat(quantity) * parseFloat(row1)
return (totalQuantity, totalPrice) return (totalQuantity, totalPrice)
proc getUserCartTable*(db: DbConn): seq[User]= proc getUserCartTable*(db: DbConn): seq[User]=

View File

@ -12,7 +12,7 @@ type
updatedAt*: DateTime updatedAt*: DateTime
accessLevel*: int accessLevel*: int
totalQuantity*: int totalQuantity*: int
totalPrice* :int totalPrice* : float
Products* = object Products* = object
## Product is the product model ## Product is the product model

View File

@ -2,13 +2,17 @@ import
mike, mike,
nimja, nimja,
./a3pkg/mics, ./a3pkg/mics,
./a3c/users ./a3c/[users, orders]
proc admin*(ctx: Context): string= proc admin*(ctx: Context): string=
var var
db = newDatabase() db = newDatabase()
users = db.getUserCartTable() users = db.getOrderAdmin()
# users = db.getUserCartTable()
echo users # echov v
# var users = db.getUserCartTable()
# echo users
compileTemplateFile(getScriptDir() / "a3a" / "admin" / "index.html") compileTemplateFile(getScriptDir() / "a3a" / "admin" / "index.html")