forked from aahmadyar123/CookMyFridge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecipe-services.js
134 lines (123 loc) · 3.3 KB
/
recipe-services.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const recipeModel = require("../models/recipe");
// create a new recipe
async function addRecipe(recipe) {
try {
const newRecipe = new recipeModel(recipe);
const savedRecipe = await newRecipe.save();
return savedRecipe;
} catch (error) {
console.log(error);
return undefined;
}
}
async function getRecipeByWebID(id) {
/*
Find recipe from spoonacular id (used to check DB if recipe already added as to not add duplicates)
:param id: spoonacular id for recipe
:return: JSON object representing recipe
*/
//try {
const result = await recipeModel.findOne({ id: id });
return result;
// } catch (error) {
// console.log(error);
// return undefined;
// }
}
// find a recipe by ID
async function getRecipeByID(id) {
/*
Finds recipe by databse id
:param id: id for entry in DB
*/
try {
return await recipeModel.findById(id);
} catch (error) {
console.log(error);
return undefined;
}
}
async function getRatings(recipeID) {
/*
Returns ratings associated with recipe
:param recipeID: databse id of recipe
:return: recipe object
*/
// try {
const recipe = await getRecipeByID(recipeID);
if (!recipe) return undefined;
return recipe;
// } catch (error) {
// console.log(error);
// return undefined;
// }
}
async function addRating(recipeID, rating) {
/*
Adds rating to recipe
:param recipeID: recipe database id
:param rating: JSON containing recipe review
:return: recipe object
*/
// try {
//find recipe then update average rating and push new rating
const recipe = await getRecipeByID(recipeID);
if (!recipe) return undefined;
if (!rating.score) rating.score = 0;
//check if score updated successfully
if (updateAverageRating(recipe, rating.score)) {
recipe.ratings.push(rating);
await recipe.save(); //save updated recipe to DB
return recipe;
} else {
return undefined;
}
// } catch (error) {
// console.log(error);
// return undefined;
// }
}
// --------------------------------------------------
// HELPER FUNCTIONS
// --------------------------------------------------
// async function findRecipeByName(name) {
// return await recipeModel.find({ name: name });
// }
// This function is only used inside this file (it's a helper function)
function updateAverageRating(recipe, rating) {
/*
Updates average rating of a recipe
:param recipe: recipeModel object
:param: rating: integer represnting rating
:return: boolean specifying if rating successfully updated
*/
// try {
//check for valid rating
if (rating < 0 || rating > 5) {
return false;
}
//get current rating and total number of ratings
let curRating = recipe.rating;
let numRatings = recipe.ratings.length;
//average previous rating and new rating and weight them based on how many previous ratings
recipe.rating =
curRating * (numRatings / (numRatings + 1)) +
rating * (1 / (numRatings + 1));
return true;
// } catch (error) {
// console.log(error);
// return false;
// }
}
// --------------------------------------------------
// EXPORTS
// --------------------------------------------------
module.exports = {
// getRecipes,
addRecipe,
getRecipeByID,
// updateAverageRating,
getRecipeByWebID,
getRatings,
addRating,
};