| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- const Merchant = require("../models/merchant");
- const Ingredient = require("../models/ingredient");
- module.exports = {
- //GET - gets a list of all database ingredients
- //Returns:
- // ingredients: list containing all ingredients
- getIngredients: function(req, res){
- Ingredient.find()
- .then((ingredients)=>{
- return res.json(ingredients);
- })
- .catch((err)=>{
- let errorMessage = "Unable to retrieve ingredients";
- let error = new Error({
- code: 626,
- displayMessage: errorMessage,
- error: err
- });
- error.save();
- return res.json(errorMessage);
- });
- },
- //POST - creates new ingredients from a list
- //Inputs:
- // req.body: list of ingredients (name, category, unit)
- //Returns:
- // ingredients: list containing the newly created ingredients
- createNewIngredients: function(req, res){
- Ingredient.create(req.body)
- .then((ingredients)=>{
- return res.json(ingredients);
- })
- .catch((err)=>{
- let errorMessage = "There was an error and the ingredients could not be created";
- let error = new Error({
- code: 547,
- displayMessage: errorMessage,
- error: err
- });
- error.save();
- return res.json(errorMessage);
- });
- },
- //TODO - Redirect to merchantData.js rather than adding here
- //POST - create a single ingredient and then add to the merchant
- //Inputs:
- // req.body.ingredient: full ingredient to create (name, category, unit)
- // req.body.quantity: quantity of ingredient for merchant
- //Returns:
- // item: ingredient and quantity
- createIngredient: function(req, res){
- if(!req.session.user){
- req.session.error = "Must be logged in to do that";
- return res.redirect("/");
- }
-
- Ingredient.create(req.body.ingredient)
- .then((ingredient)=>{
- Merchant.findOne({_id: req.session.user})
- .then((merchant)=>{
- let item = {
- ingredient: ingredient,
- quantity: req.body.quantity
- }
- merchant.inventory.push(item);
- merchant.save()
- .then((merchant)=>{
- return res.json(item);
- })
- .catch((err)=>{
- let errorMessage = "There was an error and the data could not be saved";
- let error = new Error({
- code: 547,
- displayMessage: errorMessage,
- error: err
- });
- error.save();
-
- return res.json(errorMessage);
- });
- })
- .catch((err)=>{
- let errorMessage = "There was an error and your data could not be retrieved";
- let error = new Error({
- code: 626,
- displayMessage: errorMessage,
- error: err
- });
- error.save();
-
- return res.json(errorMessage);
- });
- })
- .catch((err)=>{
- let errorMessage = "There was an error and the ingredient could not be created";
- let error = new Error({
- code: 547,
- displayMessage: errorMessage,
- error: err
- });
- error.save();
- return res.json(errorMessage);
- });
- }
- }
|