diff --git a/db/Seed.js b/db/Seed.js index ee6807d..6ddbdec 100644 --- a/db/Seed.js +++ b/db/Seed.js @@ -64,7 +64,7 @@ async function main() { description VARCHAR, categoryId INT REFERENCES category(id), regionId INT REFERENCES region(id), - price NUMERIC, + price MONEY, unit VARCHAR, inventory INT ); diff --git a/models/CartModel.js b/models/CartModel.js index e98ecfc..cbc7141 100644 --- a/models/CartModel.js +++ b/models/CartModel.js @@ -4,7 +4,7 @@ const pgp = require('pg-promise')({ capSQL: true }); module.exports = class CartModel { async create(userid) { try { - const statement = pgp.helpers.insert(userid, null, 'cart') + 'RETURNING *'; + const statement = pgp.helpers.insert({userid: userid}, null, 'cart') + 'RETURNING *'; const result = await db.query(statement); if (result.rows.length) return result.rows[0]; return null; diff --git a/models/CartProductModel.js b/models/CartProductModel.js index dec2250..d608323 100644 --- a/models/CartProductModel.js +++ b/models/CartProductModel.js @@ -1,8 +1,14 @@ const db = require('../db/Pool'); const pgp = require('pg-promise')({ capSQL: true }); +const CartModel = require('./CartModel'); +const ProductModel = require('./ProductModel'); +const CartInstance = new CartModel(); +const ProductInstance = new ProductModel(); + +// TODO: ensure all methods point to the table 'products_carts' rather than 'products_orders' module.exports = class CartProductModel { - async create(data) { + async _create(data) { try { const statement = pgp.helpers.insert(data, null, 'products_orders') + 'RETURNING *'; const result = await db.query(statement); @@ -13,6 +19,18 @@ module.exports = class CartProductModel { } } + async create(productid) { + try { + const data = ProductInstance.findOne(productid); + const statement = pgp.helpers.insert(data, null, 'products_carts') + '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"; diff --git a/routes/auth.js b/routes/auth.js index 9907ae4..1434ce3 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -18,18 +18,17 @@ module.exports = (app, passport) => { router.post('/login', passport.authenticate('local'), async (req, res, next) => { try { const data = req.body; - const response = await AuthServiceInstance.login(data); - res.status(200).send(response); + const user = await AuthServiceInstance.login(data); + if (user) req.user = user; + + res.status(200).send(user); } catch(e) { next(e); } }) + // OAuth2 yet to be implemented router.get('/google', async (req, res, next) => { - try { - - } catch(e) { - next(e); - } + res.send("google response will go here"); }) } \ No newline at end of file diff --git a/routes/cart.js b/routes/cart.js index 05c84be..e995740 100644 --- a/routes/cart.js +++ b/routes/cart.js @@ -5,22 +5,36 @@ const CartServiceInstance = new CartService(); module.exports = (app) => { app.use('/api/cart', router); - router.post('/:userId', async (req, res, next) => { - const { userId } = req.params; + // logic for global cart entries + router.get('/', async (req, res, next) => { + const { id } = req.user; try { - const response = await CartServiceInstance.getCart(userId); + const response = await CartServiceInstance.getCart(id); res.status(200).send(response); } catch(e) { next(e); } }) - router.put('/:userId', async (req, res, next) => { - const { userId, data } = req.params; + router.post('/', async (req, res, next) => { + const { id } = req.user; try { + const response = await CartServiceInstance.create(id); + res.status(201).send(response); + } catch(e) { + next(e); + } + }) + // logic for cart contents + router.post('/items/:itemid', async (req, res, next) => { + const { id } = req.user; + const { itemid } = req.params; + + try { + const response = await CartServiceInstance.addItem(id, itemid); } catch(e) { next(e); } diff --git a/services/AuthService.js b/services/AuthService.js index 3e63561..a09d69f 100644 --- a/services/AuthService.js +++ b/services/AuthService.js @@ -37,16 +37,9 @@ module.exports = class AuthService { try { const user = await UserInstance.findOneByEmail(email); if (!user) throw createError(401, 'Incorrect email or password'); - // const match = bcrypt.compare(user.password, password, (result, err) => { - // if (err) throw err; - // return result; - // }) - - // console.log(match); - // if (!match) throw createError(401, 'Incorrect email or password'); - - console.log(user.password); - + + const match = bcrypt.compare(password, user.password).then((result) => console.log(result)); + if (!match) throw createError(401, 'Incorrect email or password'); return user; } catch(e) { throw createError(500, e); diff --git a/services/CartService.js b/services/CartService.js index 4c783a5..7747a29 100644 --- a/services/CartService.js +++ b/services/CartService.js @@ -15,11 +15,15 @@ module.exports = class CartService { async getCart(userid) { const result = await CartInstance.findOneByUserId(userid); + if (!result) throw createError(404, "Cart not found"); + + console.log(result.id); + return result; } - async addItem(userid, item) { + async addItem(userid, itemid) { const cart = await CartInstance.findOneByUserId(userid); - const newItem = await CartProductInstance.create(item); + const newItem = await CartProductInstance.create(itemid); } async removeItem(userid, item) {