Skip to content

Commit

Permalink
Refactor return messages
Browse files Browse the repository at this point in the history
Add validate sale input to sales route
Let return message be an object of keys, message and success
  • Loading branch information
ebenezerdon committed Nov 9, 2018
1 parent 42f8999 commit 873315e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
4 changes: 2 additions & 2 deletions server/routes/controllers/productsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const addProduct = (req, res) => {
const text = 'SELECT * FROM products WHERE productname = $1';
pool.query(text, [body.productname], (err, data) => {
if (data.rowCount) {
return res.status(400).json({
return res.status(402).json({
message: 'There\'s already a product with that name',
success: false,
});
Expand Down Expand Up @@ -99,7 +99,7 @@ const deleteProduct = (req, res) => {
throw err;
}
return res.status(200).json({
message: 'Deleted!',
message: 'Product deleted!',
success: true,
});
});
Expand Down
33 changes: 24 additions & 9 deletions server/routes/controllers/salesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ const getAllSales = (req, res) => {

const getOneSale = (req, res) => {
if (!Number(req.params.id)) {
return res.status(404).json('Hi! The id has to be a number');
return res.status(400).json({
message: 'Hi! The id has to be a number',
success: false,
});
}
const text = 'SELECT * FROM sales WHERE id = $1';
pool.query(text, [req.params.id], (err, data) => {
if (!data.rowCount) {
return res.status(404).json('Hi! There\'s no sale record with that id');
return res.status(404).json({
message: 'Hi! There\'s no sale record with that id',
success: false,
});
}
if (err) throw err;
return res.status(200).json(data.rows[0]);
Expand All @@ -27,18 +33,24 @@ const getMySale = (req, res) => {
const text = 'SELECT * FROM sales WHERE attendant_id = $1';
pool.query(text, [req.decoded.id], (err, data) => {
if (!data.rowCount) {
return res.status(404).json('Hi! There\'s no sale record with that attendant_id');
return res.status(404).json({
message: 'Hi! There\'s no sale record with that attendant_id',
success: false,
});
}
if (err) throw err;
return res.status(200).json(data.rows[0]);
});
};

const getAttSale = (req, res) => {
const getAttendantSale = (req, res) => {
const text = 'SELECT * FROM sales WHERE attendant_id = $1';
pool.query(text, [req.params.id], (err, data) => {
if (!data.rowCount) {
return res.status(404).json('Hi! There\'s no sale record with that attendant_id');
return res.status(404).json({
message: 'Hi! There\'s no sale record with that attendant_id',
success: false,
});
}
if (err) throw err;
return res.status(200).json(data.rows[0]);
Expand All @@ -59,8 +71,11 @@ const addSale = (req, res) => {
req.decoded.id,
];
pool.query(text, values, (err, data) => {
if (!data.rowCount) {
return res.status(404).json('Hi! There\'s no sale record with that id');
if (!data.rowCount) {
return res.status(404).json({
message: 'Hi! There\'s no sale record with that id',
success: false,
});
}
if (err) throw err;
return res.status(200).json(data.rows[0]);
Expand Down Expand Up @@ -99,7 +114,7 @@ const deleteSale = (req, res) => {
throw err;
}
return res.status(200).json({
message: 'Deleted!',
message: 'Sale record deleted!',
success: true,
});
});
Expand All @@ -109,7 +124,7 @@ export {
getAllSales,
getOneSale,
getMySale,
getAttSale,
getAttendantSale,
addSale,
updateSale,
deleteSale,
Expand Down
6 changes: 3 additions & 3 deletions server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
getAllProducts, getOneProduct, addProduct, deleteProduct, updateProduct,
} from './controllers/productsController';
import {
getAllSales, getOneSale, getMySale, getAttSale, addSale, updateSale, deleteSale,
getAllSales, getOneSale, getMySale, getAttendantSale, addSale, updateSale, deleteSale,
} from './controllers/salesController';
import {
getAllUsers, getOneUser, addUser, updateUser, deleteUser, loginUser, makeAdmin,
Expand All @@ -19,8 +19,8 @@ const router = express.Router();
/* router.get('/', (req, res, next) => {
res.send('index.html');
}); */
router.get('/sales/att', authenticate, verifyAttendant, getMySale);
router.get('/sales/att/:id', authenticate, verifyAdmin, getAttSale);
router.get('/sales/user', authenticate, verifyAttendant, getMySale);
router.get('/sales/users/:id', authenticate, verifyAdmin, getAttendantSale);
/* Products Router */
router.get('/products', authenticate, getAllProducts);
router.get('/products/:id', authenticate, validateId, getOneProduct);
Expand Down
4 changes: 2 additions & 2 deletions server/tests/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('Create New Product', () => {
})
.set('accesstoken', token)
.end((error, data) => {
expect(data).to.have.status(400);
expect(data).to.have.status(402);
expect(data.body.success).to.equal(false);
done();
});
Expand Down Expand Up @@ -291,7 +291,7 @@ describe('Delete Product', () => {
.set('accesstoken', token)
.end((error, data) => {
expect(data).to.have.status(200);
expect(data.body.message).to.equal('Deleted!');
expect(data.body.message).to.equal('Product deleted!');
expect(data.body.success).to.equal(true);
done();
});
Expand Down
4 changes: 2 additions & 2 deletions server/tests/sales.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('Get sales', () => {
.end((err, res) => {
const token = res.body;
chai.request(app)
.get('/api/v1/sales/att/41')
.get('/api/v1/sales/users/41')
.set('accesstoken', token)
.end((error, data) => {
expect(data).to.have.status(200);
Expand Down Expand Up @@ -320,7 +320,7 @@ describe('Delete sale record', () => {
.set('accesstoken', token)
.end((error, data) => {
expect(data).to.have.status(200);
expect(data.body.message).to.equal('Deleted!');
expect(data.body.message).to.equal('Sale record deleted!');
expect(data.body.success).to.equal(true);
done();
});
Expand Down

0 comments on commit 873315e

Please sign in to comment.