diff --git a/backend/src/controllers/student.ts b/backend/src/controllers/student.ts index 626dc23..bb59337 100644 --- a/backend/src/controllers/student.ts +++ b/backend/src/controllers/student.ts @@ -8,6 +8,7 @@ import { validationResult } from "express-validator"; import mongoose, { HydratedDocument } from "mongoose"; import EnrollmentModel from "../models/enrollment"; +import ProgressNoteModel from "../models/progressNote"; import StudentModel from "../models/student"; import { Enrollment } from "../types/enrollment"; import { createEnrollment, editEnrollment } from "../util/enrollment"; @@ -110,3 +111,23 @@ export const getStudent: RequestHandler = async (req, res, next) => { next(error); } }; + +export const deleteStudent: RequestHandler = async (req, res, next) => { + try { + const errors = validationResult(req); + validationErrorParser(errors); + + const studentId = req.params.id; + if (!(await StudentModel.findById(req.params.id))) { + return res.status(404).json({ message: "Student not found" }); + } + + await EnrollmentModel.deleteMany({ studentId }); + await ProgressNoteModel.deleteMany({ studentId }); + await StudentModel.deleteOne({ _id: studentId }); + + res.status(200); + } catch (error) { + next(error); + } +}; diff --git a/backend/src/routes/student.ts b/backend/src/routes/student.ts index f377688..ebb92c8 100644 --- a/backend/src/routes/student.ts +++ b/backend/src/routes/student.ts @@ -6,12 +6,12 @@ import express from "express"; import * as StudentController from "../controllers/student"; import * as StudentValidator from "../validators/student"; - const router = express.Router(); router.post("/create", StudentValidator.createStudent, StudentController.createStudent); router.put("/edit/:id", StudentValidator.editStudent, StudentController.editStudent); router.get("/all", StudentController.getAllStudents); -router.get("/:id", StudentValidator.getStudent, StudentController.getStudent); +router.get("/:id", StudentController.getStudent); +router.delete("/:id", StudentController.deleteStudent); export default router; diff --git a/backend/src/validators/student.ts b/backend/src/validators/student.ts index 9b754d1..aa7254d 100644 --- a/backend/src/validators/student.ts +++ b/backend/src/validators/student.ts @@ -209,4 +209,3 @@ export const createStudent = [ ]; export const editStudent = [...createStudent, makeIdValidator()]; -export const getStudent = makeIdValidator();