-
Notifications
You must be signed in to change notification settings - Fork 28
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 6 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,50 @@ | ||
import { NextResponse } from "next/server"; | ||
import { connectToDatabase } from "@/lib/mongoose"; | ||
import Paper from "@/db/papers"; | ||
|
||
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(); | ||
|
||
const subjects: string[] = body; | ||
|
||
const usersPapers = await Paper.find({ | ||
subject: { $in: subjects }, | ||
}); | ||
|
||
const transformedPapers = usersPapers.reduce((acc, 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; | ||
}, []); | ||
|
||
// check duplicates | ||
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 |
---|---|---|
|
@@ -15,7 +15,11 @@ import { | |
import Autoplay from "embla-carousel-autoplay"; | ||
import { chunkArray } from "@/util/utils"; | ||
|
||
function StoredPapers() { | ||
function PapersCarousel({ | ||
carouselType, | ||
}: { | ||
carouselType: "users" | "default"; | ||
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. Nice work in here, just change it to upcoming and users and make the default as upcoming using carouselType="default" |
||
}) { | ||
const [displayPapers, setDisplayPapers] = useState<IUpcomingPaper[]>([]); | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [chunkSize, setChunkSize] = useState<number>(4); | ||
|
@@ -29,6 +33,17 @@ function StoredPapers() { | |
} | ||
}; | ||
|
||
localStorage.setItem( | ||
"userSubjects", | ||
JSON.stringify([ | ||
"Information Security [CBS3002]", | ||
"Foundations of Data Analytics [BCSE351E]", | ||
"Design and Analysis of Algorithms [MCSE502L]", | ||
"Complex Variables and Linear Algebra [BMAT201L]", | ||
"Differential Equations and Transforms [BMAT102L]", | ||
]), | ||
); | ||
|
||
handleResize(); | ||
window.addEventListener("resize", handleResize); | ||
|
||
|
@@ -43,10 +58,18 @@ function StoredPapers() { | |
async function fetchPapers() { | ||
try { | ||
setIsLoading(true); | ||
const response = await axios.get<IUpcomingPaper[]>( | ||
"/api/upcoming-papers", | ||
); | ||
setDisplayPapers(response.data); | ||
if (carouselType === "users") { | ||
const storedSubjects = JSON.parse( | ||
localStorage.getItem("userSubjects"), | ||
); | ||
const response = await axios.post("/api/user-papers", storedSubjects); | ||
setDisplayPapers(response.data); | ||
} else { | ||
const response = await axios.get<IUpcomingPaper[]>( | ||
"/api/upcoming-papers", | ||
); | ||
setDisplayPapers(response.data); | ||
} | ||
} catch (error) { | ||
console.error("Failed to fetch papers:", error); | ||
} finally { | ||
|
@@ -66,7 +89,7 @@ function StoredPapers() { | |
return ( | ||
<div className="px-4"> | ||
<p className="my-8 text-center font-play text-lg font-semibold"> | ||
Upcoming Papers | ||
{carouselType === "users" ? "Your Papers" : "Upcoming Papers"} | ||
</p> | ||
|
||
<div className=""> | ||
|
@@ -107,4 +130,4 @@ function StoredPapers() { | |
); | ||
} | ||
|
||
export default StoredPapers; | ||
export default PapersCarousel; |
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