in progress: connecting new service providers to express api
This commit is contained in:
@@ -1,9 +1,8 @@
|
|||||||
|
const db = require('../db/Pool');
|
||||||
|
const pgp = require('pg-promise')({ capSQL: true });
|
||||||
|
|
||||||
module.exports = class CartProductModel {
|
module.exports = class CartProductModel {
|
||||||
async insert(data) {
|
async create(data) {
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
async update(data) {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -11,6 +10,10 @@ module.exports = class CartProductModel {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async update(data) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
async delete(productid) {
|
async delete(productid) {
|
||||||
|
|
||||||
}
|
}
|
||||||
11
old_routes/API.js
Normal file
11
old_routes/API.js
Normal file
@@ -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);
|
||||||
|
};
|
||||||
8
old_routes/cart.js
Normal file
8
old_routes/cart.js
Normal file
@@ -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;
|
||||||
8
old_routes/order.js
Normal file
8
old_routes/order.js
Normal file
@@ -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;
|
||||||
63
old_routes/user.js
Normal file
63
old_routes/user.js
Normal file
@@ -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.");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
@@ -1,11 +1,13 @@
|
|||||||
|
const authRouter = require('./auth');
|
||||||
const userRouter = require('./user');
|
const userRouter = require('./user');
|
||||||
const productsRouter = require('./products');
|
const productRouter = require('./product');
|
||||||
const registerRouter = require('./register');
|
const orderRouter = require('./order');
|
||||||
const loginRouter = require('./login');
|
const cartRouter = require('./cart');
|
||||||
|
|
||||||
module.exports = async (app, passport) => {
|
module.exports = async (app, passport) => {
|
||||||
loginRouter(app, passport);
|
authRouter(app, passport);
|
||||||
productsRouter(app);
|
|
||||||
registerRouter(app);
|
|
||||||
userRouter(app);
|
userRouter(app);
|
||||||
};
|
productRouter(app);
|
||||||
|
orderRouter(app);
|
||||||
|
cartRouter(app);
|
||||||
|
}
|
||||||
0
routes/auth.js
Normal file
0
routes/auth.js
Normal file
@@ -1,8 +1,28 @@
|
|||||||
const express = require('express');
|
const router = require('express').Router();
|
||||||
const cartRouter = express.Router();
|
const CartService = require('../services/CartService');
|
||||||
|
const CartServiceInstance = new CartService();
|
||||||
|
|
||||||
cartRouter.route('/cart').get((req, res) => {
|
module.exports = (app) => {
|
||||||
res.send('get cart');
|
app.use('/api/cart', router);
|
||||||
});
|
|
||||||
|
|
||||||
module.exports = cartRouter;
|
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);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -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;
|
|
||||||
27
routes/product.js
Normal file
27
routes/product.js
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -1,63 +1,29 @@
|
|||||||
const userRouter = require('express').Router();
|
const router = require('express').Router();
|
||||||
const { connect } = require('../db/Pool');
|
|
||||||
|
const UserService = require('../services/UserService');
|
||||||
|
const UserServiceInstance = new UserService();
|
||||||
|
|
||||||
module.exports = (app) => {
|
module.exports = (app) => {
|
||||||
app.use('/api/user', userRouter);
|
app.use('/api/user', router);
|
||||||
|
|
||||||
// get a list of all users, or a single user matching an email passed in as a query param
|
router.get('/:userId', async (req, res, next) => {
|
||||||
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 {
|
try {
|
||||||
await client.query("BEGIN");
|
const { userId } = req.params;
|
||||||
const results = await client.query("SELECT * FROM users");
|
const response = await UserServiceInstance.get({ id: userId });
|
||||||
await client.query("COMMIT");
|
res.status(200).send(response);
|
||||||
if (results) res.send(results.rows);
|
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
await client.query('ROLLBACK');
|
next(e);
|
||||||
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)";
|
|
||||||
|
|
||||||
|
router.put('/:userId', async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
await client.query("BEGIN");
|
const { userId } = req.params;
|
||||||
await client.query(input, [name, email]);
|
const data = req.body;
|
||||||
await client.query("COMMIT");
|
const response = await UserServiceInstance.update({ id: userId, ...data });
|
||||||
res.sendStatus(200);
|
res.status(200).send(response);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
await client.query('ROLLBACK');
|
next(e);
|
||||||
throw new Error(e);
|
|
||||||
} finally {
|
|
||||||
await client.release();
|
|
||||||
console.log("Client disconnected.");
|
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
};
|
}
|
||||||
@@ -7,4 +7,13 @@ module.exports = class CartService {
|
|||||||
const result = await CartInstance.create(userid);
|
const result = await CartInstance.create(userid);
|
||||||
if (!result) throw createError();
|
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)";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user