|
| 1 | +import { Router, type Request, type Response } from "express"; |
| 2 | +import { |
| 3 | + createEducation, |
| 4 | + getAllEducation, |
| 5 | + getEducationById, |
| 6 | + updateEducation, |
| 7 | + deleteEducation, |
| 8 | +} from "../controllers/education.controller"; |
| 9 | +import { HttpError, HttpStatus } from "../utils/errors"; |
| 10 | +import { type EducationType } from "../models/education.model"; |
| 11 | + |
| 12 | +export const educationRouter = Router(); |
| 13 | + |
| 14 | +//Add an education |
| 15 | +//Note that the user field (which is part of EducationType) in body is automatically populated by verifyToken middleware |
| 16 | +educationRouter.post( |
| 17 | + "/", |
| 18 | + async (req: Request<any, any, EducationType>, res: Response) => { |
| 19 | + try { |
| 20 | + const education = await createEducation(req.body); |
| 21 | + res.status(HttpStatus.OK).json(education); |
| 22 | + } catch (err: unknown) { |
| 23 | + if (err instanceof HttpError) { |
| 24 | + res.status(err.errorCode).json({ error: err.message }); |
| 25 | + } else { |
| 26 | + res |
| 27 | + .status(HttpStatus.INTERNAL_SERVER_ERROR) |
| 28 | + .json({ error: "An unknown error occurred" }); |
| 29 | + } |
| 30 | + } |
| 31 | + }, |
| 32 | +); |
| 33 | + |
| 34 | +//Get all education |
| 35 | +educationRouter.get( |
| 36 | + "/", |
| 37 | + async (req: Request<any, any, EducationType>, res: Response) => { |
| 38 | + try { |
| 39 | + const education = await getAllEducation(req.body.user); |
| 40 | + res.status(HttpStatus.OK).json(education); |
| 41 | + } catch (err: unknown) { |
| 42 | + if (err instanceof HttpError) { |
| 43 | + res.status(err.errorCode).json({ error: err.message }); |
| 44 | + } else { |
| 45 | + res |
| 46 | + .status(HttpStatus.INTERNAL_SERVER_ERROR) |
| 47 | + .json({ error: "An unknown error occurred" }); |
| 48 | + } |
| 49 | + } |
| 50 | + }, |
| 51 | +); |
| 52 | + |
| 53 | +//Get a single education by id |
| 54 | +educationRouter.get( |
| 55 | + "/:educationId", |
| 56 | + async (req: Request<any, any, EducationType>, res: Response) => { |
| 57 | + try { |
| 58 | + const education = await getEducationById( |
| 59 | + req.body.user, |
| 60 | + req.params.educationId, |
| 61 | + ); |
| 62 | + res.status(HttpStatus.OK).json(education); |
| 63 | + } catch (err: unknown) { |
| 64 | + if (err instanceof HttpError) { |
| 65 | + res.status(err.errorCode).json({ error: err.message }); |
| 66 | + } else { |
| 67 | + res |
| 68 | + .status(HttpStatus.INTERNAL_SERVER_ERROR) |
| 69 | + .json({ error: "An unknown error occurred" }); |
| 70 | + } |
| 71 | + } |
| 72 | + }, |
| 73 | +); |
| 74 | + |
| 75 | +//Update an education |
| 76 | +educationRouter.put( |
| 77 | + "/:educationId", |
| 78 | + async (req: Request<any, any, EducationType>, res: Response) => { |
| 79 | + try { |
| 80 | + const education = await updateEducation( |
| 81 | + req.body.user, |
| 82 | + req.params.educationId, |
| 83 | + req.body, |
| 84 | + ); |
| 85 | + res.status(HttpStatus.OK).json(education); |
| 86 | + } catch (err: unknown) { |
| 87 | + if (err instanceof HttpError) { |
| 88 | + res.status(err.errorCode).json({ error: err.message }); |
| 89 | + } else { |
| 90 | + res |
| 91 | + .status(HttpStatus.INTERNAL_SERVER_ERROR) |
| 92 | + .json({ error: "An unknown error occurred" }); |
| 93 | + } |
| 94 | + } |
| 95 | + }, |
| 96 | +); |
| 97 | + |
| 98 | +//Delete an education |
| 99 | +educationRouter.delete( |
| 100 | + "/:educationId", |
| 101 | + async (req: Request<any, any, EducationType>, res: Response) => { |
| 102 | + try { |
| 103 | + const education = await deleteEducation( |
| 104 | + req.body.user, |
| 105 | + req.params.educationId, |
| 106 | + ); |
| 107 | + res.status(HttpStatus.OK).json(education); |
| 108 | + } catch (err: unknown) { |
| 109 | + if (err instanceof HttpError) { |
| 110 | + res.status(err.errorCode).json({ error: err.message }); |
| 111 | + } else { |
| 112 | + res |
| 113 | + .status(HttpStatus.INTERNAL_SERVER_ERROR) |
| 114 | + .json({ error: "An unknown error occurred" }); |
| 115 | + } |
| 116 | + } |
| 117 | + }, |
| 118 | +); |
0 commit comments