Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate name checking route #7

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/routers/duplicate.router.ts
Original file line number Diff line number Diff line change
@@ -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" });
}
}
},
);
2 changes: 2 additions & 0 deletions src/routers/root.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -18,3 +19,4 @@ router.use("/headings", headingRouter);
router.use("/projects", projectRouter);
router.use("/resumes", resumeRouter);
router.use("/sectionHeadings", sectionHeadingRouter);
router.use("/sectionHeadings", duplicateRouter);
Loading