diff --git a/db5.sqlite3 b/db5.sqlite3 index 82d33c2..9725381 100644 Binary files a/db5.sqlite3 and b/db5.sqlite3 differ diff --git a/src/a3 b/src/a3 index a7d49a4..b8a30db 100755 Binary files a/src/a3 and b/src/a3 differ diff --git a/src/a3c/orders.nim b/src/a3c/orders.nim index bd788d7..d3c7810 100644 --- a/src/a3c/orders.nim +++ b/src/a3c/orders.nim @@ -1,6 +1,9 @@ import db_connector/db_sqlite -import ../a3pkg/models +import + strutils, + ../a3pkg/models, + ./users proc close*(db: DbConn) = 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) proc drop*(db: DbConn) = - db.exec(sql"DROP TABLE IF EXISTS orders") \ No newline at end of file + 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 diff --git a/src/a3c/users.nim b/src/a3c/users.nim index eb577a7..a7e8c20 100644 --- a/src/a3c/users.nim +++ b/src/a3c/users.nim @@ -58,20 +58,20 @@ proc getUser*(db: DbConn, email, password: string): User = return user -proc getUserOrdersAmount*(db: DbConn, userId: int): (int, int)= +proc getUserOrdersAmount*(db: DbConn, userId: int): (int, float)= echo userId 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 - totalPrice = 0 + totalPrice = 0.0 echo row for b, c in row: var - quantity = c[1] - row1 = db.getValue(sql"SELECT price FROM products WHERE id=?", c[0]) + quantity = c[8] + row1 = db.getValue(sql"SELECT price FROM products WHERE id=?", c[7]) totalQuantity = totalQuantity + parseInt(quantity) - totalPrice = totalPrice + parseInt(quantity) * parseInt(row1) + totalPrice = totalPrice + parseFloat(quantity) * parseFloat(row1) return (totalQuantity, totalPrice) proc getUserCartTable*(db: DbConn): seq[User]= diff --git a/src/a3pkg/models.nim b/src/a3pkg/models.nim index 6b3c1a4..efb08ea 100644 --- a/src/a3pkg/models.nim +++ b/src/a3pkg/models.nim @@ -12,7 +12,7 @@ type updatedAt*: DateTime accessLevel*: int totalQuantity*: int - totalPrice* :int + totalPrice* : float Products* = object ## Product is the product model diff --git a/src/admin.nim b/src/admin.nim index 2641d6a..b641811 100644 --- a/src/admin.nim +++ b/src/admin.nim @@ -2,13 +2,17 @@ import mike, nimja, ./a3pkg/mics, - ./a3c/users + ./a3c/[users, orders] proc admin*(ctx: Context): string= var 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")