diff --git a/routes/api/order.js b/routes/api/order.js index f4eeed3..0cb5248 100644 --- a/routes/api/order.js +++ b/routes/api/order.js @@ -42,7 +42,7 @@ router.get('/:id', (req, res) => { }).then (() => { // For each food ordered, get the food_ordered data - db.collection(keys.FOOD_ORDERED_COLLECTION_NAME).find({ id: {$in: orderData.food_ordered} }).toArray().then(foodOrdered => { + db.collection(keys.FOOD_ORDERED_COLLECTION_NAME).find({ id: {$in: orderData.food_ordered}, part: orderData.part }).toArray().then(foodOrdered => { foodOrderedData = foodOrdered; }).then(() => { const foodIdList = []; @@ -125,7 +125,7 @@ router.post('/', (req, res) => { const db = client.db(DB_NAME); const collection = db.collection(keys.ORDER_COLLECTION_NAME); - collection.insertOne(req.body).then(result => { + collection.insertOne({...req.body, part: 1, date: new Date().toISOString()}).then(result => { res.json(result); }).catch(err => { res.status(500).send("Error inserting order into database : " + err); @@ -211,5 +211,29 @@ router.get('/status/pending', async (req, res) => { } }); +router.put('/next/:id', async (req, res) => { + try { + await client.connect(); + const db = client.db(DB_NAME); + const collection = db.collection(keys.ORDER_COLLECTION_NAME); + + collection.findOne({ id: Number(req.params.id) }) + .then ((order) => { + const newPart = order.part + 1; + collection.updateOne({ id: Number(req.params.id) }, { $set: { part: newPart, date: new Date().toISOString() } }) + .then(() => { + res.status(200).send(); + }).catch(err => { + res.status(500).send("Error updating order in database : " + err); + }); + }).catch(err => { + res.status(500).send("Error reading order from database : " + err); + }); + + } catch (err) { + res.status(500).send("Error connecting to database: " + err); + } +}); + module.exports = router; \ No newline at end of file