Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

20 routes pour commandes prtes #23

Merged
merged 5 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading