From 625d0858c4cfe5fa3a51c307af390efacf526c73 Mon Sep 17 00:00:00 2001 From: Nishant Kaushal <101548649+nishant0708@users.noreply.github.com> Date: Mon, 1 Jul 2024 02:04:25 +0530 Subject: [PATCH] now user can update image and description --- server/controllers/canteenController.js | 64 +++++++- server/routes/canteen.js | 6 +- src/App.css | 3 - src/components/Modal-update.js | 26 +++- src/components/Navbar.jsx | 77 +++------- src/pages/Foodlist.jsx | 45 ++++-- src/pages/MenuPage.jsx | 190 ++++-------------------- 7 files changed, 160 insertions(+), 251 deletions(-) diff --git a/server/controllers/canteenController.js b/server/controllers/canteenController.js index 697e48e..834120c 100644 --- a/server/controllers/canteenController.js +++ b/server/controllers/canteenController.js @@ -334,12 +334,29 @@ const updateCanteen = async (req, res, next) => { // Controller function to update a breakfast dish const updateBreakfastDish = asyncHandler(async (req, res, next) => { const canteenId = req.params.id; - const { dishId, dish } = req.body; + const { dishId, dish, description, dishImage } = req.body; + + let uploadedImageUrl = null; + + if (dishImage) { + try { + const uploadResult = await uploadImage(dishImage); + uploadedImageUrl = uploadResult.secure_url; + } catch (error) { + console.error('Error uploading image:', error); + return res.status(500).json({ message: 'Failed to upload image' }); + } + } try { + const updateFields = { dish, description }; + if (uploadedImageUrl) { + updateFields.dishImage = uploadedImageUrl; + } + const updatedDish = await Breakfast.findOneAndUpdate( { _id: dishId, canteen: canteenId }, - { $set: { dish } }, + { $set: updateFields }, { new: true } ).exec(); @@ -355,12 +372,29 @@ const updateBreakfastDish = asyncHandler(async (req, res, next) => { //Controller to update Lunch const updateLunchDish = asyncHandler(async (req, res, next) => { const canteenId = req.params.id; - const { dishId, dish } = req.body; + const { dishId, dish, description, dishImage } = req.body; + + let uploadedImageUrl = null; + + if (dishImage) { + try { + const uploadResult = await uploadImage(dishImage); + uploadedImageUrl = uploadResult.secure_url; + } catch (error) { + console.error('Error uploading image:', error); + return res.status(500).json({ message: 'Failed to upload image' }); + } + } try { + const updateFields = { dish, description }; + if (uploadedImageUrl) { + updateFields.dishImage = uploadedImageUrl; + } + const updatedDish = await Lunch.findOneAndUpdate( { _id: dishId, canteen: canteenId }, - { $set: { dish } }, + { $set: updateFields }, { new: true } ).exec(); @@ -377,12 +411,29 @@ const updateLunchDish = asyncHandler(async (req, res, next) => { const updateDinnerDish = asyncHandler(async (req, res, next) => { const canteenId = req.params.id; - const { dishId, dish } = req.body; + const { dishId, dish, description, dishImage } = req.body; + + let uploadedImageUrl = null; + + if (dishImage) { + try { + const uploadResult = await uploadImage(dishImage); + uploadedImageUrl = uploadResult.secure_url; + } catch (error) { + console.error('Error uploading image:', error); + return res.status(500).json({ message: 'Failed to upload image' }); + } + } try { + const updateFields = { dish, description }; + if (uploadedImageUrl) { + updateFields.dishImage = uploadedImageUrl; + } + const updatedDish = await Dinner.findOneAndUpdate( { _id: dishId, canteen: canteenId }, - { $set: { dish } }, + { $set: updateFields }, { new: true } ).exec(); @@ -397,6 +448,7 @@ const updateDinnerDish = asyncHandler(async (req, res, next) => { }); + module.exports = { getCanteenDashboard, addBreakfastDish, diff --git a/server/routes/canteen.js b/server/routes/canteen.js index f8dd47a..7cb5b36 100644 --- a/server/routes/canteen.js +++ b/server/routes/canteen.js @@ -43,8 +43,8 @@ router.delete('/:id/dinner/remove',auth,isCanteen, canteenController.removeDinne router.put('/:id/update', auth, isCanteen, multerUploads, canteenController.updateCanteen); // New update routes -router.put('/:id/breakfast/updateitem',auth,isCanteen, canteenController.updateBreakfastDish); -router.put('/:id/lunch/updateitem',auth,isCanteen, canteenController.updateLunchDish); -router.put('/:id/dinner/updateitem',auth,isCanteen, canteenController.updateDinnerDish); +router.put('/:id/breakfast/updateitem',auth,isCanteen,multerUploads, canteenController.updateBreakfastDish); +router.put('/:id/lunch/updateitem',auth,isCanteen,multerUploads, canteenController.updateLunchDish); +router.put('/:id/dinner/updateitem',auth,isCanteen,multerUploads, canteenController.updateDinnerDish); module.exports = router; diff --git a/src/App.css b/src/App.css index 95be599..a445221 100644 --- a/src/App.css +++ b/src/App.css @@ -42,8 +42,6 @@ li:hover span { .nav-item{ font-size: 52px !important; -<<<<<<< Updated upstream -======= } .star-rating { @@ -90,5 +88,4 @@ li:hover span { .stars { color: yellow; font-size: 24px; /* Increase the font size to make the stars bigger */ ->>>>>>> Stashed changes } \ No newline at end of file diff --git a/src/components/Modal-update.js b/src/components/Modal-update.js index 37e99df..140d73d 100644 --- a/src/components/Modal-update.js +++ b/src/components/Modal-update.js @@ -1,8 +1,15 @@ - import React, { useState } from "react"; const Modalupdate = ({ dish, onUpdate, onCancel }) => { - const [updatedDish, setUpdatedDish] = useState(dish); + const [updatedDish, setUpdatedDish] = useState({ + dish: dish.dish, + description: dish.description || "", + dishImage: null, // For the new image file + }); + + const handleFileChange = (e) => { + setUpdatedDish({ ...updatedDish, dishImage: e.target.files[0] }); + }; const handleUpdate = () => { onUpdate(updatedDish); @@ -14,8 +21,19 @@ const Modalupdate = ({ dish, onUpdate, onCancel }) => {