-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/idrag0/assignments
- Loading branch information
Showing
12 changed files
with
403 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
// Connect to MongoDB | ||
mongoose.connect('mongodb+srv://kirags123:[email protected]/course_selling_app'); | ||
|
||
// Define schemas | ||
const AdminSchema = new mongoose.Schema({ | ||
// Schema definition here | ||
username: String, | ||
password: String | ||
}); | ||
|
||
const UserSchema = new mongoose.Schema({ | ||
// Schema definition here | ||
username: String, | ||
password: String, | ||
purchasedCourses: [{ | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'Course' | ||
}] | ||
}); | ||
|
||
const CourseSchema = new mongoose.Schema({ | ||
// Schema definition here | ||
title: String, | ||
description: String, | ||
imageLink: String, | ||
price: Number | ||
}); | ||
|
||
const Admin = mongoose.model('Admin', AdminSchema); | ||
const User = mongoose.model('User', UserSchema); | ||
const Course = mongoose.model('Course', CourseSchema); | ||
|
||
module.exports = { | ||
Admin, | ||
User, | ||
Course | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const { Admin } = require("../db"); | ||
|
||
// Middleware for handling auth | ||
function adminMiddleware(req, res, next) { | ||
// Implement admin auth logic | ||
// You need to check the headers and validate the admin from the admin DB. Check readme for the exact headers to be expected | ||
const username = req.headers.username; // harkirat@gmail.com | ||
const password = req.headers.password; /// 123456 | ||
|
||
Admin.findOne({ | ||
username: username, | ||
password: password | ||
}) | ||
.then(function(value) { | ||
if (value) { | ||
next(); | ||
} else { | ||
res.status(403).json({ | ||
msg: "Admin doesnt exist" | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
module.exports = adminMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const { User } = require("../db"); | ||
|
||
function userMiddleware(req, res, next) { | ||
// Implement admin auth logic | ||
// You need to check the headers and validate the admin from the admin DB. Check readme for the exact headers to be expected | ||
const username = req.headers.username; // harkirat@gmail.com | ||
const password = req.headers.password; /// 123456 | ||
|
||
User.findOne({ | ||
username: username, | ||
password: password | ||
}) | ||
.then(function(value) { | ||
if (value) { | ||
next(); | ||
} else { | ||
res.status(403).json({ | ||
msg: "User doesnt exist" | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
module.exports = userMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const express = require("express"); | ||
const adminMiddleware = require("../middleware/admin"); | ||
const { Admin, Course } = require("../db"); | ||
const router = express.Router(); | ||
|
||
router.post('/signup', async (req, res) => { | ||
// Implement admin signup logic | ||
const username = req.body.username; | ||
const password = req.body.password; | ||
|
||
// check if a user with this username already exists | ||
await Admin.create({ | ||
username: username, | ||
password: password | ||
}) | ||
|
||
res.json({ | ||
message: 'Admin created successfully' | ||
}) | ||
|
||
}); | ||
|
||
router.post('/courses', adminMiddleware, async (req, res) => { | ||
// Implement course creation logic | ||
const title = req.body.title; | ||
const description = req.body.description; | ||
const imageLink = req.body.imageLink; | ||
const price = req.body.price; | ||
// zod | ||
const newCourse = await Course.create({ | ||
title, | ||
description, | ||
imageLink, | ||
price | ||
}) | ||
|
||
res.json({ | ||
message: 'Course created successfully', courseId: newCourse._id | ||
}) | ||
}); | ||
|
||
router.get('/courses', adminMiddleware, async (req, res) => { | ||
// Implement fetching all courses logic | ||
const response = await Course.find({}); | ||
|
||
res.json({ | ||
courses: response | ||
}) | ||
|
||
}); | ||
|
||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const { Router } = require("express"); | ||
const router = Router(); | ||
const userMiddleware = require("../middleware/user"); | ||
const { User, Course } = require("../db"); | ||
const { default: mongoose } = require("mongoose"); | ||
|
||
// User Routes | ||
router.post('/signup', (req, res) => { | ||
// Implement user signup logic | ||
const username = req.body.username; | ||
const password = req.body.password; | ||
User.create({ | ||
username, | ||
password | ||
}) | ||
res.json({ | ||
message: "User created successfully" | ||
}) | ||
}); | ||
|
||
router.get('/courses', async (req, res) => { | ||
// Implement listing all courses logic | ||
// Implement fetching all courses logic | ||
const response = await Course.find({}); | ||
|
||
res.json({ | ||
courses: response | ||
}) | ||
}); | ||
|
||
router.post('/courses/:courseId', userMiddleware, async(req, res) => { | ||
// Implement course purchase logic | ||
const courseId = req.params.courseId; | ||
const username = req.headers.username; | ||
|
||
await User.updateOne({ | ||
username: username | ||
}, { | ||
"$push": { | ||
purchasedCourses: courseId | ||
} | ||
}) | ||
res.json({ | ||
message: "Purchase complete!" | ||
}) | ||
}); | ||
|
||
router.get('/purchasedCourses', userMiddleware, async (req, res) => { | ||
// Implement fetching purchased courses logic | ||
const user = await User.findOne({ | ||
username: req.headers.username | ||
}); | ||
|
||
console.log(user.purchasedCourses); | ||
const courses = await Course.find({ | ||
_id: { | ||
"$in": user.purchasedCourses | ||
} | ||
}); | ||
|
||
res.json({ | ||
courses: courses | ||
}) | ||
}); | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
// Connect to MongoDB | ||
mongoose.connect('mongodb+srv://kirags123:[email protected]/course_selling_app2'); | ||
|
||
// Define schemas | ||
const AdminSchema = new mongoose.Schema({ | ||
// Schema definition here | ||
username: String, | ||
password: String | ||
}); | ||
|
||
const UserSchema = new mongoose.Schema({ | ||
// Schema definition here | ||
username: String, | ||
password: String, | ||
purchasedCourses: [{ | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'Course' | ||
}] | ||
}); | ||
|
||
const CourseSchema = new mongoose.Schema({ | ||
// Schema definition here | ||
title: String, | ||
description: String, | ||
imageLink: String, | ||
price: Number | ||
}); | ||
|
||
const Admin = mongoose.model('Admin', AdminSchema); | ||
const User = mongoose.model('User', UserSchema); | ||
const Course = mongoose.model('Course', CourseSchema); | ||
|
||
module.exports = { | ||
Admin, | ||
User, | ||
Course | ||
} |
Oops, something went wrong.