Created
July 16, 2020 18:58
-
-
Save tioback/fa90b543a3d39b707568700f9cef7b24 to your computer and use it in GitHub Desktop.
server.js for Pager interview
This file contains hidden or 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
// author Renato Back ([email protected]) | |
const Hapi = require('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 itemCollection = db.collection('items'); | |
const locationCollection = db.collection('locations'); | |
const { locationId: locationIdAsString } = request.params; | |
if (!locationIdAsString) { | |
// error bad request | |
} | |
const locationId = Number(locationIdAsString); | |
const locations = await locationCollection.find({ locationId }).toArray(); | |
if (!locations.length) { | |
return h.response({}).code(404); | |
} | |
// query all items from a specific location | |
const documents = await itemCollection.find({ locationId }).toArray(); | |
// const documents = await itemCollection.find({ 'locationId': 1 }).toArray(); | |
return documents; | |
} | |
}, | |
{ | |
method: 'POST', | |
path: '/locations/{locationId}/order', | |
handler: async (request, h) => { | |
const db = server.app.db; | |
const ordersCollection = db.collection('orders'); | |
const itemsCollection = db.collection("items"); | |
const { payload } = request; | |
// query all locations | |
payload.total = 0; | |
for (let i = 0; i < payload.items.length; i++) { | |
let orderItem = payload.items[i]; | |
const item = await itemsCollection.findOne({ itemId: orderItem.itemId }); | |
const totalForItem = (orderItem.quantity * item.price); | |
payload.total += totalForItem; | |
} | |
payload.total = Math.round(payload.total * 100) / 100; | |
console.log("stop here"); | |
// try { | |
// // const document = await ordersCollection.create(payload); | |
// } catch (e) { | |
// | |
// } | |
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 }); | |
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