-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
135 lines (115 loc) · 3.69 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
const express = require('express');
const app = express();
app.use(express.json());
let ADMINS = [];
let USERS = [];
let COURSES = [];
const adminAuthentication = (req, res, next) => {
const { username, password } = req.headers;
const admin = ADMINS.find(a => a.username === username && a.password === password);
if (admin) {
next();
}
else {
res.status(403).json({ message: "Admin Authentication Failed!" });
}
};
const userAuthentication = (req, res, next) => {
const { username, password } = req.headers;
const user = USERS.find(u => u.username === username && u.password === password)
if (user) {
req.user = user;
next();
} else {
res.status(403).json({ message: "User Authetication Failed!" });
}
};
// Admin routes
app.post('/admin/signup', (req, res) => {
// logic to sign up admin
const admin = req.body;
const existingAdmin = ADMINS.find(a => a.username === admin.username);
if (existingAdmin) {
res.status(403).json({ message: "Admin already exists" });
} else {
ADMINS.push(admin);
res.json({ message: "Admin created successfully" });
}
});
app.post('/admin/login', (req, res) => {
// logic to log in admin
const admin = req.body;
const adminlog = ADMINS.find(a => a.username === admin.username && a.password === admin.password);
if (adminlog) {
res.json({ message: "Logged in successfully" });
} else {
res.status(404).json({ message: "Wrong Username or Password" })
}
});
app.post('/admin/courses', adminAuthentication, (req, res) => {
// logic to create a course
const course = req.body;
course.id = Date.now();
COURSES.push(course);
res.json({ message: "Course created successfully", courseId: course.id });
});
app.put('/admin/courses/:courseId', adminAuthentication, (req, res) => {
// logic to edit a course
const courseId = parseInt(req.params.courseId);
const course = COURSES.find(c => c.id === courseId);
if (course) {
Object.assign(course, req.body);
res.json({ message: "Course updated successfully" });
} else {
res.status(404).json({ message: "Course not found" });
}
});
app.get('/admin/courses', adminAuthentication, (req, res) => {
// logic to get all courses
res.json({ courses: COURSES });
});
// User routes
app.post('/users/signup', (req, res) => {
// logic to sign up user
const user = {
username: req.body.username,
password: req.body.password,
purchasedCourses: []
}
USERS.push(user);
res.json({ message: "User created successfully" });
});
app.post('/users/login', (req, res) => {
// logic to log in user
res.json({ message: "Logged in successfully" });
});
app.get('/users/courses', userAuthentication, (req, res) => {
// logic to list all courses
const filteredCourses = COURSES.filter(c => c.published);
res.json({ courses: filteredCourses });
});
app.post('/users/courses/:courseId', userAuthentication, (req, res) => {
// logic to purchase a course
const courseId = Number(req.params.courseId);
const course = COURSES.find(c => c.id === courseId && c.published);
if (course) {
req.user.purchasedCourses.push(courseId);
res.json({ message: 'Course purchased successfully' });
} else {
res.status(404).json({ message: 'Course not found or not available' });
}
});
app.get('/users/purchasedCourses', userAuthentication, (req, res) => {
// logic to view purchased courses
var purchasedCourseIds = req.user.purchasedCourses;[1, 4];
var purchasedCourses = [];
for (let i = 0; i < COURSES.length; i++) {
if (purchasedCourseIds.indexOf(COURSES[i].id) !== -1) {
purchasedCourses.push(COURSES[i]);
}
}
res.json({ purchasedCourses });
});
app.listen(3000, () => {
console.log('Server is listening on port 3000');
});