-
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.
ClassConnect: Virtual Classroom Controllers are added
- Loading branch information
1 parent
3067b00
commit c253d8c
Showing
2 changed files
with
66 additions
and
54 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,60 @@ | ||
const { StudentModel } = require("../../models/StudentsModel"); | ||
|
||
module.exports.index = (req, res) => { | ||
res.json({ student: "Students data is here" }); | ||
}; | ||
|
||
// Get All Students | ||
|
||
module.exports.getAllStudents = async (req, res) => { | ||
try { | ||
const students = await StudentModel.find({}).sort({ createdAt: -1 }); | ||
res.status(200).json({ students }); | ||
} catch (err) { | ||
res.status(404).json({ error: err.message }); | ||
} | ||
}; | ||
|
||
module.exports.createStudent = async (req, res) => { | ||
const { email, studentName, studentId, studentClass, password } = req.body; | ||
if ( | ||
[email, studentName, studentClass, studentId, password].includes( | ||
"undefined" | ||
) | ||
) { | ||
return res.json({ error: "All fields are required" }); | ||
} | ||
try { | ||
const student = await StudentModel.create(req.body); | ||
res.status(200).json({ student });4 | ||
} catch (err) { | ||
res.status(400).json({ error: err.message }); | ||
} | ||
}; | ||
|
||
// Get Single Student | ||
module.exports.getStudent = async (req, res) => { | ||
const id = req.params.id; | ||
try { | ||
const student = await StudentModel.find({ studentId: id }); | ||
res.status(200).json({ student }); | ||
} catch (err) { | ||
res.status(400).json({ error: err }); | ||
} | ||
}; | ||
|
||
module.exports.deleteStudent = async (req, res) => { | ||
const id = req.params.id; | ||
try { | ||
const student = await StudentModel.findOneAndDelete({ studentId: id }); | ||
res.status(200).json({ student }); | ||
} catch (err) { | ||
res.status(400).json({ error: err }); | ||
} | ||
}; | ||
|
||
// Create Student | ||
// Delete Student | ||
// Update Student | ||
|
||
// 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 |
---|---|---|
@@ -1,62 +1,14 @@ | ||
const express = require("express"); | ||
const router = express.Router(); | ||
const { StudentModel } = require("../../models/StudentsModel"); | ||
const studentController = require("../../controllers/api/Students"); | ||
|
||
router.get("/", (req, res) => { | ||
res.json({ student: "Students data is here" }); | ||
}); | ||
router.get("/", studentController.index); | ||
|
||
// Get All Students | ||
|
||
router.get("/all-students", async (req, res) => { | ||
try { | ||
const students = await StudentModel.find({}).sort({ createdAt: -1 }); | ||
res.status(200).json({ students }); | ||
} catch (err) { | ||
res.status(404).json({ error: err.message }); | ||
} | ||
}); | ||
|
||
router.post("/create-student", async (req, res) => { | ||
const { email, studentName, studentId, studentClass, password } = req.body; | ||
if ( | ||
[email, studentName, studentClass, studentId, password].includes( | ||
"undefined" | ||
) | ||
) { | ||
return res.json({ error: "All fields are required" }); | ||
} | ||
try { | ||
const student = await StudentModel.create(req.body); | ||
res.status(200).json({ student }); | ||
} catch (err) { | ||
res.status(400).json({ error: err.message }); | ||
} | ||
}); | ||
|
||
// Get Single Student | ||
router.get("/:id", async (req, res) => { | ||
const id = req.params.id; | ||
try { | ||
const student = await StudentModel.find({ studentId: id }); | ||
res.status(200).json({ student }); | ||
} catch (err) { | ||
res.status(400).json({ error: err }); | ||
} | ||
}); | ||
|
||
router.delete("/:id", async (req, res) => { | ||
const id = req.params.id; | ||
try { | ||
const student = await StudentModel.findOneAndDelete({ studentId: id }); | ||
res.status(200).json({ student }); | ||
} catch (err) { | ||
res.status(400).json({ error: err }); | ||
} | ||
}); | ||
|
||
// Create Student | ||
// Delete Student | ||
// Update Student | ||
router.get("/all-students", studentController.getAllStudents); | ||
router.post("/create-student", studentController.createStudent); | ||
router.get("/:id", studentController.getStudent); | ||
router.delete("/:id", studentController.deleteStudent); | ||
|
||
module.exports = router; |