-
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
c253d8c
commit 6f10433
Showing
27 changed files
with
1,136 additions
and
765 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,6 @@ | ||
PORT= | ||
MONGO_DB_URL= | ||
# Cloudinary Keys | ||
CLOUD_NAME= | ||
API_KEY= | ||
API_SECRET= |
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,10 @@ | ||
const cloudinary = require('cloudinary').v2 | ||
require("dotenv").config() | ||
|
||
cloudinary.config({ | ||
cloud_name: process.env.CLOUD_NAME, | ||
api_key: process.env.API_KEY, | ||
api_secret: process.env.API_SECRET, | ||
}); | ||
|
||
module.exports = cloudinary; |
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,30 @@ | ||
const passport = require("passport"); | ||
const JWTStrategy = require("passport-jwt").Strategy; | ||
const ExtractJWT = require("passport-jwt").ExtractJwt; | ||
require('dotenv').config() | ||
|
||
const User = require("../models/User"); | ||
|
||
let opts = { | ||
jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken(), | ||
secretOrKey: process.env.SECRET_KEY, | ||
}; | ||
|
||
passport.use( | ||
new JWTStrategy(opts, async (jwtPayLoad, done) => { | ||
console.log("JWT Payload ID:", jwtPayLoad._id); | ||
try { | ||
const user = await User.findById(jwtPayLoad._id); | ||
|
||
if (user) { | ||
return done(null, user); | ||
} else { | ||
return done(null, false); | ||
} | ||
} catch (error) { | ||
return done(error); | ||
} | ||
}) | ||
); | ||
|
||
module.exports = passport; |
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,98 @@ | ||
const { default: mongoose } = require("mongoose"); | ||
const ClassModel = require("../../models/Class"); | ||
|
||
module.exports.createClass = async (req, res) => { | ||
const { name, teacherId } = req.body; | ||
if (!name || !teacherId) { | ||
return res.status(400).json({ message: "All fields are required." }); | ||
} | ||
try { | ||
const classroom = await ClassModel.create(req.body); | ||
res.status(200).json({ message: "Classroom is created.", classroom }); | ||
} catch (error) { | ||
res.status(400).json({ error: error.message }); | ||
} | ||
}; | ||
|
||
module.exports.allClasses = async (req, res) => { | ||
const page = req.query.page || 1; | ||
const limit = req.query.limit || 10; | ||
|
||
const skip = (page - 1) * limit; | ||
try { | ||
const classrooms = await ClassModel.find({}).skip(skip).limit(limit); | ||
res.status(200).json({ page: page, limit: limit, classrooms: classrooms }); | ||
} catch (error) { | ||
res.status(400).json({ error: error.message }); | ||
} | ||
}; | ||
|
||
module.exports.updateClass = async (req, res) => { | ||
const { id } = req.params; | ||
if (!mongoose.Types.ObjectId.isValid(id)) { | ||
return res.status(400).json("No such classroom exists."); | ||
} | ||
try { | ||
const classroom = await ClassModel.findByIdAndUpdate( | ||
{ _id: id }, | ||
{ ...req.body } | ||
); | ||
res | ||
.status(200) | ||
.json({ message: "Classroom is updated.", classroom: classroom }); | ||
} catch (error) { | ||
res.status(400).json({ error: error.message }); | ||
} | ||
}; | ||
|
||
module.exports.assignTeacher = async (req, res) => { | ||
const { teacherId, classId } = req.body; | ||
if ( | ||
!mongoose.Types.ObjectId.isValid(teacherId) || | ||
!mongoose.Types.ObjectId.isValid(classId) | ||
) { | ||
return res.status(400).json("No such teacher exists."); | ||
} | ||
try { | ||
const classroom = await ClassModel.findByIdAndUpdate( | ||
classId, | ||
{ teacherId: teacherId }, | ||
{ new: true } | ||
); | ||
res.status(200).json({ message: "Teacher is Assigned.", classroom }); | ||
} catch (error) { | ||
res.status(400).json({ error: error.message }); | ||
} | ||
}; | ||
|
||
module.exports.deleteClass = async (req, res) => { | ||
const { id } = req.params; | ||
if (!mongoose.Types.ObjectId.isValid(id)) { | ||
return res.status(400).json("No such classroom exists."); | ||
} | ||
try { | ||
const classroom = await ClassModel.findByIdAndDelete(id); | ||
res | ||
.status(200) | ||
.json({ message: "Classroom is deleted.", classroom: classroom }); | ||
} catch (error) { | ||
res.status(400).json({ error: error.message }); | ||
} | ||
}; | ||
|
||
module.exports.report = async (req, res) => { | ||
const { id } = req.params; | ||
if (!mongoose.Types.ObjectId.isValid(id)) { | ||
return res.status(400).json({ message: "Id is invalid." }); | ||
} | ||
|
||
try { | ||
const classroom = await ClassModel.findById(id).populate({ | ||
path: "attendees.studentId", | ||
select: "name", | ||
}); | ||
res.status(200).json({message:"Classroom Report.", classroom}); | ||
} catch (error) { | ||
res.status(400).json({ error: error.message }); | ||
} | ||
}; |
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,35 @@ | ||
const mongoose = require("mongoose"); | ||
const examModel = require("../../models/Exam"); | ||
|
||
module.exports.createExam = async (req, res) => { | ||
const { name, subject, totalMarks } = req.body; | ||
if (!name || !subject || !totalMarks) { | ||
return res.status(400).json({ message: "All Fields are required." }); | ||
} | ||
|
||
try { | ||
const exam = await examModel.create(req.body); | ||
res.status(200).json({ message: "Exam is created", exam }); | ||
} catch (error) { | ||
res.status(400).json({ error: error.message }); | ||
} | ||
}; | ||
|
||
module.exports.updateExam = async (req, res) => { | ||
const { name, subject, totalMarks } = req.body; | ||
if (!name || !subject || !totalMarks) { | ||
return res.status(400).json({ message: "All Fields are required." }); | ||
} | ||
|
||
const { id } = req.params; | ||
if(!mongoose.Types.ObjectId.isValid(id)){ | ||
return res.status(400).json({message:"The exam with this id does not exists."}) | ||
} | ||
|
||
try { | ||
const exam = await examModel.findByIdAndUpdate(id,{...req.body}); | ||
res.status(200).json({ message: "Exam is updated", exam }); | ||
} catch (error) { | ||
res.status(400).json({ error: error.message }); | ||
} | ||
}; |
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,60 @@ | ||
const mongoose = require("mongoose"); | ||
const resultModel = require("../../models/Result"); | ||
|
||
module.exports.addResult = async (req, res) => { | ||
const { studentId, examId, marksObtained } = req.body; | ||
if (!studentId || !examId || !marksObtained) { | ||
return res.status(400).json({ message: "All Fields are required." }); | ||
} | ||
|
||
if ( | ||
!mongoose.Types.ObjectId.isValid(studentId) || | ||
!mongoose.Types.ObjectId.isValid(examId) | ||
) { | ||
return res.status(400).json({ message: "Invalid Id's." }); | ||
} | ||
|
||
try { | ||
const result = await resultModel.create(req.body); | ||
res.status(200).json({ message: "Result is added.", result }); | ||
} catch (error) { | ||
res.status(400).json({ error: error.message }); | ||
} | ||
res.json(400, { message: "All hail tejas" }); | ||
}; | ||
|
||
module.exports.examResult = async (req, res) => { | ||
const { id } = req.params; | ||
if (!mongoose.Types.ObjectId.isValid(id)) { | ||
return res.status(400).json({ message: "Invalid Id" }); | ||
} | ||
|
||
try { | ||
const results = await resultModel.find({ examId: id }); | ||
res | ||
.status(200) | ||
.json({ | ||
message: "Results of all students for a specific exam.", | ||
results, | ||
}); | ||
} catch (error) { | ||
res.status(400).json({ error: error.message }); | ||
} | ||
}; | ||
|
||
module.exports.studentResult = async (req, res) => { | ||
const { id } = req.params; | ||
if (!mongoose.Types.ObjectId.isValid(id)) { | ||
return res.status(400).json({ message: "Invalid Id" }); | ||
} | ||
|
||
try { | ||
const results = await resultModel.find({ studentId: id }); | ||
res.status(200).json({ | ||
message: "Results of all exams for a specific student.", | ||
results, | ||
}); | ||
} catch (error) { | ||
res.status(400).json({ error: error.message }); | ||
} | ||
}; |
Oops, something went wrong.