diff --git a/Backend/Controllers/crop.controller.js b/Backend/Controllers/crop.controller.js index 60c3930..42dd896 100644 --- a/Backend/Controllers/crop.controller.js +++ b/Backend/Controllers/crop.controller.js @@ -1,11 +1,28 @@ -import Crop from "../models/cropModel.js"; -import Farm from "../models/farmModel.js"; +const Crop = require("../Models/crop.model.js"); +const Farm = require("../Models/farm.model.js"); +const { uploadOnCloudinary } = require("../Utils/cloudinary.js"); // Create a new crop -export const createCrop = async (req, res) => { +const createCrop = async (req, res) => { try { - const { name, farm, image, harvestDate, growthStage, healthStatus } = - req.body; + const { name, farm, harvestDate, growthStage, healthStatus } = req.body; + if (!req.file.path) { + res.status(500).json({ + success: false, + message: "Avatar not uploaded on cloudinary.", + }); + } + + const imageUrl = await uploadOnCloudinary(req.file.path); + + console.log("Image url is : ", imageUrl); + + if (!imageUrl) { + return res.status(500).json({ + success: false, + message: "Image not uploaded on cloudinary.", + }); + } // Check if the farm exists const existingFarm = await Farm.findById(farm); @@ -15,7 +32,7 @@ export const createCrop = async (req, res) => { const crop = new Crop({ name, farm, - image, + image: imageUrl, harvestDate, growthStage, healthStatus, @@ -34,8 +51,9 @@ export const createCrop = async (req, res) => { }; // Get all crops for a specific farm -export const getCropsByFarm = async (req, res) => { +const getCropsByFarm = async (req, res) => { try { + console.log("My farm id is : ", req.params.farmId); const crops = await Crop.find({ farm: req.params.farmId }); res.status(200).json(crops); @@ -45,7 +63,7 @@ export const getCropsByFarm = async (req, res) => { }; // Get a single crop by ID -export const getCropById = async (req, res) => { +const getCropById = async (req, res) => { try { const crop = await Crop.findById(req.params.cropId).populate("farm"); @@ -58,7 +76,7 @@ export const getCropById = async (req, res) => { }; // Update crop details -export const updateCrop = async (req, res) => { +const updateCrop = async (req, res) => { try { const updatedCrop = await Crop.findByIdAndUpdate( req.params.cropId, @@ -76,7 +94,7 @@ export const updateCrop = async (req, res) => { }; // Delete a crop -export const deleteCrop = async (req, res) => { +const deleteCrop = async (req, res) => { try { const crop = await Crop.findById(req.params.cropId); @@ -94,7 +112,7 @@ export const deleteCrop = async (req, res) => { }; // Update crop growth stage -export const updateGrowthStage = async (req, res) => { +const updateGrowthStage = async (req, res) => { try { const { growthStage } = req.body; @@ -111,7 +129,7 @@ export const updateGrowthStage = async (req, res) => { }; // Update crop health status -export const updateHealthStatus = async (req, res) => { +const updateHealthStatus = async (req, res) => { try { const { healthStatus } = req.body; @@ -126,3 +144,13 @@ export const updateHealthStatus = async (req, res) => { res.status(500).json({ message: error.message }); } }; + +module.exports = { + createCrop, + getCropsByFarm, + getCropById, + updateCrop, + deleteCrop, + updateGrowthStage, + updateHealthStatus, +}; diff --git a/Backend/Routes/crop.routes.js b/Backend/Routes/crop.routes.js index 38fd0f1..8a43234 100644 --- a/Backend/Routes/crop.routes.js +++ b/Backend/Routes/crop.routes.js @@ -1,20 +1,25 @@ -import express from "express"; -import { +const express = require("express"); +const { createCrop, - getCrops, + getCropsByFarm, getCropById, updateCrop, deleteCrop, -} from "../controllers/cropController.js"; -import { checkAuthenticated } from "../Middlewares/authentication.js"; + updateHealthStatus, + updateGrowthStage, +} = require("../Controllers/crop.controller.js"); +const { checkAuthenticated } = require("../Middlewares/authentication.js"); +const upload = require("../Middlewares/multer.js"); const router = express.Router(); // Routes for crop management -router.post("/", checkAuthenticated, createCrop); // Create a new crop -router.get("/", checkAuthenticated, getCrops); // Get all crops +router.post("/", checkAuthenticated, upload.single("image"), createCrop); // Create a new crop +router.get("/farm/:farmId", checkAuthenticated, getCropsByFarm); // Get all crops router.get("/:cropId", checkAuthenticated, getCropById); // Get a crop by ID router.put("/:cropId", checkAuthenticated, updateCrop); // Update crop details router.delete("/:cropId", checkAuthenticated, deleteCrop); // Delete a crop +router.put("/health/:cropId", checkAuthenticated, updateHealthStatus); +router.put("/growth/:cropId", checkAuthenticated, updateGrowthStage); -export default router; +module.exports = router; diff --git a/Backend/app.js b/Backend/app.js index 145fba4..34be975 100644 --- a/Backend/app.js +++ b/Backend/app.js @@ -5,6 +5,7 @@ const { Server } = require("socket.io"); const { createServer } = require("http"); const userRoute = require("./Routes/user.routes.js"); const farmRoute = require("./Routes/farm.routes.js"); +const cropRoute = require("./Routes/crop.routes.js"); const { checkAuthenticated } = require("./Middlewares/authentication.js"); const dotenv = require("dotenv"); @@ -35,4 +36,6 @@ app.use("/api/v1", userRoute); app.use("/api/v1/farm", farmRoute); +app.use("/api/v1/crop", cropRoute); + module.exports = app;