<repo_name>NGowthamKumar/FoodFactoryApp<file_sep>/src/controller/userController.js
import User from "../model/userSchema";
var md5 = require("md5");

export const userLogin = (req, res) => {
  req.body.password = md5(req.body.password);
  User.findOne({
    $and: [{ email: req.body.email }, { password: req.body.password }],
  }).exec((error, result) => {
    if (error || result == null) {
      res.send("Incorrect username or password." + error);
    } else {
      res.send("User login successful");
    }
  });
};

export const userSignup = (req, res) => {
  User.findOne({ email: req.body.email }).exec((error, result) => {
    if (error === null) {
      req.body.password = md5(req.body.password);
      const user = new User(req.body);
      user.save((error, result) => {
        if (error) res.send("Error in saving User :" + error);
        else res.send("User registered" + result);
      });
    } else {
      res.send("User already registered");
    }
  });
};

export const updatePassword = (req, res) => {
  req.body.password = md5(<PASSWORD>);
  User.updateOne(
    { email: req.body.email },
    { $set: { password: req.body.<PASSWORD> } }
  ).exec((error, result) => {
    if (error) {
      res.send("Error in updating user password:" + error);
    } else {
      res.send("User password updated");
    }
  });
};

export const updateUser = (req, res) => {
  User.updateOne(
    { email: req.body.email },
    { $set: { name: req.body.name, password: req.body.password } }
  ).exec((error, result) => {
    if (error) {
      res.send("Error in updating User:" + error);
    } else {
      res.send("User updated");
    }
  });
};

export const updateUserStatus = (req, res) => {
  User.updateOne(
    { email: req.body.email },
    { $set: { status: req.body.status } }
  ).exec((error, result) => {
    if (error) {
      res.send("Error in updating User Status:" + error);
    } else {
      res.send("User Status updated");
    }
  });
};

export const deactivateUser = (req, res) => {
  User.updateOne(
    { email: req.body.email },
    { $set: { status: "Deactive" } }
  ).exec((error, result) => {
    if (error) {
      res.send("Error in deactivating User" + error);
    } else {
      res.send("User deactivated");
    }
  });
};
<file_sep>/src/index.js
import app from "./config/express";
require("./config/mongoose");

const PORT = 8000;
app.use("/user", require("./routes/userRoutes"));
app.use("/food", require("./routes/foodRoutes"));
app.use("/ingredients", require("./routes/ingredientsRoutes"));
app.use("/order", require("./routes/orderRoutes"));

app.listen(PORT, () => {
  console.log("Running on port " + PORT + "...");
});
<file_sep>/src/controller/ingredientsController.js
import Ingredients from "../model/ingredientsSchema";

export const getIngredients = (req, res) => {
  Ingredients.findOne({ lotNumber: req.params.lotNumber }).exec(
    (error, result) => {
      if (error || result == null) {
        res.send("Error in getting Ingredients :" + error);
      } else {
        res.send("Ingredients found " + result);
      }
    }
  );
};

export const getLessAvailableIngredients = (req, res) => {
  let lessAvailable = [];
  Ingredients.find({}).exec((error, result) => {
    if (error || result == null) {
      res.send("Error in getting Ingredients :" + error);
    } else {
      lessAvailable = result.filter(
        (Ingredients) =>
          Ingredients.availableQuantity < Ingredients.thresholdQuantity
      );
      res.send(
        lessAvailable.length +
          " Ingredients have less Availability " +
          lessAvailable
      );
    }
  });
};

export const getIngredientsOfVendor = (req, res) => {
  Ingredients.find({ vendorName: req.params.vendorName }).exec(
    (error, result) => {
      if (error || result == null) {
        res.send("Error in getting Ingredients :" + error);
      } else {
        res.send("Vendor have " + result.length + " Ingredients " + result);
      }
    }
  );
};

export const createIngredients = (req, res) => {
  const ingredients = new Ingredients(req.body);
  ingredients.save((error, result) => {
    if (error) {
      res.send("Error in adding Ingredients :" + error);
    } else {
      res.send("Ingredients added " + result);
    }
  });
};

export const updateIngredients = (req, res) => {
  Ingredients.updateOne(
    { lotNumber: req.body.lotNumber },
    { $set: req.body }
  ).exec((error, result) => {
    if (error) {
      res.send("Error in updating Ingredients :" + error);
    } else {
      res.send("Ingredients updated");
    }
  });
};

export const deleteIngredients = (req, res) => {
  Ingredients.deleteOne({ lotNumber: req.params.lotNumber }).exec(
    (error, result) => {
      if (error) {
        res.send("Error in deleting Ingredients :" + error);
      } else {
        res.send("Ingredients deleted");
      }
    }
  );
};
<file_sep>/src/model/ingredientsSchema.js
const mongoose = require("mongoose");

const ingredientsSchema = new mongoose.Schema(
  {
    name: String,
    lotNumber: { type: Number, unique: true, required: true },
    availableQuantity: Number,
    thresholdQuantity: Number,
    price: Number,
    vendorName: String,
    vendorEmail: String,
  },
  { versionKey: false }
);

module.exports = mongoose.model("Ingredients", ingredientsSchema);
<file_sep>/src/routes/foodRoutes.js
import router from "../config/router";
import * as foodController from "../controller/foodController";

router.get("/getFood/:lotNumber", foodController.getFood);
router.get("/getHighExpensiveFoods", foodController.getHighExpensiveFoods);
router.post("/createFood", foodController.createFood);
router.put("/updateFood", foodController.updateFood);
router.delete("/deleteFood/:lotNumber", foodController.deleteFood);

module.exports = router;
<file_sep>/src/model/foodSchema.js
const mongoose = require("mongoose");

const foodSchema = new mongoose.Schema(
  {
    name: String,
    createdAt: Date,
    cuisine: String,
    ingredients: String,
    lotNumber: { type: Number, unique: true, required: true },
    costOfProduction: Number,
    sellingCost: Number,
  },
  { versionKey: false }
);

module.exports = mongoose.model("Food", foodSchema);
<file_sep>/src/routes/orderRoutes.js
import router from "../config/router";
import * as orderController from "../controller/orderController";

router.get("/getOrder/:orderNum", orderController.getOrder);
router.get("/getAllOrdersOfUser/:email", orderController.getAllOrdersOfUser);
router.post("/createOrder", orderController.createOrder);
router.put("/updateOrder", orderController.updateOrder);
router.delete("/deleteOrder/:orderNum", orderController.deleteOrder);

module.exports = router;
<file_sep>/src/routes/ingredientsRoutes.js
import router from "../config/router";
import * as ingredientsController from "../controller/ingredientsController";

router.get("/getIngredients/:lotNumber", ingredientsController.getIngredients);
router.get(
  "/getLessAvailableIngredients",
  ingredientsController.getLessAvailableIngredients
);
router.get(
  "/getIngredientsOfVendor/:vendorName",
  ingredientsController.getIngredientsOfVendor
);
router.post("/createIngredients", ingredientsController.createIngredients);
router.put("/updateIngredients", ingredientsController.updateIngredients);
router.delete(
  "/deleteIngredients/:lotNumber",
  ingredientsController.deleteIngredients
);

module.exports = router;
<file_sep>/src/controller/orderController.js
import Order from "../model/orderSchema";
import User from "../model/userSchema";
import Food from "../model/foodSchema";

export const getOrder = (req, res) => {
  Order.findOne({ orderNum: req.params.orderNum })
    .populate("user")
    .populate("food")
    .exec((error, result) => {
      if (error) {
        res.send("Error in getting Order :" + error);
      } else {
        res.send("Order found " + result);
      }
    });
};

export const getAllOrdersOfUser = (req, res) => {
  User.findOne({ email: req.params.email }).exec((error, userResult) => {
    if (error) {
      res.send("User not Found : " + error);
    } else {
      Order.find({ user: userResult._id }).exec((error, result) => {
        if (error) {
          res.send("Error in getting Orders :" + error);
        } else {
          res.send(result.length + " Orders found " + result);
        }
      });
    }
  });
};

export const createOrder = (req, res) => {
  const userEmail = req.body.email;
  User.findOne({ email: req.body.email }).exec((error, userResult) => {
    if (error || userResult == null) {
      res.send("User not Found : " + error);
    } else {
      Food.findOne({ lotNumber: req.body.lotNumber }).exec(
        (error, foodResult) => {
          if (error || foodResult == null) {
            res.send("Food not Found : " + error);
          } else {
            req.body.user = userResult._id;
            req.body.food = foodResult._id;
            const order = new Order(req.body);
            order.save((error, result) => {
              if (error) {
                res.send("Error in creating Order :" + error);
              } else {
                res.send("Order created " + result);
              }
            });
          }
        }
      );
    }
  });
};

export const updateOrder = (req, res) => {
  Order.updateOne({ orderNum: req.body.orderNum }, { $set: req.body }).exec(
    (error, result) => {
      if (error) {
        res.send("Error in updating Order :" + error);
      } else {
        res.send("Order updated");
      }
    }
  );
};

export const deleteOrder = (req, res) => {
  Order.deleteOne({ orderNum: req.params.orderNum }).exec((error, result) => {
    if (error) {
      res.send("Error in deleting Order :" + error);
    } else {
      res.send("Order deleted");
    }
  });
};
