From f4a7ced8874ec5a496e565136e9e545423db4c44 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Mon, 26 Sep 2022 15:49:36 -0500 Subject: [PATCH] continuing to connect models and services --- db/Seed.js | 20 ++++++++++------- db/readStarterData.js | 5 ----- db/util/insert_file_contents.py | 1 - db/util/main.py | 6 +++-- models/CartProductModel.js | 39 ++++++++++++++++++++++++++++++--- models/OrderModel.js | 33 +++++++++++++++++++++++++--- package.json | 3 +-- server.js | 5 ++--- services/AuthService.js | 5 ----- services/CartService.js | 15 ++++++++++++- services/OrderService.js | 9 ++++++++ 11 files changed, 108 insertions(+), 33 deletions(-) delete mode 100644 db/readStarterData.js diff --git a/db/Seed.js b/db/Seed.js index 588da6d..2c10819 100644 --- a/db/Seed.js +++ b/db/Seed.js @@ -2,8 +2,10 @@ const { Client } = require('pg'); require('dotenv').config({ path: "../.env" }); async function main() { + console.log("Beginning database setup."); + const client = new Client({ connectionString: process.env.CONNECTION }); - await client.connect().then(console.log("Connection successful.")); + await client.connect().then(console.log("Now connected to postgres")); // user const createUserTable = ` @@ -11,8 +13,8 @@ async function main() { id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY NOT NULL, email VARCHAR NOT NULL, password VARCHAR NOT NULL, - firstname VARCHAR NOT NULL, - lastname VARCHAR NOT NULL + firstname VARCHAR, + lastname VARCHAR ); `; @@ -29,7 +31,7 @@ async function main() { CREATE TABLE IF NOT EXISTS orders ( id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY NOT NULL, userId INT REFERENCES users(id), - total NUMERIC NOT NULL, + total NUMERIC, delivered BOOLEAN, processed BOOLEAN, shipped BOOLEAN @@ -61,7 +63,8 @@ async function main() { description VARCHAR, categoryId INT REFERENCES category(id), regionId INT REFERENCES region(id), - price NUMERIC + price NUMERIC, + inventory INT ); `; @@ -69,7 +72,8 @@ async function main() { const createProductsCarts = ` CREATE TABLE IF NOT EXISTS products_carts ( productId INT REFERENCES product(id), - cartId INT REFERENCES cart(id) + cartId INT REFERENCES cart(id), + quantity INT ); `; @@ -94,11 +98,11 @@ async function main() { } await client.end(); - status = "Database setup successful!"; + status = "Database initialization successful."; } catch(e) { status = e; } finally { - if (status !== "Database setup successful!") { + if (status !== "Database initialization successful.") { throw new Error(status); } } diff --git a/db/readStarterData.js b/db/readStarterData.js deleted file mode 100644 index 81807a0..0000000 --- a/db/readStarterData.js +++ /dev/null @@ -1,5 +0,0 @@ - - -async function main() { - return; -} \ No newline at end of file diff --git a/db/util/insert_file_contents.py b/db/util/insert_file_contents.py index 6b617bd..0849c2f 100644 --- a/db/util/insert_file_contents.py +++ b/db/util/insert_file_contents.py @@ -1,5 +1,4 @@ import csv -from re import M from psycopg2 import sql # function to read from a given csv file into postgres diff --git a/db/util/main.py b/db/util/main.py index 56c0010..b3751e4 100644 --- a/db/util/main.py +++ b/db/util/main.py @@ -1,6 +1,5 @@ from insert_file_contents import insert_file_contents import psycopg2 -from psycopg2 import sql import os # read data from environment if present @@ -26,9 +25,12 @@ os.close(fd) conn = psycopg2.connect("dbname=e-commerce-092122 user=mikayladobson") cur = conn.cursor() +print("Now attempting to populate database...") + # read contents of each file into postgres insert_file_contents(conn, cur, "./data/categories.csv", 'category') insert_file_contents(conn, cur, "./data/regions.csv", 'region') insert_file_contents(conn, cur, "./data/products.csv", 'product') -print("Database insertions executed successfully.") \ No newline at end of file +print("Insertions executed successfully.") +print("Database preparations complete!") \ No newline at end of file diff --git a/models/CartProductModel.js b/models/CartProductModel.js index 91e4012..dec2250 100644 --- a/models/CartProductModel.js +++ b/models/CartProductModel.js @@ -3,18 +3,51 @@ const pgp = require('pg-promise')({ capSQL: true }); module.exports = class CartProductModel { async create(data) { - + try { + const statement = pgp.helpers.insert(data, null, 'products_orders') + 'RETURNING *'; + const result = await db.query(statement); + if (result.rows.length) return result.rows[0]; + return null; + } catch(e) { + throw new Error(e); + } } async find(cartid) { - + try { + const statement = "SELECT * FROM products_orders WHERE cartid = $1"; + const values = [cartid]; + const result = await db.query(statement, values); + if (result.rows.length) return result.rows[0]; + return []; + } catch(e) { + throw new Error(e); + } } async update(data) { + const { id } = data; + try { + const condition = pgp.as.format("WHERE id = $1", [id]); + const statement = pgp.helpers.update(data, null, 'products_orders') + condition; + const result = await db.query(statement); + if (result.rows.length) return result.rows[0]; + return null; + } catch(e) { + throw new Error(e); + } } async delete(productid) { - + try { + const statement = "DELETE FROM products_orders WHERE id = $1 RETURNING *"; + const values = [productid]; + const result = await db.query(statement, values); + if (result.rows.length) return result.rows[0]; + return null; + } catch(e) { + throw new Error(e); + } } } \ No newline at end of file diff --git a/models/OrderModel.js b/models/OrderModel.js index 4d698d2..4dc2121 100644 --- a/models/OrderModel.js +++ b/models/OrderModel.js @@ -1,17 +1,44 @@ -module.exports = class OrderModel { - async create() { +const db = require('../db/Pool'); +const pgp = require('pg-promise')({ capSQL: true }); +/** + * TODO: conceptualize and implement order lifecycle, from not submitted, + * to submitted/not shipped, to shipped and pending, to delivered? +**/ + +module.exports = class OrderModel { + async create(userid) { + try { + const statement = pgp.helpers.insert(userid, null, 'orders') + 'RETURNING *'; + const result = await db.query(statement); + if (result.rows.length) return result.rows[0]; + return null; + } catch(e) { + throw new Error(e); + } } async update(data) { + try { + } catch(e) { + throw new Error(e); + } } async findByUser(userid) { + try { + } catch(e) { + throw new Error(e); + } } async findByOrderId(orderid) { - + try { + + } catch(e) { + throw new Error(e); + } } } \ No newline at end of file diff --git a/package.json b/package.json index a1a50bd..62c150a 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,7 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "nodemon server.js", - "seed": "cd db && node seed.js", - "populate": "cd db/util && python3 main.py" + "seed": "cd db && node seed.js && cd util && python3 main.py" }, "engines": { "node": "v16.13.1" diff --git a/server.js b/server.js index 5de1917..a090e75 100644 --- a/server.js +++ b/server.js @@ -1,11 +1,10 @@ require('dotenv').config(); const express = require('express'); +const loaders = require('./loaders'); const PORT = process.env.PORT || 8088; -const app = express(); -const loaders = require('./loaders'); - async function start() { + const app = express(); loaders(app); app.listen(PORT, () => { diff --git a/services/AuthService.js b/services/AuthService.js index d090fa1..d255858 100644 --- a/services/AuthService.js +++ b/services/AuthService.js @@ -36,10 +36,5 @@ module.exports = class AuthService { } } - // yet to be implemented - async deleteOne(data) { - return data; - } - // TO IMPLEMENT: google, facebook passport strategies } \ No newline at end of file diff --git a/services/CartService.js b/services/CartService.js index 780c507..e35902a 100644 --- a/services/CartService.js +++ b/services/CartService.js @@ -1,6 +1,11 @@ const createError = require('http-errors'); const CartModel = require('../models/CartModel'); +const OrderModel = require('../models/OrderModel'); +const CartProductModel = require('../models/CartProductModel'); + const CartInstance = new CartModel(); +const OrderInstance = new OrderModel(); +const CartProductInstance = new CartProductModel(); module.exports = class CartService { async create(userid) { @@ -14,6 +19,14 @@ module.exports = class CartService { async addItem(userid, item) { const cart = await CartInstance.findOneByUserId(userid); - const item = "await CartProductInstance.create(item)"; + const item = await CartProductInstance.create(item); + } + + async removeItem(userid, item) { + + } + + async checkout() { + } } \ No newline at end of file diff --git a/services/OrderService.js b/services/OrderService.js index e69de29..f80704b 100644 --- a/services/OrderService.js +++ b/services/OrderService.js @@ -0,0 +1,9 @@ +const OrderModel = require('../models/OrderModel'); +const OrderInstance = new OrderModel(); + +module.exports = class OrderService { + async create(userid) { + const result = await OrderInstance.create(userid); + if (!result) throw new Error(); + } +} \ No newline at end of file