diff --git a/Backend/Controllers/farm.controller.js b/Backend/Controllers/farm.controller.js index 19c77e7..054858d 100644 --- a/Backend/Controllers/farm.controller.js +++ b/Backend/Controllers/farm.controller.js @@ -39,6 +39,7 @@ const getUserFarms = async (req, res) => { // Get a single farm by ID const getFarmById = async (req, res) => { try { + console.log("also i am clla ing", "My farm id is : ", req.params.farmId); const farm = await Farm.findById(req.params.farmId) .populate("crops") .populate("finances"); diff --git a/Backend/Controllers/finance.controller.js b/Backend/Controllers/finance.controller.js index df6e982..58497f5 100644 --- a/Backend/Controllers/finance.controller.js +++ b/Backend/Controllers/finance.controller.js @@ -2,15 +2,55 @@ const Finance = require("../Models/finance.model.js"); const Farm = require("../Models/farm.model.js"); // Create finance record for a farm +// const createFinance = async (req, res) => { +// try { +// const { farm } = req.body; + +// console.log("My farm id is which is going to be created : ", req.body); + +// // Check if the farm exists +// const existingFarm = await Farm.findById(farm); +// if (!existingFarm) +// return res.status(404).json({ message: "Farm not found" }); + +// const finance = new Finance({ +// farm, +// transactions: [], +// totalExpenses: 0, +// totalRevenue: 0, +// }); + +// await finance.save(); + +// // Link finance to farm +// existingFarm.finances = finance._id; +// await existingFarm.save(); + +// res.status(201).json(finance); +// } catch (error) { +// res.status(500).json({ message: error.message }); +// } +// }; const createFinance = async (req, res) => { try { const { farm } = req.body; + console.log("My farm id is which is going to be created : ", farm); + // Check if the farm exists const existingFarm = await Farm.findById(farm); - if (!existingFarm) + if (!existingFarm) { return res.status(404).json({ message: "Farm not found" }); + } + // Check if finance already exists for this farm + if (existingFarm.finances) { + return res + .status(400) + .json({ message: "Finance already exists for this farm" }); + } + + // Create finance entry const finance = new Finance({ farm, transactions: [], @@ -50,6 +90,12 @@ const addTransaction = async (req, res) => { try { const { type, amount, description } = req.body; + console.log("My type is : ", type); + console.log("My amount is : ", amount); + console.log("My description is : ", description); + + console.log("My finance id is : ", req.params.financeId); + const finance = await Finance.findById(req.params.financeId); if (!finance) return res.status(404).json({ message: "Finance record not found" }); diff --git a/Backend/app.js b/Backend/app.js index 0560f82..f236985 100644 --- a/Backend/app.js +++ b/Backend/app.js @@ -30,7 +30,7 @@ app.use(express.static("public")); app.use(cookieParser()); app.get("/", (req, res) => { - return res.send("Hiddskpkpk..."); + return res.send("Server is running..."); }); app.use("/api/v1", userRoute); @@ -43,6 +43,4 @@ app.use("/api/v1/finance", financeRoute); app.use("/api/v1/task", taskRoute); - - module.exports = app;