From 8ee244adb04d0cb00ff3aa8fd89352e0b7805ec1 Mon Sep 17 00:00:00 2001 From: Jack Garritano <114269804+jackgarritano@users.noreply.github.com> Date: Sat, 13 Apr 2024 15:36:20 -0500 Subject: [PATCH] duplicate name checking route --- src/routers/duplicate.router.ts | 32 ++++++++++++++++++++++++++++++++ src/routers/root.router.ts | 2 ++ 2 files changed, 34 insertions(+) create mode 100644 src/routers/duplicate.router.ts diff --git a/src/routers/duplicate.router.ts b/src/routers/duplicate.router.ts new file mode 100644 index 0000000..e2dc38b --- /dev/null +++ b/src/routers/duplicate.router.ts @@ -0,0 +1,32 @@ +import { Router, type Request, type Response } from "express"; +import { checkDuplicateItemName } from "../utils/checkDuplicates"; +import { HttpError, HttpStatus } from "../utils/errors"; + +export const duplicateRouter = Router(); + +//Get if there is a duplicate +//Include excludedItemId query param to exclude an item +duplicateRouter.get( + "/:itemName", + async ( + req: Request<{ itemName: string }, any, any, { excludedItemId?: string }>, + res: Response, + ) => { + try { + const isDuplicate = await checkDuplicateItemName( + req.body.user, + req.params.itemName, + req.query.excludedItemId || null, + ); + res.status(HttpStatus.OK).json({ isDuplicate }); + } catch (err: unknown) { + if (err instanceof HttpError) { + res.status(err.errorCode).json({ error: err.message }); + } else { + res + .status(HttpStatus.INTERNAL_SERVER_ERROR) + .json({ error: "An unknown error occurred" }); + } + } + }, +); diff --git a/src/routers/root.router.ts b/src/routers/root.router.ts index e3b4f99..8b8426e 100644 --- a/src/routers/root.router.ts +++ b/src/routers/root.router.ts @@ -7,6 +7,7 @@ import { headingRouter } from "./heading.router"; import { projectRouter } from "./project.router"; import { resumeRouter } from "./resume.router"; import { sectionHeadingRouter } from "./sectionHeading.router"; +import { duplicateRouter } from "./duplicate.router"; export const router = Router(); @@ -18,3 +19,4 @@ router.use("/headings", headingRouter); router.use("/projects", projectRouter); router.use("/resumes", resumeRouter); router.use("/sectionHeadings", sectionHeadingRouter); +router.use("/sectionHeadings", duplicateRouter);