Created
April 8, 2020 16:42
-
-
Save fega/f4076a8668ef45f6fef0ef3c4b5d2469 to your computer and use it in GitHub Desktop.
Pager solution
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
const Hapi = require('@hapi/hapi'); | |
const MongoClient = require('mongodb').MongoClient; | |
const Boom = require('@hapi/boom'); | |
const totalAmountReducer = (userItems) => (prev, item) => { | |
const itemsPrice = item.price * userItems.find(v => v.itemId === item.itemId).quantity | |
return prev + itemsPrice; | |
}; | |
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 = parseInt(request.params.locationId); | |
const { db } = server.app; | |
const Items = db.collection('items'); | |
const Locations = db.collection('locations') | |
const location = await Locations.findOne({ locationId }); | |
if (!location) { | |
throw Boom.notFound('Location not found') | |
} | |
const documents = await Items.find({ locationId }).toArray(); | |
return documents; | |
} | |
}, | |
{ | |
method: 'POST', | |
path: '/locations/{locationId}/order', | |
handler: async (request, h) => { | |
const { customerId, items } = request.payload; | |
if (!customerId === undefined) throw Boom.badRequest('Missing customerId'); | |
if (!Array.isArray(items)) throw Boom.badRequest('Missing Item array'); | |
const db = server.app.db; | |
/** @type {import('mongodb').Collection} */ | |
const Orders = db.collection('orders'); | |
const Items = db.collection('items'); | |
const itemsIds = items.map(value => value.itemId); | |
const itemsObjects = await Items.find({ itemId: { $in: itemsIds } }).toArray(); | |
const total = itemsObjects.reduce(totalAmountReducer(items),0); | |
await Orders.insertOne({ customerId, items }); | |
return { customerId, items, total } | |
} | |
}, | |
]; | |
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