From 8d47a8077ddf33d48b1023450d71288fa3bdc0d6 Mon Sep 17 00:00:00 2001 From: mraysu Date: Thu, 7 Nov 2024 15:36:39 -0800 Subject: [PATCH] Deletion route --- backend/src/controllers/student.ts | 21 +++++++++++++++++++++ backend/src/routes/student.ts | 4 ++-- backend/src/validators/student.ts | 1 - 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/backend/src/controllers/student.ts b/backend/src/controllers/student.ts index 626dc233..bb59337f 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 f3776880..ebb92c80 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 9b754d16..aa7254d8 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();