Skip to content

Commit 5672068

Browse files
committed
Refactor Initial Code
1 parent 9c00b16 commit 5672068

File tree

13 files changed

+1257
-504
lines changed

13 files changed

+1257
-504
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ dist
2525
dist-ssr
2626
*.local
2727

28-
config.env
28+
.env

app.js

Lines changed: 20 additions & 200 deletions
Original file line numberDiff line numberDiff line change
@@ -1,210 +1,30 @@
1-
// const dotenv = require("dotenv");
2-
// dotenv.config({ path: "./config.env" });
1+
// Imports from libraries
32
const express = require("express");
43
const bodyParser = require("body-parser");
5-
const striptags = require("striptags");
6-
const cheerio = require("cheerio");
7-
84
const ejs = require("ejs");
95

6+
// Imports from projects
7+
const viewsRoutes = require("./routes/viewsRoutes");
8+
const weekMenusRoutes = require("./routes/weekMenusRoutes");
9+
const fridgeRoutes = require("./routes/fridgeRoutes");
10+
const tagRoutes = require("./routes/tagRoutes");
11+
const recipeRoutes = require("./routes/recipeRoutes");
12+
1013
const app = express();
1114

12-
app.set("view engine", "ejs");
15+
// Req body modifiers
1316
app.use(bodyParser.urlencoded({ extended: true }));
14-
app.use(express.static("public"));
15-
16-
const apiKey = "54fe1ed340324dde9577a8ba35e809fc";
17-
18-
// All the get request to render different page.
19-
20-
app.get("/", (req, res) => {
21-
res.render("home");
22-
});
23-
24-
app.get("/inputWeekPlanMeals", (req, res) => {
25-
res.render("inputWeekPlanMeals");
26-
});
27-
28-
app.get("/inputIngredient", (req, res) => {
29-
res.render("inputIngredient");
30-
});
31-
32-
app.get("/inputRandom", (req, res) => {
33-
res.render("inputRandom");
34-
});
35-
36-
// POST request to render menus for a week based on user input
37-
38-
app.post("/mealList", async (req, res) => {
39-
// Variables for meal planning based on user input
40-
const targetCalories = req.body.targetCalories;
41-
let diets = req.body.diets;
42-
if (typeof diets === "string") {
43-
diets = req.body.diets.split(",");
44-
}
45-
let cuisines = req.body.cusines;
46-
if (typeof cuisines === "string") {
47-
cuisines = req.body.cuisines.split(",");
48-
}
49-
let excludeIngredients = req.body.excludeIngredients;
50-
if (typeof excludeIngredients === "string") {
51-
excludeIngredients = req.body.excludeIngredients.split(",");
52-
}
53-
let includeIngredients = req.body.includeIngredients;
54-
if (typeof includeIngredients === "string") {
55-
includeIngredients = req.body.includeIngredients.split(",");
56-
}
57-
let preferences = req.body.preferences;
58-
if (typeof preferences === "string") {
59-
preferences = req.body.preferences.split(",");
60-
}
61-
62-
try {
63-
const { default: fetch } = await import("node-fetch");
64-
65-
// Week meal plan using user input
66-
const weekPlan = await fetch(
67-
`https://api.spoonacular.com/mealplanner/generate?apiKey=${apiKey}&timeFrame=week&targetCalories=${targetCalories}&diet=${diets}&excludeIngredients=${excludeIngredients}&includeIngredients=${includeIngredients}&cuisine=${cuisines}&preferences=${preferences}`
68-
);
69-
70-
const dataOfMeal = await weekPlan.json();
71-
const days = await dataOfMeal.week;
72-
73-
res.render("planMeals", { meals: days });
74-
} catch (error) {
75-
res.send("Sorry! Some error occurred. Please try again!");
76-
console.log(error);
77-
}
78-
});
79-
80-
//POST request to generate recipe based on ingredients provided by user
81-
82-
app.post("/ingredientList", async (req, res) => {
83-
// Storing the input provided by user in a variable in backend
84-
85-
let ingredients = req.body.ingredients;
86-
if (typeof ingredients === "string") {
87-
ingredients = req.body.ingredients.split(",");
88-
}
89-
90-
let number = req.body.number;
91-
if (typeof number === "string") {
92-
number = req.body.number.split(",");
93-
}
94-
95-
try {
96-
const { default: fetch } = await import("node-fetch");
9717

98-
//Generate recipes by ingredients
99-
const generateRecipesByIngredients = await fetch(
100-
`https://api.spoonacular.com/recipes/findByIngredients?ingredients=${ingredients}&number=${number}&apiKey=${apiKey}`
101-
);
102-
const recipesData = await generateRecipesByIngredients.json();
103-
104-
// Generate recipe information
105-
const recipeInfoPromises = recipesData.map(async (recipe) => {
106-
const recipeInfo = await fetch(
107-
`https://api.spoonacular.com/recipes/${recipe.id}/nutritionWidget.json?apiKey=${apiKey}`
108-
);
109-
const infoData = await recipeInfo.json();
110-
return {
111-
...recipe,
112-
nutrients: infoData,
113-
};
114-
});
115-
116-
// Wait for all recipe nutrient information to be fetched
117-
const recipesWithNutrients = await Promise.all(recipeInfoPromises);
118-
119-
res.render("planMealsByIngredients", {
120-
recipes: recipesWithNutrients,
121-
});
122-
123-
// res.send(recipesWithNutrients);
124-
} catch (error) {
125-
res.send("Sorry! Some error occurred. Please try again!");
126-
console.log(error);
127-
}
128-
});
129-
130-
//POST request to generate random list of recipes based on tags provided by user
131-
132-
app.post("/randomList", async (req, res) => {
133-
// Tags and number provided by user
134-
135-
const number = req.body.number;
136-
let tags = req.body.tags;
137-
if (typeof tags === "strings") {
138-
tags = req.body.tags.split(",");
139-
}
140-
141-
try {
142-
const { default: fetch } = await import("node-fetch");
143-
const generateRandomRecipes =
144-
await fetch(`https://api.spoonacular.com/recipes/random?number=${number}&tags=${tags}&apiKey=${apiKey}
145-
`);
146-
147-
const randomData = await generateRandomRecipes.json();
148-
149-
// Generate Recipe information including nutrients
150-
151-
const recipeInfoPromises = randomData.recipes.map(async (recipe) => {
152-
const recipeInfo = await fetch(
153-
`https://api.spoonacular.com/recipes/${recipe.id}/nutritionWidget.json?apiKey=${apiKey}`
154-
);
155-
const infoData = await recipeInfo.json();
156-
return {
157-
...recipe,
158-
nutrients: infoData,
159-
};
160-
});
161-
162-
// Wait for all recipe nutrient information to be fetched
163-
const recipesWithNutrients = await Promise.all(recipeInfoPromises);
164-
165-
res.render("randomMeals", {
166-
recipes: recipesWithNutrients,
167-
});
168-
} catch (error) {
169-
res.send("Sorry! Some error occurred. Please try again!");
170-
console.log(error);
171-
}
172-
});
173-
174-
app.get("/recipe/:mealId", async (req, res) => {
175-
const mealId = req.params.mealId;
176-
177-
const { default: fetch } = await import("node-fetch");
178-
179-
//Recipe info based on meal id
180-
const recipeInfo =
181-
await fetch(`https://api.spoonacular.com/recipes/${mealId}/information?apiKey=${apiKey}&includeNutrition=true
182-
`);
183-
const info = await recipeInfo.json();
184-
185-
// Summary of the recipe based on meal id
186-
187-
const summaryInfo = await fetch(
188-
`https://api.spoonacular.com/recipes/${mealId}/summary?apiKey=${apiKey}`
189-
);
190-
const summary = await summaryInfo.json();
191-
192-
//Ingredient info based on meal id
193-
const ingredientsInfo =
194-
await fetch(`https://api.spoonacular.com/recipes/${mealId}/ingredientWidget.json?apiKey=${apiKey}
195-
`);
196-
const ingredientsData = await ingredientsInfo.json();
18+
// Views middleware
19+
app.set("view engine", "ejs");
20+
app.use(express.static("public"));
19721

198-
res.render("recipe", {
199-
title: info.title,
200-
readyInMinutes: info.readyInMinutes,
201-
image: info.image,
202-
nutrition: info.nutrition,
203-
summary: striptags(summary.summary),
204-
analyzedInstructions: info.analyzedInstructions,
205-
ingredients: ingredientsData.ingredients,
206-
});
207-
// res.send(summary.summary);
208-
});
22+
// -----------------------------------
23+
// ROUTERS
24+
app.use("/", viewsRoutes);
25+
app.post("/mealList", weekMenusRoutes);
26+
app.post("/ingredientList", fridgeRoutes);
27+
app.post("/randomList", tagRoutes);
28+
app.get("/recipe/:mealId", recipeRoutes);
20929

210-
module.exports.main = app;
30+
module.exports = app;

env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SPOONACULAR_API_KEY=<SPOONACULAR_API_KEY> # (https://spoonacular.com/food-api/)
2+
DB_URI=

models/recipes/recipeModel.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const mongoose = require("mongoose");
2+
3+
const recipesSchema = new mongoose.Schema({
4+
name: {
5+
type: String,
6+
required: true,
7+
},
8+
image: {
9+
type: String,
10+
required: true,
11+
},
12+
id: {
13+
type: String,
14+
required: true,
15+
},
16+
time: {
17+
type: String,
18+
}
19+
});
20+
21+
const Recipe = mongoose.model('Recipe', recipesSchema);
22+
23+
module.exports = Recipe;

0 commit comments

Comments
 (0)