-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (112 loc) · 3.29 KB
/
index.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
135
136
137
138
139
140
141
142
const express = require("express");
const { PORT } = require("./config/constants");
const loggerMiddleWare = require("morgan");
const authRouter = require("./routers/auth");
const authMiddleWare = require("./auth/middleware");
const Users = require("./models").user;
const Recipes = require("./models").recipe;
const { toData } = require("./auth/jwt");
const app = new express();
app.use(loggerMiddleWare("dev"));
const bodyParserMiddleWare = express.json();
app.use(bodyParserMiddleWare);
const corsMiddleWare = require("cors");
app.use(corsMiddleWare());
app.use("/", authRouter);
app.get("/", (req, res) => {
res.send("Hi from express");
});
// ./tests TEST
app.post("/authorized_post_request", authMiddleWare, (req, res) => {
const user = req.user;
delete user.dataValues["password"];
res.json({
youPosted: {
...req.body,
},
userFoundWithToken: {
...user.dataValues,
},
});
});
// GET all recipes of a user
app.get("/getrecipes/:id", async (req, res) => {
try {
const { id } = req.params;
const findUser = await Users.findByPk(id);
const userName = findUser.name;
const userPic = findUser.profileUrl;
const recipes = await Recipes.findAll({ where: { userId: findUser.id } });
return res.status(200).send({ message: "ok", recipes, userName, userPic });
} catch (error) {
console.log(error.message);
}
}),
// GET all user profiles
app.get("/profiles", async (req, res) => {
try {
const profiles = await Users.findAll({
attributes: ["name", "profileUrl", "id"],
});
return res.status(200).send({ message: "ok", profiles });
} catch (error) {
console.log(error.message);
}
});
// ADD a recipe to profile
app.post("/addrecipe", async (req, res) => {
try {
const { userId, recipeId, recipeName, recipePic } = req.body;
console.log("body content add rec", userId, recipeId);
addRecipe = await Recipes.create({
userId: userId,
recipeId: recipeId,
recipeName: recipeName,
recipePic: recipePic,
});
return res.status(201).send({ message: "Recipe added!", addRecipe });
} catch (error) {
console.log(error.message);
}
});
// DELETE a recipe from profile
app.delete("/deleterecipe/:recipe", authMiddleWare, async (req, res) => {
try {
const { recipe } = req.params;
const user = req.user;
console.log("USER: ", user);
const userId = req.user.id;
console.log("userID", userId);
console.log("recipe id:", recipe);
// console.log("recipeId to del", userId);
// parseInt
const recipeToDelete = await Recipes.findOne({
where: {
userId: userId,
recipeId: recipe,
},
});
console.log("recipe to delete", recipeToDelete);
if (!recipeToDelete) {
console.log("loading..");
} else {
await recipeToDelete.destroy();
}
res.status(200).send({ message: "recipe deleted" });
} catch (error) {
console.log(error.message);
}
});
app.patch("/ratings", authMiddleWare, async (req, res) => {
try {
const { id, stars } = req.body;
const recipe = await Recipes.findByPk(id);
await recipe.update({ stars });
return res.send(recipe);
} catch (error) {
console.log(error.message);
}
});
app.listen(PORT, () => {
console.log(`Listening on port: ${PORT}`);
});