login now checks against db and attaches user data to req.user
This commit is contained in:
@@ -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
|
||||
);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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");
|
||||
})
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user