Skip to content

Commit

Permalink
Deletion route
Browse files Browse the repository at this point in the history
  • Loading branch information
mraysu committed Nov 7, 2024
1 parent 78f0a9e commit 8d47a80
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
21 changes: 21 additions & 0 deletions backend/src/controllers/student.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}
};
4 changes: 2 additions & 2 deletions backend/src/routes/student.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
1 change: 0 additions & 1 deletion backend/src/validators/student.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,3 @@ export const createStudent = [
];

export const editStudent = [...createStudent, makeIdValidator()];
export const getStudent = makeIdValidator();

0 comments on commit 8d47a80

Please sign in to comment.