diff --git a/server/Controllers/FeedbackController.js b/server/Controllers/FeedbackController.js new file mode 100644 index 0000000..0a368a5 --- /dev/null +++ b/server/Controllers/FeedbackController.js @@ -0,0 +1,64 @@ +const Feedback = require("../Models/FeedbackModel"); +const { ElectiveSubject } = require("../Models/StudentModel"); +const { Student } = require("../Models/StudentModel"); +exports.addFeedback = async (req, res) => { + try { + const { electiveSubjectId, studentId, reviews } = req.body; + console.log(electiveSubjectId, studentId, reviews); + if (!electiveSubjectId || !studentId || !reviews) { + return res + .status(400) + .json({ error: "Please provide all the required fields" }); + } + const student = await Student.findById(studentId); + const electiveSubject = await ElectiveSubject.findById(electiveSubjectId); + if (!student || !electiveSubject) { + return res + .status(404) + .json({ error: "Student or Elective subject not found" }); + } + const feedback = new Feedback({ + student: student._id, + electiveSubject: electiveSubject._id, + reviews, + }); + const savedFeedback = await feedback.save(); + res.json(savedFeedback); + } catch (error) { + console.log(error); + res.status(500).json({ error: "Internal server error" }); + } +}; + +exports.getFeedback = async (req, res) => { + try { + let { subjectCode } = req.params; + if (!subjectCode) { + return res + .status(400) + .json({ error: "Please provide the Elective Subject Id" }); + } + subjectCode = subjectCode.split(":")[1]; + console.log(subjectCode); + const subject = await ElectiveSubject.findOne( + { subjectCode: subjectCode }, + "_id" + ); + + console.log(subject); + if (!subject) { + return res.status(404).json({ + error: + "Subject Not Found! Please provide a valid Elective Subject Code", + }); + } + // console.log(subject._id); + const feedbacks = await Feedback.find({ + electiveSubject: subject._id, + }).populate("student"); + res.json(feedbacks); + } catch (error) { + console.log(error); + res.status(500).json({ error: "Internal server error" }); + } +}; diff --git a/server/Controllers/StudentController.js b/server/Controllers/StudentController.js index 6ea385c..f73edea 100644 --- a/server/Controllers/StudentController.js +++ b/server/Controllers/StudentController.js @@ -1,240 +1,183 @@ -const{ Student} = require("../Models/StudentModel"); - +const { Student } = require("../Models/StudentModel"); exports.getAllStudents = async (req, res) => { console.log("hello"); - try { - console.log(Student); - - const students = await Student.find({}); - console.log(students); - // Process students data here - - if(!students) - { - res.status(404).json({ error: "No students" }); - } - console.log("student",students); - res.json(students); - } catch (error) { - res.status(500).json({ error: "Internal server error" }); - } - }; - - exports.getStudentById = async (req, res) => { - try { - - const { id } = req.params; - console.log(id); - - // Find the student by ID - const student = await Student.find({idNumber:id}); - console.log("heelo",student); - // Check if the student exists - if (!student) { - return res.status(404).json({ error: 'Student not found' }); - } - console.log("heelo",student); - res.json(student); - } catch (error) { - res.status(500).json({ error: 'Internal server error' }); - } - }; - - - - exports.deleteStudent = async (req, res) => { - try { - const { id } = req.params; - - // Check if the student exists - - const student = await Student.findOneAndDelete({ idNumber: id }); - - if (!student) { - return res.status(404).json({ error: "Student not found" }); - } - - // Delete the student - - - res.json({ message: "Student deleted successfully" }); - } catch (error) { - res.status(500).json({ error: "Internal server error" }); - } - }; - - - - - - - - - + try { + console.log(Student); + const students = await Student.find({}); + console.log(students); + // Process students data here - - exports.addStudent = async (req, res) => { - try { - const { name, idNumber, email, phoneNumber } = req.body; - - // Check if the student already exists by idNumber or email - const existingStudent = await Student.findOne({ - $or: [{ idNumber: idNumber }, { email: email }] - }); - - if (existingStudent) { - return res.status(400).json({ error: "Student already exists" }); - } - - const student = new Student({ name, idNumber, email, phoneNumber }); - const newStudent = await student.save(); - res.json(newStudent); - } catch (error) { - res.status(500).json({ error: "Internal server error" }); + if (!students) { + res.status(404).json({ error: "No students" }); } - }; - - exports.updateStudent = async (req, res) => { - try { - const { id } = req.params; - const { name, idNumber, email, phoneNumber } = req.body; - console.log(name,idNumber,email,phoneNumber); - // Check if the student exists - const student = await Student.findOne({idNumber:id}); - - - if (!student) { - return res.status(404).json({ error: "Student not found" }); - } - - // Update the student details - student.name = name; - console.log(student.name); - student.idNumber = idNumber; - student.email = email; - student.phoneNumber = phoneNumber; - console.log(student); - - const updatedStudent = await student.save(); - - res.json(updatedStudent); - } catch (error) { - res.status(500).json({ error: "Internal server error" }); + console.log("student", students); + res.json(students); + } catch (error) { + res.status(500).json({ error: "Internal server error" }); + } +}; + +exports.getStudentById = async (req, res) => { + try { + const { id } = req.params; + console.log(id); + + // Find the student by ID + const student = await Student.find({ idNumber: id }); + console.log("heelo", student); + // Check if the student exists + if (!student) { + return res.status(404).json({ error: "Student not found" }); } - }; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + console.log("heelo", student); + res.json(student); + } catch (error) { + res.status(500).json({ error: "Internal server error" }); + } +}; +exports.deleteStudent = async (req, res) => { + try { + const { id } = req.params; + // Check if the student exists + const student = await Student.findOneAndDelete({ idNumber: id }); + if (!student) { + return res.status(404).json({ error: "Student not found" }); + } + // Delete the student + res.json({ message: "Student deleted successfully" }); + } catch (error) { + res.status(500).json({ error: "Internal server error" }); + } +}; +exports.addStudent = async (req, res) => { + try { + const { name, idNumber, email, phoneNumber } = req.body; + // Check if the student already exists by idNumber or email + const existingStudent = await Student.findOne({ + $or: [{ idNumber: idNumber }, { email: email }], + }); + if (existingStudent) { + return res.status(400).json({ error: "Student already exists" }); + } + const student = new Student({ name, idNumber, email, phoneNumber }); + const newStudent = await student.save(); + console.log(newStudent); + res.json(newStudent); + } catch (error) { + console.log(error); + res.status(500).json({ error: "Internal server error" }); + } +}; + +exports.updateStudent = async (req, res) => { + try { + const { id } = req.params; + const { name, idNumber, email, phoneNumber } = req.body; + console.log(name, idNumber, email, phoneNumber); + // Check if the student exists + const student = await Student.findOne({ idNumber: id }); + + if (!student) { + return res.status(404).json({ error: "Student not found" }); + } - exports.insertDummyData = (req, res) => { - const dummyData =[ - { - "name": "John Doe", - "idNumber": "2021001", - "email": "john.doe@example.com", - "phoneNumber": "+91 9876543210" - }, - { - "name": "Jane Smith", - "idNumber": "2021002", - "email": "jane.smith@example.com", - "phoneNumber": "+91 9876543211" - }, - { - "name": "Robert Johnson", - "idNumber": "2021003", - "email": "robert.johnson@example.com", - "phoneNumber": "+91 9876543212" - }, - { - "name": "Emily Davis", - "idNumber": "2021004", - "email": "emily.davis@example.com", - "phoneNumber": "+91 9876543213" - }, - { - "name": "Michael Wilson", - "idNumber": "2021005", - "email": "michael.wilson@example.com", - "phoneNumber": "+91 9876543214" - }, - { - "name": "Olivia Taylor", - "idNumber": "2021006", - "email": "olivia.taylor@example.com", - "phoneNumber": "+91 9876543215" - }, - { - "name": "William Anderson", - "idNumber": "2021007", - "email": "william.anderson@example.com", - "phoneNumber": "+91 9876543216" - }, - { - "name": "Sophia Martinez", - "idNumber": "2021008", - "email": "sophia.martinez@example.com", - "phoneNumber": "+91 9876543217" - }, - { - "name": "Liam Thomas", - "idNumber": "2021009", - "email": "liam.thomas@example.com", - "phoneNumber": "+91 9876543218" - }, - { - "name": "Ava Clark", - "idNumber": "2021010", - "email": "ava.clark@example.com", - "phoneNumber": "+91 9876543219" - } - ] - - - - Student.insertMany(dummyData).then(function () { - console.log("Successfully saved defult items to DB"); - }).catch(function (err) { - console.log(err); - }); - - - }; \ No newline at end of file + // Update the student details + student.name = name; + console.log(student.name); + student.idNumber = idNumber; + student.email = email; + student.phoneNumber = phoneNumber; + console.log(student); + + const updatedStudent = await student.save(); + + res.json(updatedStudent); + } catch (error) { + res.status(500).json({ error: "Internal server error" }); + } +}; + +exports.insertDummyData = (req, res) => { + const dummyData = [ + { + name: "John Doe", + idNumber: "2021001", + email: "john.doe@example.com", + phoneNumber: "+91 9876543210", + }, + { + name: "Jane Smith", + idNumber: "2021002", + email: "jane.smith@example.com", + phoneNumber: "+91 9876543211", + }, + { + name: "Robert Johnson", + idNumber: "2021003", + email: "robert.johnson@example.com", + phoneNumber: "+91 9876543212", + }, + { + name: "Emily Davis", + idNumber: "2021004", + email: "emily.davis@example.com", + phoneNumber: "+91 9876543213", + }, + { + name: "Michael Wilson", + idNumber: "2021005", + email: "michael.wilson@example.com", + phoneNumber: "+91 9876543214", + }, + { + name: "Olivia Taylor", + idNumber: "2021006", + email: "olivia.taylor@example.com", + phoneNumber: "+91 9876543215", + }, + { + name: "William Anderson", + idNumber: "2021007", + email: "william.anderson@example.com", + phoneNumber: "+91 9876543216", + }, + { + name: "Sophia Martinez", + idNumber: "2021008", + email: "sophia.martinez@example.com", + phoneNumber: "+91 9876543217", + }, + { + name: "Liam Thomas", + idNumber: "2021009", + email: "liam.thomas@example.com", + phoneNumber: "+91 9876543218", + }, + { + name: "Ava Clark", + idNumber: "2021010", + email: "ava.clark@example.com", + phoneNumber: "+91 9876543219", + }, + ]; + + Student.insertMany(dummyData) + .then(function () { + console.log("Successfully saved defult items to DB"); + }) + .catch(function (err) { + console.log(err); + }); +}; diff --git a/server/Controllers/StudentElectiveSubjectController.js b/server/Controllers/StudentElectiveSubjectController.js index 73c6583..260f809 100644 --- a/server/Controllers/StudentElectiveSubjectController.js +++ b/server/Controllers/StudentElectiveSubjectController.js @@ -1,224 +1,247 @@ +const StudentElectiveSubject = require("../Models/StudentElectiveSubjectModel"); - -const StudentElectiveSubject =require('../Models/StudentElectiveSubjectModel'); - -const{ Student,ElectiveSubject} = require("../Models/StudentModel"); +const { Student, ElectiveSubject } = require("../Models/StudentModel"); exports.addElectiveSubjectToStudent = async (req, res) => { - try { - const { studentId, electiveSubjectId } = req.body; - const student = await Student.findById(studentId); - console.log(student); - const electiveSubject = await ElectiveSubject.findById(electiveSubjectId); - console.log(electiveSubject); - if (!student || !electiveSubject) { - return res.status(404).json({ error: 'Student or Elective subject not found' }); - } - const studentElectiveSubject = new StudentElectiveSubject({ - student: student._id, - electiveSubject: electiveSubject._id - }); - const savedStudentElectiveSubject = await studentElectiveSubject.save(); - res.json(savedStudentElectiveSubject); - } catch (error) { - res.status(500).json({ error: 'Internal server error' }); + try { + const { studentId, electiveSubjectId } = req.body; + const student = await Student.findById(studentId); + console.log(student); + const electiveSubject = await ElectiveSubject.findById(electiveSubjectId); + console.log(electiveSubject); + if (!student || !electiveSubject) { + return res + .status(404) + .json({ error: "Student or Elective subject not found" }); } - }; - - // Remove an elective subject from a student - exports.removeElectiveSubjectFromStudent = async (req, res) => { - try { - const { studentId, electiveSubjectId } = req.body; - const student = await Student.findById(studentId); - const electiveSubject = await ElectiveSubject.findById(electiveSubjectId); - if (!student || !electiveSubject) { - return res.status(404).json({ error: 'Student or Elective subject not found' }); - } - const removedStudentElectiveSubject = await StudentElectiveSubject.findOneAndDelete({ + const studentElectiveSubject = new StudentElectiveSubject({ + student: student._id, + electiveSubject: electiveSubject._id, + }); + const savedStudentElectiveSubject = await studentElectiveSubject.save(); + res.json(savedStudentElectiveSubject); + } catch (error) { + res.status(500).json({ error: "Internal server error" }); + } +}; + +// Remove an elective subject from a student +exports.removeElectiveSubjectFromStudent = async (req, res) => { + try { + const { studentId, electiveSubjectId } = req.body; + const student = await Student.findById(studentId); + const electiveSubject = await ElectiveSubject.findById(electiveSubjectId); + if (!student || !electiveSubject) { + return res + .status(404) + .json({ error: "Student or Elective subject not found" }); + } + const removedStudentElectiveSubject = + await StudentElectiveSubject.findOneAndDelete({ student: student._id, - electiveSubject: electiveSubject._id + electiveSubject: electiveSubject._id, }); - if (!removedStudentElectiveSubject) { - return res.status(404).json({ error: 'Student-Elective subject relationship not found' }); - } - res.json({ message: 'Elective subject removed from student successfully' }); - } catch (error) { - res.status(500).json({ error: 'Internal server error' }); + if (!removedStudentElectiveSubject) { + return res + .status(404) + .json({ error: "Student-Elective subject relationship not found" }); } - }; - - // Get all elective subjects for a student - exports.getElectiveSubjectsForStudent = async (req, res) => { - try { - - const { id } = req.params; - - const student = await Student.findOne({_id:id}); - - if (!student) { - return res.status(404).json({ error: 'Student not found' }); - } - const studentElectiveSubjects = await StudentElectiveSubject.find({ student: student._id }).populate('electiveSubject'); - res.json(studentElectiveSubjects); - } catch (error) { - res.status(500).json({ error: 'Internal server error' }); + res.json({ message: "Elective subject removed from student successfully" }); + } catch (error) { + res.status(500).json({ error: "Internal server error" }); + } +}; + +// Get all elective subjects for a student +exports.getElectiveSubjectsForStudent = async (req, res) => { + try { + const { id } = req.params; + + const student = await Student.findOne({ _id: id }); + + if (!student) { + return res.status(404).json({ error: "Student not found" }); } - }; - exports.getStudentsCountPerElectiveSubject = async (req, res) => { - try { - const electiveSubjects = await ElectiveSubject.find(); - - const studentsCountPerSubject = []; - - for (const subject of electiveSubjects) { - const studentElectiveSubjects = await StudentElectiveSubject.find({ - electiveSubject: subject._id - }); - - studentsCountPerSubject.push({ - subjectName: subject.subjectName, - studentCount: studentElectiveSubjects.length - }); - } - - res.json(studentsCountPerSubject); - } catch (error) { - res.status(500).json({ error: 'Internal server error' }); + const studentElectiveSubjects = await StudentElectiveSubject.find({ + student: student._id, + }).populate("electiveSubject"); + res.json(studentElectiveSubjects); + } catch (error) { + res.status(500).json({ error: "Internal server error" }); + } +}; +exports.getStudentsCountPerElectiveSubject = async (req, res) => { + try { + const electiveSubjects = await ElectiveSubject.find(); + + const studentsCountPerSubject = []; + + for (const subject of electiveSubjects) { + const studentElectiveSubjects = await StudentElectiveSubject.find({ + electiveSubject: subject._id, + }); + + studentsCountPerSubject.push({ + subjectName: subject.subjectName, + studentCount: studentElectiveSubjects.length, + }); } - }; - exports.generateTreeData = async (req, res) => { - try { - const allElectiveSubjects = await ElectiveSubject.find(); - - const treeData = { - type: "node", - name: "boss", - value: 0, - children: [], - }; - - for (const electiveSubject of allElectiveSubjects) { - const studentElectiveSubjects = await StudentElectiveSubject.find({ - electiveSubject: electiveSubject._id, - }).populate('student', ['name']); - console.log(studentElectiveSubjects) - if (studentElectiveSubjects.length > 0) { - const subjectNode = { - type: "node", - name: electiveSubject.subjectName, + + res.json(studentsCountPerSubject); + } catch (error) { + res.status(500).json({ error: "Internal server error" }); + } +}; +exports.generateTreeData = async (req, res) => { + try { + const allElectiveSubjects = await ElectiveSubject.find(); + + const treeData = { + type: "node", + name: "boss", + value: 0, + children: [], + }; + + for (const electiveSubject of allElectiveSubjects) { + const studentElectiveSubjects = await StudentElectiveSubject.find({ + electiveSubject: electiveSubject._id, + }).populate("student", ["name"]); + console.log(studentElectiveSubjects); + if (studentElectiveSubjects.length > 0) { + const subjectNode = { + type: "node", + name: electiveSubject.subjectName, + value: Math.floor(Math.random() * (90 - 40 + 1)) + 40, + children: studentElectiveSubjects.map((studentElectiveSubject) => ({ + type: "leaf", + name: studentElectiveSubject.student.name, value: Math.floor(Math.random() * (90 - 40 + 1)) + 40, - children: studentElectiveSubjects.map((studentElectiveSubject) => ({ - type: "leaf", - name: studentElectiveSubject.student.name, - value: Math.floor(Math.random() * (90 - 40 + 1)) + 40, - })), - }; - treeData.children.push(subjectNode); - } + })), + }; + treeData.children.push(subjectNode); } - - res.json(treeData); - } catch (error) { - res.status(500).json({ error: 'Internal server error' }); } - }; - - - // Get all students for an elective subject - exports.getStudentsForElectiveSubject = async (req, res) => { - try { - const { id } = req.params; - const electiveSubject = await ElectiveSubject.findById(id); - console.log("hello"); - console.log(electiveSubject); - if (!electiveSubject) { - return res.status(404).json({ error: 'Elective subject not found' }); - } - - const studentElectiveSubjects = await StudentElectiveSubject.find({ electiveSubject: electiveSubject._id }) - .populate('student', ['name', 'idNumber', 'email', 'phoneNumber']); - console.log(studentElectiveSubjects); - - res.json(studentElectiveSubjects); - } catch (error) { - res.status(500).json({ error: 'Internal server error' }); + + res.json(treeData); + } catch (error) { + res.status(500).json({ error: "Internal server error" }); + } +}; + +// Get all students for an elective subject +exports.getStudentsForElectiveSubject = async (req, res) => { + try { + const { id } = req.params; + const electiveSubject = await ElectiveSubject.findById(id); + console.log("hello"); + console.log(electiveSubject); + if (!electiveSubject) { + return res.status(404).json({ error: "Elective subject not found" }); } - }; - - // Edit the subject details of a student - exports.editSubjectOfStudent = async (req, res) => { - try { - const { studentId, electiveSubjectId, subjectName, subjectDescription, subjectCode } = req.body; - const student = await Student.findById(studentId); - - if (!student) { - return res.status(404).json({ error: 'Student not found' }); - } - - const studentElectiveSubject = await StudentElectiveSubject.findOne({ student: studentId, electiveSubject: electiveSubjectId }) - .populate('electiveSubject'); - - if (!studentElectiveSubject) { - return res.status(404).json({ error: 'Student-Elective subject relationship not found' }); - } - - // Update the subject details - studentElectiveSubject.electiveSubject.subjectName = subjectName; - studentElectiveSubject.electiveSubject.subjectDescription = subjectDescription; - studentElectiveSubject.electiveSubject.subjectCode = subjectCode; - - const updatedStudentElectiveSubject = await studentElectiveSubject.save(); - res.json(updatedStudentElectiveSubject); - } catch (error) { - res.status(500).json({ error: 'Internal server error' }); + + const studentElectiveSubjects = await StudentElectiveSubject.find({ + electiveSubject: electiveSubject._id, + }).populate("student", ["name", "idNumber", "email", "phoneNumber"]); + console.log(studentElectiveSubjects); + + res.json(studentElectiveSubjects); + } catch (error) { + res.status(500).json({ error: "Internal server error" }); + } +}; + +// Edit the subject details of a student +exports.editSubjectOfStudent = async (req, res) => { + try { + const { + studentId, + electiveSubjectId, + subjectName, + subjectDescription, + subjectCode, + } = req.body; + const student = await Student.findById(studentId); + + if (!student) { + return res.status(404).json({ error: "Student not found" }); } - }; - - // Add a student to an elective subject + + const studentElectiveSubject = await StudentElectiveSubject.findOne({ + student: studentId, + electiveSubject: electiveSubjectId, + }).populate("electiveSubject"); + + if (!studentElectiveSubject) { + return res + .status(404) + .json({ error: "Student-Elective subject relationship not found" }); + } + + // Update the subject details + studentElectiveSubject.electiveSubject.subjectName = subjectName; + studentElectiveSubject.electiveSubject.subjectDescription = + subjectDescription; + studentElectiveSubject.electiveSubject.subjectCode = subjectCode; + + const updatedStudentElectiveSubject = await studentElectiveSubject.save(); + res.json(updatedStudentElectiveSubject); + } catch (error) { + res.status(500).json({ error: "Internal server error" }); + } +}; + +// Add a student to an elective subject exports.addStudentToElectiveSubject = async (req, res) => { - try { - const { studentId, electiveSubjectId } = req.body; - const student = await Student.findById(studentId); - const electiveSubject = await ElectiveSubject.findById(electiveSubjectId); - - - if (!student || !electiveSubject) { - return res.status(404).json({ error: 'Student or Elective subject not found' }); - } - - const studentElectiveSubject = new StudentElectiveSubject({ - student: student._id, - electiveSubject: electiveSubject._id - }); - - const savedStudentElectiveSubject = await studentElectiveSubject.save(); - res.json(savedStudentElectiveSubject); - } catch (error) { - res.status(500).json({ error: 'Internal server error' }); + try { + const { studentId, electiveSubjectId } = req.body; + const student = await Student.findById(studentId); + const electiveSubject = await ElectiveSubject.findById(electiveSubjectId); + + if (!student || !electiveSubject) { + return res + .status(404) + .json({ error: "Student or Elective subject not found" }); } - }; - - exports.removeStudentFromElectiveSubject = async (req, res) => { - try { - const { studentId, electiveSubjectId } = req.body; - const student = await Student.findById(studentId); - const electiveSubject = await ElectiveSubject.findById(electiveSubjectId); - - if (!student || !electiveSubject) { - return res.status(404).json({ error: 'Student or Elective subject not found' }); - } - - const removedStudentElectiveSubject = await StudentElectiveSubject.findOneAndDelete({ + + const studentElectiveSubject = new StudentElectiveSubject({ + student: student._id, + electiveSubject: electiveSubject._id, + }); + + const savedStudentElectiveSubject = await studentElectiveSubject.save(); + res.json(savedStudentElectiveSubject); + } catch (error) { + res.status(500).json({ error: "Internal server error" }); + } +}; + +exports.removeStudentFromElectiveSubject = async (req, res) => { + try { + const { studentId, electiveSubjectId } = req.body; + const student = await Student.findById(studentId); + const electiveSubject = await ElectiveSubject.findById(electiveSubjectId); + + if (!student || !electiveSubject) { + return res + .status(404) + .json({ error: "Student or Elective subject not found" }); + } + + const removedStudentElectiveSubject = + await StudentElectiveSubject.findOneAndDelete({ student: student._id, electiveSubject: electiveSubject._id, }); - - if (!removedStudentElectiveSubject) { - return res.status(404).json({ error: 'Student-Elective subject relationship not found' }); - } - - res.json({ message: 'Student removed from elective subject successfully' }); - } catch (error) { - res.status(500).json({ error: 'Internal server error' }); + + if (!removedStudentElectiveSubject) { + return res + .status(404) + .json({ error: "Student-Elective subject relationship not found" }); } - }; \ No newline at end of file + + res.json({ message: "Student removed from elective subject successfully" }); + } catch (error) { + res.status(500).json({ error: "Internal server error" }); + } +}; diff --git a/server/Middleware/AuthMiddleware.js b/server/Middleware/AuthMiddleware.js index 065861f..3fa74df 100644 --- a/server/Middleware/AuthMiddleware.js +++ b/server/Middleware/AuthMiddleware.js @@ -3,33 +3,35 @@ const jwt = require("jsonwebtoken"); const User = require("../Models/UserModel"); module.exports.authMiddleware = (requiredRoles) => { -return async (req, res, next) => { + return async (req, res, next) => { try { - console.log(requiredRoles) - console.log("Headers",req.headers.authorization); - var token =req.headers.authorization; + console.log(requiredRoles); + console.log("Headers", req.headers.authorization); + var token = req.headers.authorization; if (!token) { - return res.status(401).json({ message: "Access denied. No token provided." }); + return res + .status(401) + .json({ message: "Access denied. No token provided." }); } - token=token.split(" ")[1]; - jwt.verify(token, process.env.TOKEN_KEY, async (err, decoded) => { + token = token.split(" ")[1]; + jwt.verify(token, process.env.TOKEN_KEY, async (err, decoded) => { if (err) { return res.status(401).json({ message: "Invalid token." }); } - - const user = await User.findById(decoded.id) ; - if (user) req.user=user;// Assuming the JWT payload contains the user information + const user = await User.findById(decoded.id); + if (user) req.user = user; // Assuming the JWT payload contains the user information const { role } = user; console.log(role); // // console.log(role); - if (!hasRequiredRole(role, requiredRoles)) { - console.log("error",requiredRoles); - - return res.status(403).json({ message: "Access denied. Insufficient role." }); - } - + if (!hasRequiredRole(role, requiredRoles)) { + console.log("error", requiredRoles); + + return res + .status(403) + .json({ message: "Access denied. Insufficient role." }); + } next(); }); @@ -37,9 +39,8 @@ return async (req, res, next) => { console.error(error); res.status(500).json({ message: "Server error." }); } - } - -} + }; +}; const hasRequiredRole = (userRole, requiredRoles) => { return requiredRoles.includes(userRole); }; diff --git a/server/Models/FeedbackModel.js b/server/Models/FeedbackModel.js new file mode 100644 index 0000000..61cc1c5 --- /dev/null +++ b/server/Models/FeedbackModel.js @@ -0,0 +1,18 @@ +const mongoose = require("mongoose"); + +const feedbackSchema = new mongoose.Schema({ + electiveSubject: { + type: mongoose.Schema.Types.ObjectId, + ref: "ElectiveSubject", + }, + reviews: { + type: String, + }, + student: { + type: mongoose.Schema.Types.ObjectId, + ref: "Student", + }, +}); + +const Feedback = mongoose.model("Feedback", feedbackSchema); +module.exports = Feedback; diff --git a/server/Routes/FeedbackRoutes.js b/server/Routes/FeedbackRoutes.js new file mode 100644 index 0000000..3894c79 --- /dev/null +++ b/server/Routes/FeedbackRoutes.js @@ -0,0 +1,8 @@ +const router = require("express").Router(); +const { + addFeedback, + getFeedback, +} = require("../Controllers/FeedbackController"); +router.post("/addFeedback", addFeedback); +router.get("/getFeedback/:subjectCode", getFeedback); +module.exports = router; diff --git a/server/app.js b/server/app.js index 50ce8d8..1b62e60 100644 --- a/server/app.js +++ b/server/app.js @@ -5,10 +5,11 @@ const app = express(); const cookieParser = require("cookie-parser"); const authRoute = require("./Routes/AuthRoutes"); const StudentRoute = require("./Routes/StudentRoutes"); -const SubjectRoute = require('./Routes/SubjectRoutes'); -const StudentElectiveSubjectRoute = require('./Routes/StudentElectiveSubjectRoutes'); +const SubjectRoute = require("./Routes/SubjectRoutes"); +const StudentElectiveSubjectRoute = require("./Routes/StudentElectiveSubjectRoutes"); const { authMiddleware } = require("./Middleware/AuthMiddleware"); -const axios =require('axios') +const axios = require("axios"); +const FeedbackRoute = require("./Routes/FeedbackRoutes"); require("dotenv").config(); const { MONGO_URL, PORT } = process.env; @@ -24,7 +25,7 @@ const allowedOrigins = [ "http://127.0.0.1:5173", "http://localhost:5173", "https://elective-subject-selector-pgtl.onrender.com", - "https://electivehub.onrender.com" + "https://electivehub.onrender.com", // Add more URLs as needed ]; app.use(cookieParser()); @@ -52,11 +53,12 @@ app.use( authMiddleware(["admin", "user", "student"]), StudentElectiveSubjectRoute ); -app.use("/student", authMiddleware(["admin", "user","student"]), StudentRoute); -app.use("/subject", authMiddleware(["admin", "user","student"]), SubjectRoute); -app.get("/yaae", authMiddleware(["admin", "user","student"]), (req, res) => { +app.use("/student", authMiddleware(["admin", "user", "student"]), StudentRoute); +app.use("/subject", authMiddleware(["admin", "user", "student"]), SubjectRoute); +app.get("/yaae", authMiddleware(["admin", "user", "student"]), (req, res) => { res.json({ status: true, user: req.user }); }); +app.use("/feedback", FeedbackRoute); app.post("/subscribe-newsletter", async (req, res) => { try { @@ -72,10 +74,12 @@ app.post("/subscribe-newsletter", async (req, res) => { publicationId, } ); -console.log(response); + console.log(response); // Handle the response from the API call if (response.status === 200) { - res.status(200).json({ message: "Subscribed to the newsletter successfully!" }); + res + .status(200) + .json({ message: "Subscribed to the newsletter successfully!" }); } else { res.status(500).json({ error: "Error subscribing to the newsletter" }); } @@ -85,7 +89,6 @@ console.log(response); } }); - app.listen(PORT, () => { console.log(`Server is listening on port ${PORT}`); });