From cff08d91d6b99326dfa6ea39978577c38e48dd39 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Fri, 23 Sep 2022 17:34:59 -0500 Subject: [PATCH] in progress: connecting new service providers to express api --- .../{CartProduct.js => CartProductModel.js} | 13 +-- .../{OrderProduct.js => OrderProductModel.js} | 0 old_routes/API.js | 11 +++ old_routes/cart.js | 8 ++ {routes => old_routes}/checkout.js | 0 {routes => old_routes}/login.js | 0 old_routes/order.js | 8 ++ {routes => old_routes}/products.js | 0 {routes => old_routes}/register.js | 0 old_routes/user.js | 63 ++++++++++++++ routes/API.js | 16 ++-- routes/auth.js | 0 routes/cart.js | 32 ++++++-- routes/order.js | 8 -- routes/product.js | 27 ++++++ routes/user.js | 82 ++++++------------- services/CartService.js | 9 ++ 17 files changed, 193 insertions(+), 84 deletions(-) rename models/{CartProduct.js => CartProductModel.js} (57%) rename models/{OrderProduct.js => OrderProductModel.js} (100%) create mode 100644 old_routes/API.js create mode 100644 old_routes/cart.js rename {routes => old_routes}/checkout.js (100%) rename {routes => old_routes}/login.js (100%) create mode 100644 old_routes/order.js rename {routes => old_routes}/products.js (100%) rename {routes => old_routes}/register.js (100%) create mode 100644 old_routes/user.js create mode 100644 routes/auth.js create mode 100644 routes/product.js diff --git a/models/CartProduct.js b/models/CartProductModel.js similarity index 57% rename from models/CartProduct.js rename to models/CartProductModel.js index bd255ee..91e4012 100644 --- a/models/CartProduct.js +++ b/models/CartProductModel.js @@ -1,9 +1,8 @@ +const db = require('../db/Pool'); +const pgp = require('pg-promise')({ capSQL: true }); + module.exports = class CartProductModel { - async insert(data) { - - } - - async update(data) { + async create(data) { } @@ -11,6 +10,10 @@ module.exports = class CartProductModel { } + async update(data) { + + } + async delete(productid) { } diff --git a/models/OrderProduct.js b/models/OrderProductModel.js similarity index 100% rename from models/OrderProduct.js rename to models/OrderProductModel.js diff --git a/old_routes/API.js b/old_routes/API.js new file mode 100644 index 0000000..b8ce8a4 --- /dev/null +++ b/old_routes/API.js @@ -0,0 +1,11 @@ +const userRouter = require('./user'); +const productsRouter = require('./products'); +const registerRouter = require('./register'); +const loginRouter = require('./login'); + +module.exports = async (app, passport) => { + loginRouter(app, passport); + productsRouter(app); + registerRouter(app); + userRouter(app); +}; \ No newline at end of file diff --git a/old_routes/cart.js b/old_routes/cart.js new file mode 100644 index 0000000..0ef17a9 --- /dev/null +++ b/old_routes/cart.js @@ -0,0 +1,8 @@ +const express = require('express'); +const cartRouter = express.Router(); + +cartRouter.route('/cart').get((req, res) => { + res.send('get cart'); +}); + +module.exports = cartRouter; \ No newline at end of file diff --git a/routes/checkout.js b/old_routes/checkout.js similarity index 100% rename from routes/checkout.js rename to old_routes/checkout.js diff --git a/routes/login.js b/old_routes/login.js similarity index 100% rename from routes/login.js rename to old_routes/login.js diff --git a/old_routes/order.js b/old_routes/order.js new file mode 100644 index 0000000..b93c8ce --- /dev/null +++ b/old_routes/order.js @@ -0,0 +1,8 @@ +const express = require('express'); +const orderRouter = express.Router(); + +orderRouter.route('/orders').get((req, res) => { + res.send('get orders'); +}); + +module.exports = orderRouter; \ No newline at end of file diff --git a/routes/products.js b/old_routes/products.js similarity index 100% rename from routes/products.js rename to old_routes/products.js diff --git a/routes/register.js b/old_routes/register.js similarity index 100% rename from routes/register.js rename to old_routes/register.js diff --git a/old_routes/user.js b/old_routes/user.js new file mode 100644 index 0000000..99d5c56 --- /dev/null +++ b/old_routes/user.js @@ -0,0 +1,63 @@ +const userRouter = require('express').Router(); +const { connect } = require('../db/Pool'); + +module.exports = (app) => { + app.use('/api/user', userRouter); + + // get a list of all users, or a single user matching an email passed in as a query param + userRouter.route('/').get(async (req, res) => { + const { email } = req.query; + const client = await connect() + .then(console.log('Connection successful.')) + .catch(e => console.log(e)); + + if (!email) { + try { + await client.query("BEGIN"); + const results = await client.query("SELECT * FROM users"); + await client.query("COMMIT"); + if (results) res.send(results.rows); + } catch(e) { + await client.query('ROLLBACK'); + throw new Error(e); + } finally { + await client.release(); + console.log("Client disconnected."); + } + } else { + try { + await client.query("BEGIN"); + const result = await client.query(("SELECT * FROM users WHERE email = ($1)"), [email]) + await client.query("COMMIT"); + if (result) res.send(result.rows); + } catch(e) { + await client.query('ROLLBACK'); + throw new Error(e); + } finally { + await client.release(); + console.log("Client disconnected."); + } + } + }); + + // post a new user to the database + userRouter.route('/').post(async (req, res) => { + const { name, email } = req.body; + const client = await connect() + .then(console.log('Connection successful.')); + const input = "INSERT INTO users (name, email) VALUES ($1, $2)"; + + try { + await client.query("BEGIN"); + await client.query(input, [name, email]); + await client.query("COMMIT"); + res.sendStatus(200); + } catch(e) { + await client.query('ROLLBACK'); + throw new Error(e); + } finally { + await client.release(); + console.log("Client disconnected."); + } + }); +}; \ No newline at end of file diff --git a/routes/API.js b/routes/API.js index b8ce8a4..18ca68b 100644 --- a/routes/API.js +++ b/routes/API.js @@ -1,11 +1,13 @@ +const authRouter = require('./auth'); const userRouter = require('./user'); -const productsRouter = require('./products'); -const registerRouter = require('./register'); -const loginRouter = require('./login'); +const productRouter = require('./product'); +const orderRouter = require('./order'); +const cartRouter = require('./cart'); module.exports = async (app, passport) => { - loginRouter(app, passport); - productsRouter(app); - registerRouter(app); + authRouter(app, passport); userRouter(app); -}; \ No newline at end of file + productRouter(app); + orderRouter(app); + cartRouter(app); +} \ No newline at end of file diff --git a/routes/auth.js b/routes/auth.js new file mode 100644 index 0000000..e69de29 diff --git a/routes/cart.js b/routes/cart.js index 0ef17a9..05c84be 100644 --- a/routes/cart.js +++ b/routes/cart.js @@ -1,8 +1,28 @@ -const express = require('express'); -const cartRouter = express.Router(); +const router = require('express').Router(); +const CartService = require('../services/CartService'); +const CartServiceInstance = new CartService(); -cartRouter.route('/cart').get((req, res) => { - res.send('get cart'); -}); +module.exports = (app) => { + app.use('/api/cart', router); -module.exports = cartRouter; \ No newline at end of file + router.post('/:userId', async (req, res, next) => { + const { userId } = req.params; + + try { + const response = await CartServiceInstance.getCart(userId); + res.status(200).send(response); + } catch(e) { + next(e); + } + }) + + router.put('/:userId', async (req, res, next) => { + const { userId, data } = req.params; + + try { + + } catch(e) { + next(e); + } + }) +} \ No newline at end of file diff --git a/routes/order.js b/routes/order.js index b93c8ce..e69de29 100644 --- a/routes/order.js +++ b/routes/order.js @@ -1,8 +0,0 @@ -const express = require('express'); -const orderRouter = express.Router(); - -orderRouter.route('/orders').get((req, res) => { - res.send('get orders'); -}); - -module.exports = orderRouter; \ No newline at end of file diff --git a/routes/product.js b/routes/product.js new file mode 100644 index 0000000..ef9a656 --- /dev/null +++ b/routes/product.js @@ -0,0 +1,27 @@ +const router = require('express').Router(); +const ProductService = require('../services/ProductService'); +const ProductServiceInstance = new ProductService(); + +module.exports = (app) => { + app.use('/api/product', router); + + router.get('/', async (req, res, next) => { + try { + const response = await ProductServiceInstance.getAll(); + res.status(200).send(response); + } catch(e) { + next(e); + } + }) + + router.get('/:productId', async(req, res, next) => { + const { productId } = req.params; + + try { + const response = await ProductServiceInstance.getOne(productId); + res.status(200).send(response); + } catch(e) { + next(e); + } + }) +} \ No newline at end of file diff --git a/routes/user.js b/routes/user.js index 99d5c56..1d1d099 100644 --- a/routes/user.js +++ b/routes/user.js @@ -1,63 +1,29 @@ -const userRouter = require('express').Router(); -const { connect } = require('../db/Pool'); +const router = require('express').Router(); + +const UserService = require('../services/UserService'); +const UserServiceInstance = new UserService(); module.exports = (app) => { - app.use('/api/user', userRouter); - - // get a list of all users, or a single user matching an email passed in as a query param - userRouter.route('/').get(async (req, res) => { - const { email } = req.query; - const client = await connect() - .then(console.log('Connection successful.')) - .catch(e => console.log(e)); - - if (!email) { - try { - await client.query("BEGIN"); - const results = await client.query("SELECT * FROM users"); - await client.query("COMMIT"); - if (results) res.send(results.rows); - } catch(e) { - await client.query('ROLLBACK'); - throw new Error(e); - } finally { - await client.release(); - console.log("Client disconnected."); - } - } else { - try { - await client.query("BEGIN"); - const result = await client.query(("SELECT * FROM users WHERE email = ($1)"), [email]) - await client.query("COMMIT"); - if (result) res.send(result.rows); - } catch(e) { - await client.query('ROLLBACK'); - throw new Error(e); - } finally { - await client.release(); - console.log("Client disconnected."); - } - } - }); - - // post a new user to the database - userRouter.route('/').post(async (req, res) => { - const { name, email } = req.body; - const client = await connect() - .then(console.log('Connection successful.')); - const input = "INSERT INTO users (name, email) VALUES ($1, $2)"; - + app.use('/api/user', router); + + router.get('/:userId', async (req, res, next) => { try { - await client.query("BEGIN"); - await client.query(input, [name, email]); - await client.query("COMMIT"); - res.sendStatus(200); + const { userId } = req.params; + const response = await UserServiceInstance.get({ id: userId }); + res.status(200).send(response); } catch(e) { - await client.query('ROLLBACK'); - throw new Error(e); - } finally { - await client.release(); - console.log("Client disconnected."); + next(e); } - }); -}; \ No newline at end of file + }) + + router.put('/:userId', async (req, res, next) => { + try { + const { userId } = req.params; + const data = req.body; + const response = await UserServiceInstance.update({ id: userId, ...data }); + res.status(200).send(response); + } catch(e) { + next(e); + } + }) +} \ No newline at end of file diff --git a/services/CartService.js b/services/CartService.js index 84b33be..780c507 100644 --- a/services/CartService.js +++ b/services/CartService.js @@ -7,4 +7,13 @@ module.exports = class CartService { const result = await CartInstance.create(userid); if (!result) throw createError(); } + + async getCart(userid) { + const result = await CartInstance.findOneByUserId(userid); + } + + async addItem(userid, item) { + const cart = await CartInstance.findOneByUserId(userid); + const item = "await CartProductInstance.create(item)"; + } } \ No newline at end of file