-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
217 lines (192 loc) · 5.96 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const express = require("express");
const formidable = require("express-formidable");
const mongoose = require("mongoose");
const axios = require("axios");
const cors = require("cors");
const dotenv = require("dotenv");
const SHA256 = require("crypto-js/sha256");
const encBase64 = require("crypto-js/enc-base64");
const uid2 = require("uid2");
const User = require("./models/User");
const app = express();
const corsOptions = {
origin: "*",
credentials: true,
optionSuccessStatus: 200,
};
app.use(cors(corsOptions));
app.use(formidable());
app.use(cors(corsOptions));
dotenv.config();
mongoose.connect(process.env.DB_CONNECTION);
app.get("/", async (req, res) => {
try {
res.json({ message: "Hello to marvel comics API" });
} catch (error) {
console.log(error);
}
});
//API MARVEL
app.get("/comics", async (req, res) => {
try {
const title = req.query.title ? req.query.title : "";
const skip = req.query.skip ? req.query.skip : 0;
const limit = req.query.limit ? req.query.limit : 100;
const response = await axios.get(
`https://lereacteur-marvel-api.herokuapp.com/comics?apiKey=${process.env.API_KEY}&title=${title}&skip=${skip}&limit=${limit}`
);
res.json(response.data);
} catch (error) {
console.log(error);
}
});
app.get("/characters", async (req, res) => {
try {
const name = req.query.name ? req.query.name : "";
const skip = req.query.skip ? req.query.skip : 0;
const limit = req.query.limit ? req.query.limit : 100;
const response = await axios.get(
`https://lereacteur-marvel-api.herokuapp.com/characters?apiKey=${process.env.API_KEY}&name=${name}&skip=${skip}&limit=${limit}`
);
res.json(response.data);
} catch (error) {
console.log(error);
}
});
//USERS ROUTES
app.get("/comics/:characterId", async (req, res) => {
try {
const characterId = req.params.characterId;
const skip = req.query.skip ? req.query.skip : 0;
const response = await axios.get(
`https://lereacteur-marvel-api.herokuapp.com/comics/${characterId}?apiKey=${process.env.API_KEY}`
);
res.json(response.data);
} catch (error) {
console.log(error);
}
});
app.post("/register", async (req, res) => {
try {
const { username, email, password } = req.fields;
const findEmail = await User.findOne({ email: email }); //const mane = modelName.find()
if (findEmail === null) {
const salt = uid2(16);
const hash = SHA256(password + salt).toString(encBase64);
const token = uid2(16);
const newUser = new User({
account: {
username: username,
},
email: email,
salt: salt,
hash: hash,
token: token,
});
await newUser.save();
console.log("newUser", newUser);
res.json({ token: newUser.token, favorites: newUser.favorites });
}
} catch (error) {
console.log(error);
res.json({ error: error.message });
}
});
app.post("/login", async (req, res) => {
try {
const userData = await User.findOne({ email: req.fields.email });
console.log("userData", userData);
if (userData.email === undefined) {
throw "Information not valid";
} else {
const password = req.fields.password;
const salt = userData.salt;
const hash = SHA256(password + salt).toString(encBase64);
if (hash === userData.hash) {
res.json({ token: userData.token, favorites: userData.favorites });
} else {
throw "Information not valid";
}
}
} catch (error) {
res.status(401).json({ error: error });
}
});
// ROUTES FAVORITES
app.put("/addFavorit", async (req, res) => {
try {
let message = "";
console.log("req.fields", req.fields);
const user = await User.findOne({ token: req.fields.token });
console.log("user", user);
const comicsUserFavorits = user.favorites.comics;
// console.log("comicsUserFavorits", comicsUserFavorits);
const charactersUserFavorits = user.favorites.characters;
// console.log("charactersUserFavorits", charactersUserFavorits);
if (req.fields.categories === "characters") {
if (charactersUserFavorits?.length === 0) {
console.log("characters user empty");
await User.findByIdAndUpdate(user._id, {
favorites: {
comics: comicsUserFavorits,
characters: req.fields.data,
},
});
message = "added to favorits";
} else {
charactersUserFavorits.push(req.fields.data);
await User.findByIdAndUpdate(user._id, {
favorites: {
comics: comicsUserFavorits,
characters: charactersUserFavorits,
},
});
message = "add to favorits";
}
} else if (req.fields.categories === "comics") {
if (comicsUserFavorits.length === 0) {
console.log("characters user empty");
await User.findByIdAndUpdate(user._id, {
favorites: {
comics: req.fields.data,
characters: charactersUserFavorits,
},
});
message = "added to favorits";
} else {
comicsUserFavorits.push(req.fields.data);
await User.findByIdAndUpdate(user._id, {
favorites: {
comics: comicsUserFavorits,
characters: charactersUserFavorits,
},
});
message = "add to favorits";
}
}
console.log("user", user);
/* Returning the comics array to the frontend. */
res.json(user.favorites);
} catch (error) {
console.log(error);
}
});
app.get("/favorites", async (req, res) => {
try {
const token = req.headers.authorization.replace("Bearer ", "");
const user = await User.findOne({ token: token });
if (
user.favorites.comics.length === 0 &&
user.favorites.characters.length === 0
) {
throw "No favorites";
}
console.log("user", user);
res.json(user.favorites);
} catch (error) {
res.json({ error: error });
}
});
app.listen(process.env.PORT, () => {
console.log("Server has started");
});