Skip to content
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
18 changes: 3 additions & 15 deletions src/app/api/related-subject/route.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { getRelatedSubjects } from "@/lib/services/subject";
import { NextResponse, type NextRequest } from "next/server";
import { connectToDatabase } from "@/lib/database/mongoose";
import { IRelatedSubject } from "@/interface";
import RelatedSubject from "@/db/relatedSubjects";
import { escapeRegExp } from "@/lib/utils/regex";

export const dynamic = "force-dynamic";

export async function GET(req: NextRequest) {
try {
await connectToDatabase();
const url = req.nextUrl.searchParams;
const subject = url.get("subject");

Expand All @@ -18,18 +14,10 @@ export async function GET(req: NextRequest) {
{ status: 400 },
);
}

const escapedSubject = escapeRegExp(subject);
const subjects: IRelatedSubject[] = await RelatedSubject.find({
subject: { $regex: new RegExp(`${escapedSubject}`, "i") },
});

console.log("realted", subjects);
const relatedSubjects = await getRelatedSubjects(subject);

return NextResponse.json(
{
related_subjects: subjects[0]?.related_subjects
},
{related_subjects: relatedSubjects},
{ status: 200 },
);
} catch (error) {
Expand Down
1 change: 1 addition & 0 deletions src/app/page.tsx
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the comment in next PR :)

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const HomePage = () => {
<Hero />
<Info />
<Faq />
{/* Happy Birthday Abhi broski, Hope you have an amazing year!!! Love you lots*/}
</div>
);
};
Expand Down
14 changes: 14 additions & 0 deletions src/lib/services/subject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { connectToDatabase } from "@/lib/database/mongoose";
import { IRelatedSubject } from "@/interface";
import { escapeRegExp } from "@/lib/utils/regex";
import RelatedSubject from "@/db/relatedSubjects";

export async function getRelatedSubjects(subject: string) {
await connectToDatabase();
const escapedSubject = escapeRegExp(subject);
const subjects: IRelatedSubject[] = await RelatedSubject.find({
subject: { $regex: new RegExp(`${escapedSubject}`, "i") },
});

return subjects[0]?.related_subjects ?? [];
}
Loading