Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

done course-app-easy #274

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions week-3/02-course-app-easy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
98 changes: 80 additions & 18 deletions week-3/02-course-app-easy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,108 @@ let ADMINS = [];
let USERS = [];
let COURSES = [];

const adminAuthentication = (req,res,next)=>{
//check if admin exist in admin array if yes call next
const admin = req.headers;
const validAdmin = ADMINS.find(a=>a.username===admin.username
&&a.password === admin.password );
if(validAdmin){
next();
}else{
res.status(403).json({message:'Admin authentication failed'})
}
}
const userAuthentication = (req,res,next)=>{
//check if admin exist in admin array if yes call next
const user = req.headers;
const validUser = USERS.find(u=>u.username===user.username
&&u.password === user.password );
if(validUser){
// adding user object to req body because a user has purchased course in USERS array
req.user = validUser;
next();
}else{
res.status(403).json({message:'User Authentication 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
&& a.password ===admin.password );
if(existingAdmin)
res.status(404).json({message: 'Admin already exist'});
else{
ADMINS.push(admin);
res.json({message: 'Admin created successfully'})
}
});

app.post('/admin/login', (req, res) => {
// logic to log in admin
app.post('/admin/login',adminAuthentication, (req, res) => {
console.log(req.headers);
res.json({message: 'Logged in successfully'});
});

app.post('/admin/courses', (req, res) => {
// logic to create a course
app.post('/admin/courses',adminAuthentication, (req, res) => {
const course = req.body;
course.id = Date.now();
COURSES.push(course);
res.json({ message: 'Course created successfully', courseId: 1 })
});

app.put('/admin/courses/:courseId', (req, res) => {
// logic to edit a course
app.put('/admin/courses/:courseId',adminAuthentication, (req, res) => {
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(403).json({message:'Course not found'});
}
});

app.get('/admin/courses', (req, res) => {
// logic to get all courses
app.get('/admin/courses',adminAuthentication, (req, res) => {
res.json({courses: COURSES});
});

// User routes
app.post('/users/signup', (req, res) => {
// logic to sign up user
const user = {...req.body,purchasedCourse: []};
const existingUser = USERS.find(u=>u.username === user.username
&& u.password ===user.password );
if(existingUser)
res.status(404).json({message: 'User already exist'});
else{
USERS.push(user);
res.json({message: 'User created successfully'})
}
});

app.post('/users/login', (req, res) => {
// logic to log in user
app.post('/users/login',userAuthentication, (req, res) => {
res.json({ message: 'Logged in successfully' });
});

app.get('/users/courses', (req, res) => {
// logic to list all courses
app.get('/users/courses',userAuthentication, (req, res) => {
let filteredCourses = COURSES.filter(c => c.published) ;
res.json({course: filteredCourses});
});

app.post('/users/courses/:courseId', (req, res) => {
// logic to purchase a course
app.post('/users/courses/:courseId',userAuthentication, (req, res) => {
const courseId = parseInt(req.params.courseId);
const course = COURSES.find(c=>c.id === courseId &&c.published);
if(course){
req.user.purchasedCourse.push(course);
res.json({message:'Course purchased successfully'})
}else{
res.status(404).json({ message: 'Course not found or not available' });
}
});

app.get('/users/purchasedCourses', (req, res) => {
// logic to view purchased courses
app.get('/users/purchasedCourses',userAuthentication, (req, res) => {
// const user = USERS.find(u=>u.username === req.user.username);
// res.json({purchasedCourses:req.user.purchasedCourse});
const purchasedCourses = COURSES.filter(c=>req.user.purchasedCourses.includes(c.id));
res.json({ purchasedCourses });
});

app.listen(3000, () => {
Expand Down
Loading