-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.js
More file actions
142 lines (128 loc) · 3.66 KB
/
app.js
File metadata and controls
142 lines (128 loc) · 3.66 KB
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
135
136
137
138
139
140
141
142
import express from "express";
const app = express();
const port = 3002;
import { getCocktails,
getCocktailById,
addCocktail,
editCocktail,
deleteCocktail,
getCocktailByKey
} from "./cocktail.js";
app.use(express.json());
//MVP 2 - create a GET listener that returns cocktail by id
// create variable that pulls the id from the uri
// from path /cocktails/:id
//MVP 3 - create a post listener that create a new cocktail
// create a variable that gets the new cocktails from the request body
// use the add cocktail function
// send response
app.post("/cocktails", async (req, res) => {
try {
const { id, name, alcoholic, category, glassType, instructions, ingredients, ingredientMeasures } = req.body;
const cocktail = await addCocktail(id, name, alcoholic, category, glassType, instructions, ingredients, ingredientMeasures);
if (cocktail) {
res.status(201).json({
success: true,
payload: cocktail,
});
} else {
res.status(400).json({
success: false,
payload: "Failed to add cocktail"
});
}
} catch (error) {
console.error("Error!", error);
res.status(500).json({
success: false,
payload: "An error occurred while creating a cocktail"
});
}
});
//MVP 4 - create a patch listener that edits a cocktail
// create a variable that gets the updated cocktail from the request body
// use the edit cocktail function
// send new cocktail
app.patch("/cocktails/:id", async (req, res) => {
const { id } = req.params;
const { name, alcoholic, category, glassType, instructions, ingredients, ingredientMeasures } = req.body;
const newCocktail = await editCocktail(id, name, alcoholic, category, glassType, instructions, ingredients, ingredientMeasures);
res.json(newCocktail);
});
app.delete("/cocktails/:id", async (req, res) => {
const { id } = req.params;
const deletedCocktail = await deleteCocktail(id);
res.json(deletedCocktail);
});
app.get("/cocktails/filter", async (req, res) => {
const { key, value } = req.query;
try {
const cocktail = await getCocktailByKey(key, value);
if (cocktail && cocktail.length > 0) {
res.json({
success: true,
payload: cocktail,
});
} else {
res.status(404).json({
success: false,
payload: "No cocktails found matching the given filter",
});
}
} catch (error) {
console.error("Error!", error);
res.status(500).json({
success: false,
payload: "An error occurred while finding the cocktail"
});
}
});
app.get("/cocktails", async function (req, res) {
try {
const cocktails = await getCocktails();
if (cocktails) {
res.json({
success: true,
payload: cocktails,
});
} else {
res.status(404).json({
success: false,
payload: "Cocktails not found"
});
}
} catch (error) {
console.error("Error!", error);
res.status(500).json({
success: false,
payload: "An error occurred",
});
}
});
app.get("/cocktails/:id", async (req, res) => {
const { id } = req.params;
try {
const cocktail = await getCocktailById(id);
if (cocktail) {
res.json({
success: true,
payload: cocktail,
});
} else {
res.status(404).json({
success: false,
payload: "Cocktail not found",
});
}
} catch (error) {
console.error("Error!", error);
res.status(500).json({
success: false,
payload: "An error occurred while finding the cocktail"
});
}
});
app.listen(port, function () {
console.log(`Server is now listening on http://localhost:${port}`);
});
//This is where our requests and responses go