Skip to content

Commit

Permalink
Merge pull request #30 from Hello-Kitchen/29-route-pour-demander-la-s…
Browse files Browse the repository at this point in the history
…uite

29 route pour demander la suite
  • Loading branch information
JulesGresset authored Jul 4, 2024
2 parents 0f1ad8d + 1cdc12a commit dcc5f7d
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions routes/api/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;

0 comments on commit dcc5f7d

Please sign in to comment.