-
Notifications
You must be signed in to change notification settings - Fork 23
Paper -> Count Route #219
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
Paper -> Count Route #219
Changes from all commits
5da17de
6fb6e87
a6baf37
17d3736
899d28d
aa8b08d
f1ad130
a433e41
30f284e
ee177a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,20 +4,21 @@ import Paper from "@/db/papers"; | |
|
||
export const dynamic = "force-dynamic"; | ||
|
||
export async function GET() { | ||
export async function GET(req: Request) { | ||
try { | ||
await connectToDatabase(); | ||
|
||
const count: number = await Paper.countDocuments(); | ||
const { searchParams } = new URL(req.url); | ||
const subject = searchParams.get("subject"); | ||
|
||
return NextResponse.json( | ||
{ count }, | ||
{ status: 200 } | ||
); | ||
const filter = subject ? { subject } : {}; | ||
const count = await Paper.countDocuments(filter); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not just maintain a counter in db |
||
|
||
return NextResponse.json({ count }, { status: 200 }); | ||
} catch (error) { | ||
return NextResponse.json( | ||
{ message: "Failed to fetch papers", error }, | ||
{ status: 500 } | ||
{ status: 500 }, | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,7 @@ export async function GET(req: NextRequest) { | |
if (papers.length === 0) { | ||
return NextResponse.json( | ||
{ message: "No papers found for the specified subject" }, | ||
{ status: 404 }, | ||
{ status: 200 }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. errors shouldn't be 200 |
||
); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { NextResponse } from "next/server"; | ||
import { connectToDatabase } from "@/lib/mongoose"; | ||
import Paper from "@/db/papers"; | ||
import { StoredSubjects } from "@/interface"; | ||
|
||
export const dynamic = "force-dynamic"; | ||
|
||
export async function POST(req: Request) { | ||
try { | ||
await connectToDatabase(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will happen at startup u dont need to do this |
||
const body = (await req.json()) as StoredSubjects; | ||
|
||
const subjects = body; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. redundant |
||
|
||
const usersPapers = await Paper.find({ | ||
subject: { $in: subjects }, | ||
}); | ||
|
||
const transformedPapers = usersPapers.reduce( | ||
(acc: { subject: string; slots: string[] }[], paper) => { | ||
const existing = acc.find((item) => item.subject === paper.subject); | ||
|
||
if (existing) { | ||
existing.slots.push(paper.slot); | ||
} else { | ||
acc.push({ subject: paper.subject, slots: [paper.slot] }); | ||
} | ||
|
||
return acc; | ||
}, | ||
[], | ||
); | ||
|
||
const seenSubjects = new Set(); | ||
const uniquePapers = transformedPapers.filter((paper) => { | ||
if (seenSubjects.has(paper.subject)) return false; | ||
seenSubjects.add(paper.subject); | ||
return true; | ||
}); | ||
|
||
return NextResponse.json(uniquePapers, { | ||
status: 200, | ||
}); | ||
} catch (error) { | ||
console.error("Error fetching papers:", error); | ||
return NextResponse.json( | ||
{ | ||
error: "Failed to fetch papers.", | ||
}, | ||
{ status: 500 }, | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React from "react"; | ||
import SearchBar from "@/components/Searchbar/searchbar"; | ||
import PinnedPapersCarousel from "@/components/PinnedPapersCarousel"; | ||
|
||
const Pinned = () => { | ||
return ( | ||
<div id="pinned" className="flex flex-col justify-between"> | ||
<h1 className="mx-auto my-8 text-center font-vipnabd text-3xl font-extrabold"> | ||
Pinned Papers | ||
</h1> | ||
|
||
<div className="flex items-center justify-center gap-4 px-6"> | ||
<div className="flex max-w-xl flex-1"> | ||
<SearchBar type="pinned" /> | ||
</div> | ||
</div> | ||
<PinnedPapersCarousel carouselType="users" /> | ||
<div className="mt-6 flex w-full items-center justify-center"> | ||
<p>You can pin upto 8 Subjects</p> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Pinned; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const AddPapers = ({ onClick }: { onClick?: () => void }) => { | ||
return ( | ||
<div | ||
onClick={onClick} | ||
className="flex h-full w-full cursor-pointer items-center justify-center rounded-md border-2 border-dashed border-purple-500 bg-transparent transition hover:border-gray-400 hover:bg-[#2a2a2a] hover:text-gray-300" | ||
> | ||
<span className="text-4xl text-purple-500 transition-colors duration-200 group-hover:text-gray-300"> | ||
+ | ||
</span> | ||
</div> | ||
); | ||
}; | ||
|
||
export default AddPapers; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"use client"; | ||
|
||
import { Star, StarOff } from "lucide-react"; | ||
|
||
export default function PinButton({ | ||
isPinned, | ||
onToggle, | ||
}: { | ||
isPinned: boolean; | ||
onToggle?: () => void; | ||
}) { | ||
return ( | ||
<button | ||
onClick={onToggle} | ||
className={`ml-2 flex items-center gap-2 rounded-full px-4 py-2 text-sm font-medium ${ | ||
isPinned ? "bg-purple-700 text-white" : "bg-[#2B2B30] text-white/80" | ||
} transition hover:bg-purple-600`} | ||
> | ||
{isPinned ? ( | ||
<Star className="h-4 w-4" /> | ||
) : ( | ||
<StarOff className="h-4 w-4" /> | ||
)} | ||
<span className="hidden sm:inline">{isPinned ? "Pinned" : "Pin"}</span> | ||
</button> | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
req.query.subject
should work