Skip to content

Commit 17d3736

Browse files
committed
made paper count route in api
1 parent 6fb6e87 commit 17d3736

File tree

2 files changed

+9
-36
lines changed

2 files changed

+9
-36
lines changed

src/app/api/papers/count/route.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@ import Paper from "@/db/papers";
44

55
export const dynamic = "force-dynamic";
66

7-
export async function GET() {
7+
export async function GET(req: Request) {
88
try {
99
await connectToDatabase();
1010

11-
const count: number = await Paper.countDocuments();
11+
const { searchParams } = new URL(req.url);
12+
const subject = searchParams.get("subject");
1213

13-
return NextResponse.json(
14-
{ count },
15-
{ status: 200 }
16-
);
14+
const filter = subject ? { subject } : {};
15+
const count = await Paper.countDocuments(filter);
16+
17+
return NextResponse.json({ count }, { status: 200 });
1718
} catch (error) {
1819
return NextResponse.json(
1920
{ message: "Failed to fetch papers", error },
20-
{ status: 500 }
21+
{ status: 500 },
2122
);
2223
}
2324
}

src/components/Searchbar/searchbar-child.tsx

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,6 @@ function SearchBarChild({
2020
const suggestionsRef = useRef<HTMLUListElement | null>(null);
2121
const fuzzy = new Fuse(initialSubjects);
2222

23-
const fetchPaperQuantityByName = async (subjectName: string) => {
24-
try {
25-
const response = await axios.get("/api/papers", {
26-
params: { subject: subjectName },
27-
});
28-
29-
if (
30-
response.data.message === "No papers found for the specified subject"
31-
) {
32-
return 0;
33-
}
34-
35-
return response.data.papers.length;
36-
} catch (error) {
37-
console.error("Error fetching paper quantity:", error);
38-
return "request-error";
39-
}
40-
};
41-
4223
const handleSearchChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
4324
const text = e.target.value;
4425
setSearchText(text);
@@ -52,16 +33,7 @@ function SearchBarChild({
5233
.map((item) => item.item)
5334
.slice(0, 10);
5435

55-
const suggestionsWithCount = await Promise.all(
56-
filteredSuggestions.map(async (suggestion) => {
57-
const count = await fetchPaperQuantityByName(suggestion);
58-
return count !== "request-error"
59-
? `${suggestion} (${count})`
60-
: suggestion;
61-
}),
62-
);
63-
64-
setSuggestions(suggestionsWithCount);
36+
setSuggestions(filteredSuggestions);
6537
} else {
6638
setSuggestions([]);
6739
}

0 commit comments

Comments
 (0)