Website : rimsha.abasa.com
backdoor
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
var
/
www
/
ah7-backend
/
src
/
controllers
/
Filename :
food.controller.js
back
Copy
import Food from "../models/food.model.js"; import responseHandler from "../utils/ResponseHandler.js"; import { uploadFileToS3, deleteFileFromS3 } from "../services/s3Service.js"; // GET ALL FOODS const getFoods = async (req, res) => { try { const foods = await Food.find({}); return responseHandler.success(res, "Foods fetched successfully", foods); } catch (error) { return responseHandler.error(res, "Failed to fetch foods", 500, error.message); } }; // ADD FOOD (with image upload to S3) const addFood = async (req, res) => { try { const { food_name, calories, total_fat, protein, carbohydrates, sugars, fiber, sodium, cholesterol } = req.body; const imageFile = req.file; // multer single('image') se aayega if (!food_name || !calories || !imageFile) { return responseHandler.error(res, "Required fields missing", 400); } // Upload image to S3 const { publicURL: imageURL } = await uploadFileToS3(imageFile); const newFood = new Food({ food_name, image: imageURL, calories, total_fat, protein, carbohydrates, sugars, fiber, sodium, cholesterol, }); await newFood.save(); return responseHandler.success(res, "Food added successfully", newFood); } catch (error) { return responseHandler.error(res, "Failed to add food", 500, error.message); } }; // UPDATE FOOD (with optional image update) const updateFood = async (req, res) => { try { const { id } = req.params; const { food_name, calories, total_fat, protein, carbohydrates, sugars, fiber, sodium, cholesterol } = req.body; const imageFile = req.file; const existingFood = await Food.findById(id); if (!existingFood) { return responseHandler.error(res, "Food not found", 404); } // Upload new image to S3 if provided let imageURL = existingFood.image; if (imageFile) { if (existingFood.image) await deleteFileFromS3(existingFood.image); // delete old image const { publicURL } = await uploadFileToS3(imageFile); imageURL = publicURL; } const updatedFood = await Food.findByIdAndUpdate( id, { food_name, image: imageURL, calories, total_fat, protein, carbohydrates, sugars, fiber, sodium, cholesterol, }, { new: true, runValidators: true } ); return responseHandler.success(res, "Food updated successfully", updatedFood); } catch (error) { return responseHandler.error(res, "Failed to update food", 500, error.message); } }; // DELETE FOOD (with image deletion from S3) const deleteFood = async (req, res) => { try { const { id } = req.params; const food = await Food.findById(id); if (!food) { return responseHandler.error(res, "Food not found", 404); } // Delete image from S3 if (food.image) { await deleteFileFromS3(food.image); } await Food.findByIdAndDelete(id); return responseHandler.success(res, "Food deleted successfully"); } catch (error) { return responseHandler.error(res, "Failed to delete food", 500, error.message); } }; export { getFoods, addFood, updateFood, deleteFood };