-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
312 lines (251 loc) · 8.28 KB
/
app.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const { Signup,Suggestion,Profile,Pickedtopic,Uploadvideo,Uploadcourse, connectToDatabase } = require('./dbschemas');
const bcrypt = require('bcrypt');
const SECRET_KEY = "NOTESAPI";
const jwt = require("jsonwebtoken");
const app = express();
const port = 3002;
connectToDatabase();
app.use(cors());
app.use(bodyParser.json());
// app.post('/signup', async (req, res)=>{
// const {name, email, password, role} = req.body;
// const saltRounds = 10;
// const hashedPassword = await bcrypt.hash(password, saltRounds);
// try{
// const user = new Signup({
// name:name,
// email: email,
// password: password,
// role:role,
// });
// const saveduser=await user.save();
// console.log(saveduser);
// }catch(e){
// console.error('Error creating user:'+ e);
// res.status(500).json({ e: 'An error occurred' });
// }
// })
// Authentication***************************************************************************************************************
app.post('/signup', async(req,res) => {
try{
const {password, email,name,role} = req.body;
//check if the user already exists in database
const userExist = await Signup.findOne({email : email});
if(userExist){
return res.status(400).json ("User with this email is already exist");
}
const hashedPassword = await bcrypt.hash(password,10);
const result = await Signup.create ({
name:name,
email:email,
password : hashedPassword,
role:role
});
const token =jwt.sign({email:result.email, role:result.role, id:result._id, name:result.name}, SECRET_KEY);
res.status(201).json({token:token , user: result}) ;
console.log(token);
}catch(e){
console.log(e);
res.status(500).json({err: "Internal Server Error"});
}
})
app.post('/login', async(req,res) => {
const {lemail, lpassword, lrole}= req.body;
try{
const userExist = await Signup.findOne({email : lemail});
if(!userExist){
return res.status(404).json ("User with this email is not exist");
}
const matchpass = await bcrypt.compare (lpassword, userExist.password);
if (!matchpass ) {
return res.status(400).json("Invalid Password");
}
if(userExist.role === lrole){
const token =jwt.sign({email:userExist.email, role:userExist.role, id:userExist._id, name:userExist.name}, SECRET_KEY);
res.status(201).json({token:token , user: userExist}) ;
}else{
return res.status(400).json("Invalid role");
}
}catch(e){
console.log(e);
res.status(500).json({err: "Internal Server Error"});
}
})
// Suggetions***************************************************************************************************************
app.post('/suggetion', async (req, res) => {
const { topicname, topicdes,suggetioncategory } = req.body;
try {
const suggestion = new Suggestion({
topicname: topicname,
topicdes: topicdes,
suggetioncategory: suggetioncategory,
});
const savedSuggestion = await suggestion.save();
console.log(savedSuggestion);
res.status(200).json(savedSuggestion);
} catch (error) {
console.error('Error creating suggestion:', error);
res.status(500).json({ error: 'An error occurred' });
}
});
app.get('/fetchsuggetion', async(req,res) => {
try{
const sug= await Suggestion.find();
res.json(sug);
}
catch(e){
console.log(e);
}
})
// app.post('/login', async (req, res) => {
// console.log(req.body.email)
// try {
// const check = await Signup.findOne({ email: req.body.email });
// if (check && check.password === req.body.password) {
// res.json({ success: true });
// } else {
// res.json({ success: false});
// }
// } catch (error) {
// console.error('An error occurred', error);
// res.status(500).json({ success: false, message: 'Internal Server Error' });
// }
// });
// Profile update***************************************************************************************************************
app.put('/profile', async (req, res) => {
try {
const { email, phone, role, name } = req.body;
// Check if the profile with the specified email exists
let updatedProfile;
const existingProfile = await Profile.findOne({ email });
if (existingProfile) {
// Update profile if it exists
updatedProfile = await Profile.findOneAndUpdate(
{ email},
{ $set: { phone, occupation: role } },
{ new: true }
);
} else {
// Create a new profile if it doesn't exist
updatedProfile = await new Profile({
email,
phone,
occupation: role,
// Include fullname if it's part of the schema
...(name && { username: name }),
}).save();
}
console.log(updatedProfile);
res.status(200).json(updatedProfile);
} catch (error) {
console.error('Error updating/creating profile:', error);
res.status(500).json({ error: 'An error occurred' });
}
});
app.get('/fetchprofile', async(req,res)=>{
try{
const prof = await Profile.find();
res.json(prof);
}catch(e){
console.log(e);
}
})
// picked topics***************************************************************************************************************
app.post('/picked', async(req,res)=>{
const {Educatoremail, topicname, topicdes, suggetioncategory} = req.body;
try{
const pick = new Pickedtopic({
topicname:topicname,
topicdes:topicdes,
suggetioncategory:suggetioncategory,
Educatoremail:Educatoremail,
picked:true,
})
const picked = await pick.save() ;
console.log(picked);
res.status(200).json(picked);
const deletedEntry = await Suggestion.findOneAndDelete({topicname,topicdes });
}catch(e){
console.error('Error picking topic:', e);
res.status(500).json({ error: 'An error occurred' });
}
})
app.get('/fetchpicked', async(req,res)=>{
try{
const pick = await Pickedtopic.find();
res.json(pick);
}catch(e){
console.log(e);
}
})
// Videoupload************************************************************************************************************
app.post('/videoupload', async (req, res) => {
const { secure_url,email, topicname, topicdes, topictime,videocategory } = req.body;
// console.log(topictime);
// console.log(secure_url);
try {
const video = new Uploadvideo({
secure_url:secure_url,
videoemail:email,
topicname:topicname,
topicdes:topicdes,
topictime:topictime,
videocategory:videocategory,
});
const savedVideo = await video.save();
console.error(savedVideo);
res.status(201).json({ message: 'Video uploaded successfully' });
} catch (error) {
console.error('Error uploading video:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.get('/fetchupload',async(req, res) => {
try{
const video = await Uploadvideo.find();
res.json(video);
}catch(e){
console.log(e);
}
})
// courseupload************************************************************************************************************
app.post('/courseupload', async (req,res)=> {
const { secure_url,thumbnail_url,email, coursetitle, coursedes, coursecategory,coursevideocount, courseduration, courseprice,courselevel } = req.body;
try{
const course = new Uploadcourse({
secure_url:secure_url,
thumbnail_url:thumbnail_url,
courseemail:email,
coursetitle:coursetitle,
coursedes:coursedes,
coursecategory:coursecategory,
coursevideocount:coursevideocount,
courseduration:courseduration,
courseprice:courseprice,
courselevel:courselevel,
})
const savecourse = await course.save();
console.error(savecourse);
res.status(201).json({ message: 'Course uploaded successfully' });
}catch(error){
console.error('Error uploading video:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
})
app.get('/fetchcourses',async(req, res) => {
try{
const course = await Uploadcourse.find();
res.json(course);
}catch(e){
console.log(e);
}
})
app.get('/', (req, res) => {
res.send('Welcome to my server!');
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});