Skip to content

Commit

Permalink
Merge 1cc79e5 into c93aed3
Browse files Browse the repository at this point in the history
  • Loading branch information
akhilome committed Oct 16, 2018
2 parents c93aed3 + 1cc79e5 commit db89289
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 26 deletions.
31 changes: 15 additions & 16 deletions server/controllers/orderController.js
Expand Up @@ -4,7 +4,7 @@ import orderFormatter from '../middleware/formatter';
class OrderController {
static async getAllOrders(req, res) {
try {
const dbQuery = 'SELECT orders.id, orders.items, users.name, orders.date, orders.status FROM orders JOIN users ON orders.author = users.id';
const dbQuery = 'SELECT orders.id, orders.items, orders.price, users.name, orders.date, orders.status FROM orders JOIN users ON orders.author = users.id';
const allOrders = (await pool.query(dbQuery)).rows;
const formattedOrders = orderFormatter(allOrders);

Expand All @@ -26,7 +26,7 @@ class OrderController {
}

try {
const allOrders = (await pool.query('SELECT orders.id, orders.items, users.name, orders.date, orders.status FROM orders JOIN users ON orders.author = users.id')).rows;
const allOrders = (await pool.query('SELECT orders.id, orders.items, orders.price, users.name, orders.date, orders.status FROM orders JOIN users ON orders.author = users.id')).rows;

const formattedOrders = orderFormatter(allOrders);
const targetOrder = formattedOrders.find(order => order.id === Number(id));
Expand All @@ -44,26 +44,20 @@ class OrderController {
}

static async newOrder(req, res) {
const { foodItems } = req;
const { foodItems, foodItemsTotalPrice } = req;

const dbInsertQuery = 'INSERT INTO orders(items, author) VALUES($1, $2)';
const dbSelectQuery = 'SELECT orders.id, orders.items, users.name, orders.date, orders.status FROM orders JOIN users ON orders.author = users.id';
const dbInsertQuery = 'INSERT INTO orders(items, price, author) VALUES($1, $2, $3)';
const dbSelectQuery = 'SELECT orders.id, orders.items, orders.price, users.name, orders.date, orders.status FROM orders JOIN users ON orders.author = users.id';

try {
await pool.query(dbInsertQuery, [JSON.stringify(foodItems), req.userId]);
await pool.query(dbInsertQuery, [JSON.stringify(foodItems), foodItemsTotalPrice, req.userId]);
const allOrders = (await pool.query(dbSelectQuery));
const newOrder = allOrders.rows[allOrders.rowCount - 1];
const newOrder = (orderFormatter(allOrders.rows))[allOrders.rowCount - 1];

return res.status(201).json({
status: 'success',
message: 'new order placed successfully',
order: {
id: newOrder.id,
author: newOrder.name,
items: JSON.parse(newOrder.items),
date: newOrder.date,
status: newOrder.status,
},
order: newOrder,
});
} catch (error) {
return res.status(500).json();
Expand All @@ -79,7 +73,7 @@ class OrderController {
if (!orderExists) return res.status(404).json({ status: 'error', message: 'no order with that id exists' });

await pool.query('UPDATE orders SET status=$1 WHERE id=$2', [req.status, Number(id)]);
const updatedOrders = (await pool.query('SELECT orders.id, orders.items, users.name, orders.date, orders.status FROM orders JOIN users ON orders.author = users.id')).rows;
const updatedOrders = (await pool.query('SELECT orders.id, orders.items, orders.price, users.name, orders.date, orders.status FROM orders JOIN users ON orders.author = users.id')).rows;

const formattedOrders = orderFormatter(updatedOrders);
const targetOrder = formattedOrders.find(order => order.id === Number(id));
Expand Down Expand Up @@ -107,7 +101,12 @@ class OrderController {
try {
const userOrders = (await pool.query('SELECT * FROM orders WHERE author=$1', [id])).rows;
const formattedUserOrders = userOrders
.map(order => ({ items: JSON.parse(order.items), date: order.date, status: order.status }));
.map(order => ({
items: JSON.parse(order.items),
price: order.price,
date: order.date,
status: order.status,
}));
return res.status(200).json({
status: 'success',
message: 'orders fetched successfully',
Expand Down
1 change: 1 addition & 0 deletions server/db/schema.sql
Expand Up @@ -16,6 +16,7 @@ CREATE TABLE IF NOT EXISTS menu (
CREATE TABLE IF NOT EXISTS orders (
id serial PRIMARY KEY,
items TEXT NOT NULL,
price REAL NOT NULL,
author INTEGER REFERENCES users(id),
date DATE NOT NULL DEFAULT CURRENT_DATE,
status VARCHAR(50) NOT NULL DEFAULT 'new'
Expand Down
1 change: 1 addition & 0 deletions server/middleware/formatter.js
Expand Up @@ -4,6 +4,7 @@ const formatOrders = (orders) => {
id: foundOrder.id,
author: foundOrder.name,
items: JSON.parse(foundOrder.items),
price: foundOrder.price,
date: foundOrder.date,
status: foundOrder.status,
};
Expand Down
14 changes: 8 additions & 6 deletions server/middleware/sanitizer.js
Expand Up @@ -112,12 +112,8 @@ class Sanitize {
|| !Validator.isArray(foodIds)
|| !foodIds.length
|| !Validator.isArrayOfNumbers(foodIds)
) {
return res.status(400).json({
status: 'error',
message: 'foodIds should be an array of numbers',
});
}
) return res.status(400).json({ status: 'error', message: 'foodIds should be an array of numbers' });

const { allFoodExists, allFoodItems } = await Validator.isArrayOfValidFoodIds(foodIds);
if (!allFoodExists) {
return res.status(404).json({
Expand All @@ -129,7 +125,13 @@ class Sanitize {
const foodItems = foodIds
.map(foodId => allFoodItems.find(foodItem => foodItem.id === Number(foodId)).food_name);

// Calculate total price of all requested food items
const foodItemsTotalPrice = foodIds
.map(foodId => allFoodItems.find(foodItem => foodItem.id === Number(foodId)).price)
.reduce((accumulator, current) => accumulator + current);

req.foodItems = foodItems;
req.foodItemsTotalPrice = foodItemsTotalPrice;
return next();
}
}
Expand Down
8 changes: 4 additions & 4 deletions tests/routes/orders.spec.js
Expand Up @@ -22,7 +22,7 @@ describe('POST /orders', () => {
res.status.should.eql(201);
res.body.status.should.eql('success');
res.body.order.should.be.an('object');
res.body.order.should.have.keys(['id', 'author', 'items', 'date', 'status']);
res.body.order.should.have.keys(['id', 'author', 'items', 'price', 'date', 'status']);
res.body.order.items.should.be.an('array');
res.body.order.items.length.should.eql(2);
res.body.order.status.should.eql('new');
Expand All @@ -41,7 +41,7 @@ describe('POST /orders', () => {
res.status.should.eql(201);
res.body.status.should.eql('success');
res.body.order.should.be.an('object');
res.body.order.should.have.keys(['id', 'author', 'items', 'date', 'status']);
res.body.order.should.have.keys(['id', 'author', 'items', 'price', 'date', 'status']);
res.body.order.items.should.be.an('array');
res.body.order.items.length.should.eql(2);
res.body.order.status.should.eql('new');
Expand Down Expand Up @@ -167,7 +167,7 @@ describe('GET /orders', () => {
res.body.should.have.keys(['status', 'message', 'orders']);
res.body.orders.should.be.an('array');
res.body.orders[0].should.be.an('object');
res.body.orders[0].should.be.have.keys(['id', 'author', 'items', 'date', 'status']);
res.body.orders[0].should.be.have.keys(['id', 'author', 'items', 'price', 'date', 'status']);
res.body.orders[0].items.should.be.an('array');
done();
});
Expand Down Expand Up @@ -234,7 +234,7 @@ describe('GET /orders/:id', () => {
res.status.should.eql(200);
res.body.should.have.keys(['status', 'message', 'order']);
res.body.order.should.be.an('object');
res.body.order.should.have.all.keys(['id', 'items', 'author', 'date', 'status']);
res.body.order.should.have.all.keys(['id', 'items', 'price', 'author', 'date', 'status']);
res.body.order.items.should.be.an('array');
done();
});
Expand Down
1 change: 1 addition & 0 deletions tests/seed/seed.js
Expand Up @@ -90,6 +90,7 @@ const emptyTables = async () => {
const createOrdersTableQuery = `CREATE TABLE orders (
id serial PRIMARY KEY,
items TEXT NOT NULL,
price REAL NOT NULL,
author INTEGER REFERENCES users(id),
date DATE NOT NULL DEFAULT CURRENT_DATE,
status VARCHAR(50) NOT NULL DEFAULT 'new'
Expand Down

0 comments on commit db89289

Please sign in to comment.