Created
July 28, 2020 14:11
-
-
Save nsa-yoda/13bcbe02c975e2fefb32d1a1541dfb59 to your computer and use it in GitHub Desktop.
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
const Hapi = require('@hapi/hapi'); | |
const MongoClient = require('mongodb').MongoClient; | |
const server = Hapi.server({ | |
port: 3000, | |
debug: { | |
request: ['*'] | |
} | |
}); | |
const routes = [ | |
{ | |
method: 'GET', | |
path: '/locations', | |
handler: async (request, h) => { | |
const db = server.app.db; | |
const locationCollection = db.collection('locations'); | |
// query all locations | |
const documents = await locationCollection.find({}).toArray(); | |
return documents; | |
} | |
}, | |
{ | |
method: 'GET', | |
path: '/locations/{locationId}/items', | |
handler: async (request, h) => { | |
const db = server.app.db; | |
const locationsCollection = db.collection('locations'); | |
const itemsCollection = db.collection('items'); | |
const locationId = request.params.locationId; | |
// verify location | |
const locations = await locationsCollection.find({ 'locationId': parseInt(locationId) }).toArray(); | |
if(locations.length === 0){ | |
return h.response({}).code(404) | |
} | |
// query all items | |
const documents = await itemsCollection.find({ 'locationId': parseInt(locationId) }).toArray(); | |
return documents; | |
} | |
}, | |
{ | |
method: 'POST', | |
path: '/locations/{locationId}/order', | |
handler: async (request, h) => { | |
const db = server.app.db; | |
const orderCollection = db.collection('orders'); | |
const itemsCollection = db.collection('items'); | |
// manipulate our data | |
const payload = request.payload; | |
const locationId = request.params.locationId; | |
payload['total'] = 0; | |
const itemsArray = payload.items.map(function(a, b){ | |
return a.itemId; | |
}); | |
const itemsInOrder = await itemsCollection.find({ 'itemId': { $in: itemsArray } }).toArray(); | |
for(let j = 0; j <= itemsInOrder.length; j++){ | |
for(let i = 0; i <= payload.items.length; i++){ | |
if(typeof payload.items[i] !== 'undefined' && typeof itemsInOrder[j] !== 'undefined') { | |
if (payload.items[i].itemId === itemsInOrder[j].itemId) { | |
payload['total'] = payload['total'] + (itemsInOrder[j].price * payload.items[i].quantity); | |
} | |
} | |
} | |
}; | |
const res = await orderCollection.insertOne(payload) | |
console.log(await orderCollection.find().toArray()) | |
if (res) { | |
return h.response(payload).code(201); | |
} | |
} | |
} | |
]; | |
const init = async () => { | |
// connect to DB | |
const url = 'mongodb://localhost:27017/local-grocery-store'; | |
const client = new MongoClient(url, { useNewUrlParser: true, useUnifiedTopology: true }); | |
await client.connect(); | |
const db = client.db(); | |
server.app.db = db; | |
console.log("Connected successfully to mongo"); | |
// routes configuration | |
server.route(routes); | |
try { | |
if (!module.parent) { | |
await server.start(); | |
} | |
else { | |
await server.initialize(); | |
} | |
return server; | |
} | |
catch (error) { | |
console.error(error); | |
process.exit(1); | |
} | |
}; | |
process.on('unhandledRejection', (err) => { | |
console.log(err); | |
process.exit(1); | |
}); | |
void async function () { | |
if (!module.parent) { | |
await init(); | |
} | |
}(); | |
module.exports = { | |
init | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment