Last active
April 28, 2020 18:46
-
-
Save nebiros/c2a493103d46dd2d593cfc9591becb4d 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 locationId = Number(request.params.locationId) | |
const db = server.app.db; | |
const locationCollection = db.collection('locations'); | |
const location = await locationCollection.findOne({ locationId: locationId }) | |
if (location === null) { | |
return h.response().code(404) | |
} | |
const itemsCollection = db.collection('items') | |
const items = await itemsCollection.find({ locationId: locationId }).toArray() | |
return items | |
}, | |
}, | |
{ | |
method: 'POST', | |
path: '/locations/{locationId}/order', | |
handler: async (request, h) => { | |
const db = server.app.db; | |
const ordersCollection = db.collection('orders'); | |
await ordersCollection.insertOne(request.payload) | |
const savedOrder = await ordersCollection.findOne({ customerId: request.payload.customerId }); | |
const itemsIds = savedOrder.items.map((item) => { | |
return item.itemId | |
}) | |
const items = await db.collection('items').find({ itemId: { $in: itemsIds } }).toArray() | |
let total = 0 | |
for (index in items) { | |
const payloadItem = savedOrder.items.find((x) => { | |
if (x.itemId === items[index].itemId) { | |
return x | |
} | |
}) | |
total = total + (payloadItem.quantity * items[index].price) | |
} | |
savedOrder.total = total | |
return h.response(savedOrder).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