Skip to content

Commit

Permalink
Merge pull request #23 from Hello-Kitchen/20-routes-pour-commandes-prtes
Browse files Browse the repository at this point in the history
20 routes pour commandes prtes
  • Loading branch information
Xantass authored Jul 2, 2024
2 parents 79a2698 + a042630 commit 7e60c84
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
20 changes: 20 additions & 0 deletions routes/api/foodOrdered.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,24 @@ router.delete('/:id', (req, res) => {
});
});

router.post('/status/:id', (req, res) => {
client.connect().then(client => {
const db = client.db(DB_NAME);
const collection = db.collection(keys.FOOD_ORDERED_COLLECTION_NAME);

collection.findOne({ id: Number(req.params.id) }).then(food => {
food.is_ready = true;
collection.updateMany({ id: Number(req.params.id) }, { $set: food}).then(result => {
res.json(result);
}).catch(err => {
res.status(500).send("Error updating foodOrdered into database : " + err);
});
}).catch((err) => {
res.status(500).send("Error readinf foodOrdered from database " + err);
});
}).catch(err => {
res.status(500).send("Error connecting to database : " + err);
});
});

module.exports = router;
55 changes: 51 additions & 4 deletions routes/api/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ router.get('/:id', (req, res) => {
foodOrderedData.map((foodOrdered) => {
foodIdList.push(foodOrdered.food);
});

// For each food_ordered, get the food data
db.collection(keys.FOOD_COLLECTION_NAME).find({ id: {$in: foodIdList} }).toArray().then(food => {
foodData = food;
}).then(() => {
const foodDetailsList = [];

// For every food ordered, wrap the necessary food data in an object
foodOrderedData.forEach(foodOrdered => {
const food = foodData.find(food => food.id === foodOrdered.food);
Expand All @@ -59,14 +58,15 @@ router.get('/:id', (req, res) => {
is_ready: foodOrdered.is_ready
};

const existingFoodDetails = foodDetailsList.find(item =>
const existingFoodDetails = foodDetailsList.find(item =>
JSON.stringify(item) === JSON.stringify({ ...foodDetails, quantity: item.quantity })
);

if (existingFoodDetails) {
existingFoodDetails.quantity += 1;
} else {
foodDetailsList.push({
"id": foodOrdered.id,
...foodDetails,
quantity: 1
});
Expand All @@ -80,7 +80,7 @@ router.get('/:id', (req, res) => {
date: orderData.date,
food: foodDetailsList
});

})
.catch(err => {
res.status(500).send("Error reading foodOrdered from database : " + err);
Expand Down Expand Up @@ -149,4 +149,51 @@ router.delete('/:id', (req, res) => {
});
});

router.get('/status/ready', async (req, res) => {
try {
await client.connect();
const db = client.db(DB_NAME);
const collection = db.collection(keys.ORDER_COLLECTION_NAME);

const orders = await collection.find({}).toArray();
const readyOrders = [];

for (let order of orders) {
const foodOrdered = await db.collection(keys.FOOD_ORDERED_COLLECTION_NAME).find({ id: { $in: order.food_ordered } }).toArray();
const allReady = foodOrdered.every(food => food.is_ready);

if (allReady) {
readyOrders.push(order);
}
}
res.status(200).json(readyOrders);
} catch (err) {
res.status(500).send("Error connecting to database: " + err);
}
});

router.get('/status/pending', async (req, res) => {
try {
await client.connect();
const db = client.db(DB_NAME);
const collection = db.collection(keys.ORDER_COLLECTION_NAME);

const orders = await collection.find({}).toArray();
const readyOrders = [];

for (let order of orders) {
const foodOrdered = await db.collection(keys.FOOD_ORDERED_COLLECTION_NAME).find({ id: { $in: order.food_ordered } }).toArray();
const allReady = foodOrdered.every(food => food.is_ready);

if (!allReady) {
readyOrders.push(order);
}
}
res.status(200).json(readyOrders);
} catch (err) {
res.status(500).send("Error connecting to database: " + err);
}
});


module.exports = router;

0 comments on commit 7e60c84

Please sign in to comment.