-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from sudhons/feature-api-endpoints-160445750
- Loading branch information
Showing
13 changed files
with
7,006 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"root": true, | ||
"extends": "airbnb", | ||
"env": { | ||
"node": true, | ||
"browser": true, | ||
"es6": true, | ||
"mocha": true, | ||
"commonjs": true, | ||
"jquery": true | ||
}, | ||
"rules": { | ||
"one-var": 0, | ||
"one-var-declaration-per-line": 0, | ||
"new-cap": 0, | ||
"max-len": [1, 80, 2], | ||
"consistent-return": 0, | ||
"no-param-reassign": 0, | ||
"no-useless-escape": 0, | ||
"no-case-declarations": 0, | ||
"linebreak-style": 0, | ||
"comma-dangle": 0, | ||
"curly": ["error", "multi-line"], | ||
"import/no-unresolved": [2, { "commonjs": true }], | ||
"no-shadow": ["error", { "allow": ["req", "res", "err"] }], | ||
"valid-jsdoc": ["error", { | ||
"requireReturn": true, | ||
"requireReturnType": true, | ||
"requireParamDescription": false, | ||
"requireReturnDescription": true | ||
}], | ||
"require-jsdoc": ["error", { | ||
"require": { | ||
"FunctionDeclaration": true, | ||
"MethodDefinition": true, | ||
"ClassDeclaration": true | ||
} | ||
}] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
.nyc_output | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
language: node_js | ||
|
||
node_js: | ||
- "stable" | ||
|
||
before_script: | ||
- npm install | ||
|
||
script: | ||
- npm run test | ||
|
||
after_script: | ||
- npm run coveralls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import express from 'express'; | ||
import bodyParser from 'body-parser'; | ||
|
||
import orders from './routes/ordersRoute'; | ||
|
||
const app = express(); | ||
|
||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: true })); | ||
|
||
app.use('/api/v1', orders); | ||
|
||
app.use((request, response) => { | ||
response.status(404); | ||
return response.json({ status: 404, message: 'unknown url path' }); | ||
}); | ||
|
||
const PORT = parseInt(process.env.PORT, 10) || 5000; | ||
|
||
app.listen(PORT, () => console.log(`Listening on port ${PORT}`)); | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import Data from '../queries/orderQueries'; | ||
|
||
/** | ||
* Performs GET, POST and UPDATE operations on orders | ||
*/ | ||
class Order { | ||
/** | ||
* @static | ||
* @method getAllOrders | ||
* @description Fetches all the orders | ||
* @param {object} request - HTTP request object | ||
* @param {object} response - HTTP response object | ||
* @returns {object} status, message and order data | ||
*/ | ||
static getAllOrders(request, response) { | ||
const data = Data.getAllOrders(); | ||
|
||
response.status(200); | ||
return response.json({ status: 200, message: 'Successful', data }); | ||
} | ||
|
||
/** | ||
* @static | ||
* @method postOrder | ||
* @description Posts a new order | ||
* @param {object} request - HTTP Request object | ||
* @param {object} response - HTTP Response object | ||
* @returns {object} status, message and order data | ||
*/ | ||
static postOrder(request, response) { | ||
const { | ||
recipientName, | ||
recipientAddress, | ||
recipientPhone, | ||
order, | ||
} = request.body; | ||
|
||
const data = Data | ||
.createNewOrder(recipientName, recipientAddress, recipientPhone, order); | ||
|
||
response.status(201); | ||
return response.json({ status: 201, message: 'Successful', data }); | ||
} | ||
|
||
/** | ||
* @static | ||
* @method getOrder | ||
* @description Fetches an order by its id | ||
* @param {object} request - HTTP Request Object | ||
* @param {object} response - HTTP Response object | ||
* @returns {object} status, message and order data | ||
*/ | ||
static getOrder(request, response) { | ||
const data = Data.getAnOrder(Number(request.params.orderId)); | ||
|
||
response.status(200); | ||
return response.json({ status: 200, message: 'Sucessful', data }); | ||
} | ||
|
||
/** | ||
* @static | ||
* @description Updates an order's status | ||
* @param {object} request - HTTP Request Object | ||
* @param {object} response - HTTP Response object | ||
* @returns {object} status, message and order data | ||
*/ | ||
static updateOrder(request, response) { | ||
const data = Data | ||
.updateOrderStatus( | ||
Number(request.params.orderId), | ||
request.body.status.toLowerCase() | ||
); | ||
response.status(200); | ||
return response | ||
.json({ status: 200, message: 'Status successfully updated', data }); | ||
} | ||
} | ||
|
||
export default Order; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const orderData = [ | ||
{ | ||
orderId: 1, | ||
customer: 'James Tunde', | ||
recipientName: 'Wade', | ||
recipientAddress: '43 Araromi', | ||
recipientPhone: 545878843, | ||
order: [ | ||
{ | ||
mealId: 3, | ||
quantity: 7, | ||
unitPrice: 200, | ||
total: 1400 | ||
} | ||
], | ||
orderedTime: 1537448443629, | ||
status: 'completed', | ||
completedTime: 1537448404629, | ||
acceptedTime: 1537448403629 | ||
}, | ||
{ | ||
orderId: 2, | ||
customer: 'John Doe', | ||
recipientName: 'James Bond', | ||
recipientAddress: '43 Yaba', | ||
recipientPhone: 9434545, | ||
order: [ | ||
{ | ||
mealId: 5, | ||
quantity: 2, | ||
unitPrice: 200, | ||
total: 400 | ||
} | ||
], | ||
orderedTime: Date.now(), | ||
status: 'waiting', | ||
} | ||
]; | ||
|
||
export default orderData; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import orderData from '../db/dbData'; | ||
|
||
/** | ||
* queries data | ||
*/ | ||
class Data { | ||
/** | ||
* @constructs | ||
* @description constructs a new Data | ||
* @param {string} customer - customer name | ||
* @param {string} recipientName - recipient name | ||
* @param {string} recipientAddr - recipient address | ||
* @param {number} recipientPhone - recipient phone number | ||
* @param {array} order - an array of meal | ||
*/ | ||
constructor(customer, recipientName, recipientAddr, recipientPhone, order) { | ||
Data.count = Data.count ? Data.count + 1 : 3; | ||
|
||
this.orderId = Data.count; | ||
this.customer = customer; | ||
this.recipientName = recipientName; | ||
this.recipientAddress = recipientAddr; | ||
this.recipientPhone = recipientPhone; | ||
this.order = order; | ||
this.orderedTime = Date.now(); | ||
this.status = 'waiting'; | ||
} | ||
|
||
/** | ||
* @static | ||
* @method createNewOrder | ||
* @description creates a new order | ||
* @param {string} recipientName - the recipient name | ||
* @param {string} recipientAddr - the recipient address | ||
* @param {number} recipientPhone - the recipient phone number | ||
* @param {array} order - an array of meal | ||
* @returns {object} the new order | ||
*/ | ||
static createNewOrder(recipientName, recipientAddr, recipientPhone, order) { | ||
const customer = 'Adeolu James'; | ||
const unitPrice = 200; | ||
order.forEach((value) => { | ||
const mealOrder = value; | ||
mealOrder.unitPrice = unitPrice; | ||
mealOrder.total = value.quantity * unitPrice; | ||
}); | ||
const newOrder = new Data( | ||
customer, | ||
recipientName, | ||
recipientAddr, | ||
recipientPhone, | ||
order | ||
); | ||
orderData.push(newOrder); | ||
|
||
return newOrder; | ||
} | ||
|
||
/** | ||
* @static | ||
* @method getAllOrders | ||
* @description Fetches all the orders | ||
* @returns {Array} an array of all orders | ||
*/ | ||
static getAllOrders() { | ||
return orderData; | ||
} | ||
|
||
/** | ||
* @static | ||
* @method getAnOrder | ||
* @description Fetches an order by its id | ||
* @param {integer} orderId - the order's ID | ||
* @returns {object} an order or null if order id does not exist | ||
*/ | ||
static getAnOrder(orderId) { | ||
const requiredOrder = orderData.find(order => order.orderId === orderId); | ||
return requiredOrder || null; | ||
} | ||
|
||
/** | ||
* @static | ||
* @method getAnOrder | ||
* @description updates the status of an order | ||
* @param {integer} orderId - the order's ID | ||
* @param {string} status - the new status | ||
* @returns {object} the updated order | ||
*/ | ||
static updateOrderStatus(orderId, status) { | ||
const requiredOrder = Data.getAnOrder(orderId); | ||
|
||
requiredOrder.status = status; | ||
|
||
if (status !== 'waiting') { | ||
requiredOrder[`${status}Time`] = Date.now(); | ||
} | ||
|
||
switch (status) { | ||
case 'accepted': | ||
requiredOrder.declinedTime = null; | ||
requiredOrder.completedTime = null; | ||
break; | ||
case 'declined': | ||
requiredOrder.acceptedTime = null; | ||
requiredOrder.completedTime = null; | ||
break; | ||
case 'completed': | ||
break; | ||
default: | ||
requiredOrder.acceptedTime = null; | ||
requiredOrder.declinedTime = null; | ||
requiredOrder.completedTime = null; | ||
} | ||
|
||
|
||
if (status !== 'waiting') { | ||
requiredOrder[`${status}Time`] = Date.now(); | ||
} else { | ||
requiredOrder.acceptedTime = null; | ||
requiredOrder.declinedTime = null; | ||
requiredOrder.completedTime = null; | ||
} | ||
|
||
return requiredOrder; | ||
} | ||
|
||
/** | ||
* @static | ||
* @method deleteAllOrders | ||
* @description delete all orders | ||
* @returns {undefined} undefined | ||
*/ | ||
static deleteAllOrders() { | ||
orderData.length = 0; | ||
} | ||
} | ||
|
||
export default Data; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import express from 'express'; | ||
|
||
import Order from '../controllers/ordersController'; | ||
import Validator from '../validators/validateOrders'; | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/', (request, response) => { | ||
response.status(200); | ||
return response.json({ status: 200, message: 'Welcome to fast food fast' }); | ||
}); | ||
|
||
router.get('/orders', Order.getAllOrders); | ||
|
||
router.post('/orders', Validator.validatePost, Order.postOrder); | ||
|
||
router.get('/orders/:orderId', Validator.validateOrderId, Order.getOrder); | ||
|
||
router.put( | ||
'/orders/:orderId', | ||
Validator.validateOrderId, | ||
Validator.validateStatus, | ||
Order.updateOrder | ||
); | ||
|
||
export default router; |
Oops, something went wrong.