-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b43bff7
commit 3755c57
Showing
46 changed files
with
12,177 additions
and
0 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,81 @@ | ||
const express = require("express"); | ||
const Admin = require("../Models/AdminModel"); | ||
const Course = require("../Models/CourseModel"); | ||
const jwt = require("jsonwebtoken"); | ||
|
||
const AdminSecret = "hfaihi23hahk"; | ||
// MIDDLEWARES | ||
exports.authenticateAdmin = (req, res, next) => { | ||
const authHeader = req.headers.authorization; | ||
if (authHeader) { | ||
const token = authHeader.split(" ")[1]; | ||
jwt.verify(token, AdminSecret, (err, user) => { | ||
if (err) { | ||
return res.sendStatus(403); | ||
} | ||
req.user = user; | ||
next(); | ||
}); | ||
} else { | ||
res.sendStatus(401); | ||
} | ||
}; | ||
|
||
exports.signupAdmin = async (req, res) => { | ||
const { username, password } = req.body; | ||
const exists = await Admin.findOne({ username }); | ||
|
||
if (exists) { | ||
res.status(403).json({ message: "Admin already exists" }); | ||
} else { | ||
const newAdmin = new Admin({ username, password }); | ||
newAdmin.save(); | ||
|
||
const token = jwt.sign({ username, role: "admin" }, AdminSecret, { | ||
expiresIn: "1h", | ||
}); | ||
res.json({ message: "Admin created", token: token, username: username }); | ||
} | ||
}; | ||
exports.loginAdmin = async (req, res) => { | ||
// logic to log in admin | ||
const { username, password } = req.headers; | ||
const admin = await Admin.findOne({ username, password }); | ||
console.log(admin); | ||
if (admin) { | ||
const token = jwt.sign({ username, role: "admin" }, AdminSecret, { | ||
expiresIn: "1h", | ||
}); | ||
res.json({ | ||
message: "Logged in successfully", | ||
token: token, | ||
username: username, | ||
}); | ||
} else { | ||
res.status(403).json({ message: "invaild username or password" }); | ||
} | ||
}; | ||
exports.createCourse = async (req, res) => { | ||
console.log(req.file); | ||
const course = new Course(req.body); | ||
await course.save(); | ||
res.json({ message: "Course created successfully", courseId: course.id }); | ||
}; | ||
exports.updateCourse = async (req, res) => { | ||
const course = await Course.findByIdAndUpdate(req.params.courseId, req.body, { | ||
new: true, | ||
}); | ||
if (course) { | ||
res.json({ message: "Course updated successfully" }); | ||
} else { | ||
res.status(404).json({ message: "Course not found" }); | ||
} | ||
}; | ||
exports.getAllCourse = async (req, res) => { | ||
const courses = await Course.find({}); | ||
res.json({ courses }); | ||
}; | ||
|
||
exports.isLogin = (req, res) => { | ||
res.status(200).json({ message: "Logged in" }); | ||
}; |
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,94 @@ | ||
const express = require("express"); | ||
const User = require("../Models/UserModel"); | ||
const Course = require("../Models/CourseModel"); | ||
const jwt = require("jsonwebtoken"); | ||
|
||
const UserSecret = "hguehgiw223j"; | ||
// MIDDLEWARE | ||
exports.authenticateUser = (req, res, next) => { | ||
const authHeader = req.headers.authorization; | ||
if (authHeader) { | ||
const token = authHeader.split(" ")[1]; | ||
jwt.verify(token, UserSecret, (err, user) => { | ||
if (err) { | ||
return res.sendStatus(403); | ||
} | ||
req.user = user; | ||
next(); | ||
}); | ||
} else { | ||
res.sendStatus(401); | ||
} | ||
}; | ||
|
||
exports.signupUser = async (req, res) => { | ||
const { username, password } = req.body; | ||
try { | ||
const user = await User.findOne({ username }); | ||
if (user) { | ||
return res.status(403).json({ message: "User already exists" }); | ||
} else { | ||
const newUser = new User({ username, password }); | ||
await newUser.save(); | ||
const token = jwt.sign({ username, role: "user" }, UserSecret, { | ||
expiresIn: "1h", | ||
}); | ||
return res.json({ message: "User created successfully", token }); | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
return res.status(500).json({ message: "Internal server error" }); | ||
} | ||
}; | ||
|
||
exports.loginUser = async (req, res) => { | ||
const { username, password } = req.body; | ||
const user = await User.findOne({ username, password }); | ||
if (user) { | ||
const token = jwt.sign({ username, role: "user" }, UserSecret, { | ||
expiresIn: "1h", | ||
}); | ||
res.json({ message: "Logged in successfully", token }); | ||
} else { | ||
res.status(403).json({ message: "Invalid username or password" }); | ||
} | ||
}; | ||
|
||
exports.purchaseCourse = async (req, res) => { | ||
const course = await Course.findById(req.params.courseId); | ||
console.log(course); | ||
if (course) { | ||
const user = await User.findOne({ username: req.user.username }); | ||
if (user) { | ||
user.purchasedCourses.push(course); | ||
await user.save(); | ||
res.json({ message: "Course purchased successfully" }); | ||
} else { | ||
res.status(403).json({ message: "User not found" }); | ||
} | ||
} else { | ||
res.status(404).json({ message: "Course not found" }); | ||
} | ||
}; | ||
|
||
exports.getCourses = async (req, res) => { | ||
const courses = await Course.find({ published: true }); | ||
res.json({ courses }); | ||
}; | ||
|
||
exports.getPurchasedCourses = async (req, res) => { | ||
const user = await User.findOne({ username: req.user.username }).populate( | ||
"purchasedCourses" | ||
); | ||
if (user) { | ||
res.json({ purchasedCourses: user.purchasedCourses || [] }); | ||
} else { | ||
res.status(403).json({ message: "User not found" }); | ||
} | ||
}; | ||
|
||
exports.isLogin = (req, res) => { | ||
res.status(200).json({ | ||
message: "Logged in", | ||
}); | ||
}; |
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,11 @@ | ||
const { mongoose } = require("mongoose"); | ||
|
||
const adminSchema = new mongoose.Schema({ | ||
username: String, | ||
password: String, | ||
}); | ||
|
||
// If the Admin model already exists, use it; otherwise, define the model | ||
const Admin = mongoose.model("Admin", adminSchema); | ||
|
||
module.exports = Admin; |
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,13 @@ | ||
const { mongoose } = require("mongoose"); | ||
|
||
const courseSchema = new mongoose.Schema({ | ||
title: String, | ||
description: String, | ||
price: Number, | ||
imageLink: String, | ||
published: Boolean, | ||
}); | ||
|
||
const Course = mongoose.model("Course", courseSchema); | ||
|
||
module.exports = Course; |
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,11 @@ | ||
const { mongoose } = require("mongoose"); | ||
const Course = require("./CourseModel"); | ||
|
||
const userSchema = new mongoose.Schema({ | ||
username: String, | ||
password: String, | ||
purchasedCourses: [{ type: mongoose.Schema.Types.ObjectId, ref: "Course" }], | ||
}); | ||
const User = mongoose.model("User", userSchema); | ||
|
||
module.exports = User; |
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,36 @@ | ||
const express = require("express"); | ||
const app = express(); | ||
const cors = require("cors"); | ||
const { mongoose } = require("mongoose"); | ||
const multer = require("multer"); | ||
const dotenv = require("dotenv"); | ||
|
||
const UserRoute = require("./routes/UserRoutes"); | ||
const AdminRoute = require("./routes/AdminRoutes"); | ||
dotenv.config(); | ||
|
||
app.use(express.json()); | ||
app.use(cors()); | ||
|
||
// const Database = process.env.DATABASE_URI; | ||
const Database = 'mongodb://localhost:27017'; | ||
|
||
mongoose | ||
.connect(Database, { useNewUrlParser: true, useUnifiedTopology: true }) | ||
.then(() => console.log("Database connected")) | ||
.catch((err) => console.log("not connected", err)); | ||
|
||
app.use("/admin", AdminRoute); | ||
app.use("/users", UserRoute); | ||
|
||
app.all("*", (req, res) => { | ||
res.status(404).json({ | ||
status: "fail", | ||
message: `Can't find ${req.originalUrl} on this server! `, | ||
}); | ||
}); | ||
|
||
const port = 3000; | ||
app.listen(port, () => { | ||
console.log(`Server is listening on port ${port}`); | ||
}); |
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,11 @@ | ||
const multer = require("multer"); | ||
const storage = multer.diskStorage({}); | ||
|
||
const fileFilter = (req, file, cb) => { | ||
if (!file.mimetype.startsWith("image")) { | ||
cb("Supported only Images!", false); | ||
} | ||
console.log(file); | ||
cb(null, true); | ||
}; | ||
exports.uploadImage = multer({ storage, fileFilter }); |
Oops, something went wrong.