Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
nishant0708 committed Jun 24, 2024
1 parent d96dcec commit 31f6081
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 89 deletions.
2 changes: 1 addition & 1 deletion envexample
Original file line number Diff line number Diff line change
@@ -1 +1 @@
REACT_APP_BASE_URL=localhost:8000/api/v1 8000 can be replaced by the server you are running backend on
REACT_APP_BASE_URL=localhost:4000/api/v1 8000 can be replaced by the server you are running backend on
161 changes: 96 additions & 65 deletions server/controllers/canteenController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const Breakfast = require('../models/breakfast');
const Lunch = require('../models/lunch');
const Dinner = require('../models/dinner');
const Canteen = require("../models/canteenLoginInfo");
const { uploader } = require('../config/cloudinaryConfig');
const { uploader, uploadImage } = require('../config/cloudinaryConfig');



Expand All @@ -24,64 +24,58 @@ const getAllCanteen = async (req ,res , next) =>{
}
};

const getBreakfast = async(req , res , next) =>{

try{
const id = req.params.id;

const breakfastData = await Breakfast.find({ canteen: id }).select("dish").select("dishId").exec();
const getBreakfast = async (req, res, next) => {
try {
const id = req.params.id;


res.status(200)
.json({
success : true,
data : breakfastData,
message : "Entire breakfast was fetched"
const breakfastData = await Breakfast.find({ canteen: id })
.select("dish dishId dishImage description") // Select all fields you need
.exec();

res.status(200).json({
success: true,
data: breakfastData,
message: "Entire breakfast was fetched",
});
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
catch(error){
res.status(500).json({success : false , error : error});
}
}

const getLunch = async(req , res , next) =>{

try{
const id = req.params.id;

const lunchData = await Lunch.find({ canteen: id }).select("dish").select("dishId").exec();
};

const getLunch = async (req, res, next) => {
try {
const id = req.params.id;

res.status(200)
.json({
success : true,
data : lunchData,
message : "Entire lunch was fetched"
const lunchData = await Lunch.find({ canteen: id })
.select("dish dishId dishImage description")
.exec();

res.status(200).json({
success: true,
data: lunchData,
message: "Entire lunch was fetched",
});
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
catch(error){
res.status(500).json({success : false , error : error});
}
}

const getDinner = async(req , res , next) =>{

try{
const id = req.params.id;
const dinnerData = await Dinner.find({ canteen: id }).select("dish").select("dishId").exec();

};
const getDinner = async (req, res, next) => {
try {
const id = req.params.id;

res.status(200)
.json({
success : true,
data : dinnerData,
message : "Entire dinner was fetched"
const dinnerData = await Dinner.find({ canteen: id })
.select("dish dishId dishImage description")
.exec();

res.status(200).json({
success: true,
data: dinnerData,
message: "Entire dinner was fetched",
});
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
catch(error){
res.status(500).json({success : false , error : error});
}
}
};

// Controller function to get the canteen's dashboard
const getCanteenDashboard = asyncHandler(async (req, res, next) => {
Expand All @@ -101,20 +95,34 @@ const getCanteenDashboard = asyncHandler(async (req, res, next) => {
// Controller function to add a breakfast dish
const addBreakfastDish = asyncHandler(async (req, res, next) => {
const canteenId = req.params.id;
const { dish , dishId } = req.body;
const { dish, dishId, description, dishImage } = req.body;

let uploadedImageUrl = null;

if (dishImage) {
try {
const uploadResult = await uploadImage(dishImage); // Assuming dishImage is already a base64 string
uploadedImageUrl = uploadResult.secure_url;
} catch (error) {
console.error('Error uploading image:', error);
return res.status(500).json({ message: 'Failed to upload image' });
}
}

const existingDish = await Breakfast.findOne({ canteen: canteenId, dish }).exec();

if (existingDish) {
return res.json({ message: 'Dish already added' });
return res.status(400).json({ message: 'Dish already added' });
}

const newDish = new Breakfast({ canteen: canteenId, dish , dishId });
const newDish = new Breakfast({ canteen: canteenId, dish, dishId, dishImage: uploadedImageUrl, description });
await newDish.save();

res.json({ message: 'Dish added successfully' });
res.status(201).json({ message: 'Dish added successfully', dish: newDish });
});



// Controller function to remove a breakfast dish
const removeBreakfastDish = asyncHandler(async (req, res, next) => {
const canteenId = req.params.id;
Expand All @@ -124,24 +132,35 @@ const removeBreakfastDish = asyncHandler(async (req, res, next) => {
res.json({ message: 'Dish removed successfully' });
});

// Implement similar functions for lunch and dinner dishes
// addLunchDish, removeLunchDish, addDinnerDish, and removeDinnerDish


// Controller function to add a lunch dish
const addLunchDish = asyncHandler(async (req, res, next) => {
const canteenId = req.params.id;
const { dish, dishId } = req.body;
const { dish, dishId, description, dishImage } = req.body;

const existingDish = await Lunch.findOne({ canteen: canteenId, dish }).exec();
let uploadedImageUrl = null;

if (dishImage) {
try {
const uploadResult = await uploadImage(dishImage); // Assuming dishImage is already a base64 string
uploadedImageUrl = uploadResult.secure_url;
} catch (error) {
console.error('Error uploading image:', error);
return res.status(500).json({ message: 'Failed to upload image' });
}
}

const existingDish = await Breakfast.findOne({ canteen: canteenId, dish }).exec();

if (existingDish) {
return res.json({ message: 'Dish already added' });
return res.status(400).json({ message: 'Dish already added' });
}

const newDish = new Lunch({ canteen: canteenId, dish , dishId });
const newDish = new Lunch({ canteen: canteenId, dish, dishId, dishImage: uploadedImageUrl, description });
await newDish.save();

res.json({ message: 'Dish added successfully' });
res.status(201).json({ message: 'Dish added successfully', dish: newDish });
});

// Controller function to remove a lunch dish
Expand All @@ -157,18 +176,30 @@ const removeLunchDish = asyncHandler(async (req, res, next) => {
// Controller function to add a dinner dish
const addDinnerDish = asyncHandler(async (req, res, next) => {
const canteenId = req.params.id;
const { dish , dishId} = req.body;
const { dish, dishId, description, dishImage } = req.body;

const existingDish = await Dinner.findOne({ canteen: canteenId, dish }).exec();
let uploadedImageUrl = null;

if (dishImage) {
try {
const uploadResult = await uploadImage(dishImage); // Assuming dishImage is already a base64 string
uploadedImageUrl = uploadResult.secure_url;
} catch (error) {
console.error('Error uploading image:', error);
return res.status(500).json({ message: 'Failed to upload image' });
}
}

const existingDish = await Breakfast.findOne({ canteen: canteenId, dish }).exec();

if (existingDish) {
return res.json({ message: 'Dish already added' });
return res.status(400).json({ message: 'Dish already added' });
}

const newDish = new Dinner({ canteen: canteenId, dish ,dishId});
const newDish = new Dinner({ canteen: canteenId, dish, dishId, dishImage: uploadedImageUrl, description });
await newDish.save();

res.json({ message: 'Dish added successfully' });
res.status(201).json({ message: 'Dish added successfully', dish: newDish });

});

Expand Down
16 changes: 12 additions & 4 deletions server/models/breakfast.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ const breakfastSchema = new Schema({
type: String,
required: true,
},
dishId : {
type : String,
required : true,
}
dishId: {
type: String,
required: true,
},
dishImage: {
type: String, // Store URL or base64 representation of image
default: '', // Default value if not provided
},
description: {
type: String,
default: '', // Default value if not provided
},
});

const Breakfast = mongoose.model('Breakfast', breakfastSchema);
Expand Down
16 changes: 12 additions & 4 deletions server/models/dinner.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ const dinnerSchema = new Schema({
type: String,
required: true,
},
dishId : {
type : String,
required : true,
}
dishId: {
type: String,
required: true,
},
dishImage: {
type: String, // Store URL or base64 representation of image
default: '', // Default value if not provided
},
description: {
type: String,
default: '', // Default value if not provided
},
});

const Dinner = mongoose.model('Dinner', dinnerSchema);
Expand Down
16 changes: 12 additions & 4 deletions server/models/lunch.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ const lunchSchema = new Schema({
type: String,
required: true,
},
dishId : {
type : String,
required : true
}
dishId: {
type: String,
required: true,
},
dishImage: {
type: String, // Store URL or base64 representation of image
default: '', // Default value if not provided
},
description: {
type: String,
default: '', // Default value if not provided
},
});

const Lunch = mongoose.model('Lunch', lunchSchema);
Expand Down
2 changes: 1 addition & 1 deletion server/routes/canteen.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ router.get('/:id/dinner' , canteenController.getDinner);
// router.get('/:id', canteenController.getCanteenDashboard);

// Route to add a breakfast dish for a specific canteen
router.post('/:id/breakfast/add',auth,isCanteen, canteenController.addBreakfastDish);
router.post('/:id/breakfast/add',auth,isCanteen,multerUploads, canteenController.addBreakfastDish);

// Route to remove a breakfast dish for a specific canteen
router.delete('/:id/breakfast/remove',auth,isCanteen, canteenController.removeBreakfastDish);
Expand Down
2 changes: 1 addition & 1 deletion src/components/FoodCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function FoodCard({ dish, onClick }) {
// https://via.placeholder.com/150
return (
<div className="bg-white shadow-md rounded-lg overflow-hidden border-2 border-gray-300 cursor-pointer transition-transform duration-300 ease-in-out transform hover:-translate-y-1" onClick={onClick}>
<img src={dish.imageUrl || placeholderImage} alt={dish.dish || "No image available"} className="w-full h-48 object-cover"/>
<img src={dish.dishImage || placeholderImage} alt={dish.dish || "No image available"} className="w-full h-48 object-cover"/>
<div className="p-4">
<h3 className="text-lg text-black font-bold mb-2">{dish.dish}</h3>
<p className="text-gray-700">{dish.description || defaultDescription}</p>
Expand Down
Loading

0 comments on commit 31f6081

Please sign in to comment.