feat:Create and Test user,crop,farm
This commit is contained in:
@@ -1,11 +1,28 @@
|
|||||||
import Crop from "../models/cropModel.js";
|
const Crop = require("../Models/crop.model.js");
|
||||||
import Farm from "../models/farmModel.js";
|
const Farm = require("../Models/farm.model.js");
|
||||||
|
const { uploadOnCloudinary } = require("../Utils/cloudinary.js");
|
||||||
|
|
||||||
// Create a new crop
|
// Create a new crop
|
||||||
export const createCrop = async (req, res) => {
|
const createCrop = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { name, farm, image, harvestDate, growthStage, healthStatus } =
|
const { name, farm, harvestDate, growthStage, healthStatus } = req.body;
|
||||||
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
|
// Check if the farm exists
|
||||||
const existingFarm = await Farm.findById(farm);
|
const existingFarm = await Farm.findById(farm);
|
||||||
@@ -15,7 +32,7 @@ export const createCrop = async (req, res) => {
|
|||||||
const crop = new Crop({
|
const crop = new Crop({
|
||||||
name,
|
name,
|
||||||
farm,
|
farm,
|
||||||
image,
|
image: imageUrl,
|
||||||
harvestDate,
|
harvestDate,
|
||||||
growthStage,
|
growthStage,
|
||||||
healthStatus,
|
healthStatus,
|
||||||
@@ -34,8 +51,9 @@ export const createCrop = async (req, res) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Get all crops for a specific farm
|
// Get all crops for a specific farm
|
||||||
export const getCropsByFarm = async (req, res) => {
|
const getCropsByFarm = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
console.log("My farm id is : ", req.params.farmId);
|
||||||
const crops = await Crop.find({ farm: req.params.farmId });
|
const crops = await Crop.find({ farm: req.params.farmId });
|
||||||
|
|
||||||
res.status(200).json(crops);
|
res.status(200).json(crops);
|
||||||
@@ -45,7 +63,7 @@ export const getCropsByFarm = async (req, res) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Get a single crop by ID
|
// Get a single crop by ID
|
||||||
export const getCropById = async (req, res) => {
|
const getCropById = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const crop = await Crop.findById(req.params.cropId).populate("farm");
|
const crop = await Crop.findById(req.params.cropId).populate("farm");
|
||||||
|
|
||||||
@@ -58,7 +76,7 @@ export const getCropById = async (req, res) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Update crop details
|
// Update crop details
|
||||||
export const updateCrop = async (req, res) => {
|
const updateCrop = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const updatedCrop = await Crop.findByIdAndUpdate(
|
const updatedCrop = await Crop.findByIdAndUpdate(
|
||||||
req.params.cropId,
|
req.params.cropId,
|
||||||
@@ -76,7 +94,7 @@ export const updateCrop = async (req, res) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Delete a crop
|
// Delete a crop
|
||||||
export const deleteCrop = async (req, res) => {
|
const deleteCrop = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const crop = await Crop.findById(req.params.cropId);
|
const crop = await Crop.findById(req.params.cropId);
|
||||||
|
|
||||||
@@ -94,7 +112,7 @@ export const deleteCrop = async (req, res) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Update crop growth stage
|
// Update crop growth stage
|
||||||
export const updateGrowthStage = async (req, res) => {
|
const updateGrowthStage = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { growthStage } = req.body;
|
const { growthStage } = req.body;
|
||||||
|
|
||||||
@@ -111,7 +129,7 @@ export const updateGrowthStage = async (req, res) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Update crop health status
|
// Update crop health status
|
||||||
export const updateHealthStatus = async (req, res) => {
|
const updateHealthStatus = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { healthStatus } = req.body;
|
const { healthStatus } = req.body;
|
||||||
|
|
||||||
@@ -126,3 +144,13 @@ export const updateHealthStatus = async (req, res) => {
|
|||||||
res.status(500).json({ message: error.message });
|
res.status(500).json({ message: error.message });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
createCrop,
|
||||||
|
getCropsByFarm,
|
||||||
|
getCropById,
|
||||||
|
updateCrop,
|
||||||
|
deleteCrop,
|
||||||
|
updateGrowthStage,
|
||||||
|
updateHealthStatus,
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,20 +1,25 @@
|
|||||||
import express from "express";
|
const express = require("express");
|
||||||
import {
|
const {
|
||||||
createCrop,
|
createCrop,
|
||||||
getCrops,
|
getCropsByFarm,
|
||||||
getCropById,
|
getCropById,
|
||||||
updateCrop,
|
updateCrop,
|
||||||
deleteCrop,
|
deleteCrop,
|
||||||
} from "../controllers/cropController.js";
|
updateHealthStatus,
|
||||||
import { checkAuthenticated } from "../Middlewares/authentication.js";
|
updateGrowthStage,
|
||||||
|
} = require("../Controllers/crop.controller.js");
|
||||||
|
const { checkAuthenticated } = require("../Middlewares/authentication.js");
|
||||||
|
const upload = require("../Middlewares/multer.js");
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
// Routes for crop management
|
// Routes for crop management
|
||||||
router.post("/", checkAuthenticated, createCrop); // Create a new crop
|
router.post("/", checkAuthenticated, upload.single("image"), createCrop); // Create a new crop
|
||||||
router.get("/", checkAuthenticated, getCrops); // Get all crops
|
router.get("/farm/:farmId", checkAuthenticated, getCropsByFarm); // Get all crops
|
||||||
router.get("/:cropId", checkAuthenticated, getCropById); // Get a crop by ID
|
router.get("/:cropId", checkAuthenticated, getCropById); // Get a crop by ID
|
||||||
router.put("/:cropId", checkAuthenticated, updateCrop); // Update crop details
|
router.put("/:cropId", checkAuthenticated, updateCrop); // Update crop details
|
||||||
router.delete("/:cropId", checkAuthenticated, deleteCrop); // Delete a crop
|
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;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ const { Server } = require("socket.io");
|
|||||||
const { createServer } = require("http");
|
const { createServer } = require("http");
|
||||||
const userRoute = require("./Routes/user.routes.js");
|
const userRoute = require("./Routes/user.routes.js");
|
||||||
const farmRoute = require("./Routes/farm.routes.js");
|
const farmRoute = require("./Routes/farm.routes.js");
|
||||||
|
const cropRoute = require("./Routes/crop.routes.js");
|
||||||
|
|
||||||
const { checkAuthenticated } = require("./Middlewares/authentication.js");
|
const { checkAuthenticated } = require("./Middlewares/authentication.js");
|
||||||
const dotenv = require("dotenv");
|
const dotenv = require("dotenv");
|
||||||
@@ -35,4 +36,6 @@ app.use("/api/v1", userRoute);
|
|||||||
|
|
||||||
app.use("/api/v1/farm", farmRoute);
|
app.use("/api/v1/farm", farmRoute);
|
||||||
|
|
||||||
|
app.use("/api/v1/crop", cropRoute);
|
||||||
|
|
||||||
module.exports = app;
|
module.exports = app;
|
||||||
|
|||||||
Reference in New Issue
Block a user