forked from kth-csc-iprog/dinnerplanner-html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdinnerModel.js
103 lines (89 loc) · 2.67 KB
/
dinnerModel.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
class DinnerModel {
constructor() {
this.nrGuests = 0;
this.menu = [];
}
//sets the number of guests
setNumberOfGuests(num) {
if(num < 1)
num = 1;
this.nrGuests = num;
}
//returns the number of guests
getNumberOfGuests() {
return this.nrGuests;
}
//Returns the dishes that are on the menu for selected type
getSelectedDishes(type) {
return this.menu.filter(dish => dish.dishTypes.includes(type));
}
//Returns all the dishes on the menu.
getFullMenu() {
return this.menu;
}
//Returns all ingredients for all the dishes on the menu.
getAllIngredients() {
var fullingredients = [];
return this.menu.map(dish => dish.extendedIngredients.filter(ingredients => !fullingredients.includes(ingredients))).flat.map(x => x.name);
}
//Returns the total price of the menu (price per serving of each dish multiplied by number of guests).
getTotalMenuPrice() {
return this.menu.reduce((a,b) => a+b.pricePerServing, " ") * this.getNumberOfGuests();
}
//Adds the passed dish to the menu.
addDishToMenu(dish) {
if(this.menu.includes(dish))
{
this.removeDishFromMenu(dish.id);
}
this.menu.push(dish);
}
//Removes dish with specified id from menu
removeDishFromMenu(id) {
this.menu = this.menu.filter(dish => dish.id !== id)
}
//Returns all dishes of specific type (i.e. "starter", "main dish" or "dessert").
//query argument, text, if passed only returns dishes that contain the query in name or one of the ingredients.
//if you don't pass any query, all the dishes will be returned
getAllDishes(type, query) {
document.getElementById("loader").style.display = "";
if(!type)
type = ''
if(!query)
query = ''
return fetch((ENDPOINT.concat("/recipes/search?type=")
.concat(type).concat("&query=").concat(query)),
{headers:{"X-Mashape-Key" : API_KEY }})
.then(catchErrors)
.then(response => response.json())
.then(response => {
document.getElementById("loader").style.display = "none";
return response.results
})
.catch((error) => {
console.log(error)
});
}
//Returns a dish of specific ID
getDish(id) {
document.getElementById("loader").style.display = "";
return fetch(ENDPOINT+"/recipes/"+id+"/information",
{headers:{"X-Mashape-Key" : API_KEY }})
.then(catchErrors)
.then(response => {
document.getElementById("loader").style.display = "none";
return response.json()
})
.catch((error) => {
console.log(error)
});
}
}
//the error handler
function catchErrors(resp) {
if(resp.status === 200) {
return resp;
}
else
return resp;
}