Skip to content
Closed
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
2 changes: 1 addition & 1 deletion src/app/api/course-list/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextResponse } from "next/server";
import { connectToDatabase } from "@/lib/mongoose";
import { connectToDatabase } from "@/lib/database/mongoose";
import { Course } from "@/db/course";

export const dynamic = "force-dynamic";
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/paper-by-id/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextResponse } from "next/server";
import { connectToDatabase } from "@/lib/mongoose";
import { connectToDatabase } from "@/lib/database/mongoose";
import Paper from "@/db/papers";
import { Types } from "mongoose";

Expand Down
2 changes: 1 addition & 1 deletion src/app/api/papers/count/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextResponse } from "next/server";
import { connectToDatabase } from "@/lib/mongoose";
import { connectToDatabase } from "@/lib/database/mongoose";
import CourseCount from "@/db/course";

export const dynamic = "force-dynamic";
Expand Down
41 changes: 7 additions & 34 deletions src/app/api/papers/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { NextResponse, type NextRequest } from "next/server";
import { connectToDatabase } from "@/lib/mongoose";
import { connectToDatabase } from "@/lib/database/mongoose";
import Paper from "@/db/papers";
import { type IPaper } from "@/interface";
import { escapeRegExp } from "@/lib/utils/regex";
import { extractUniqueValues } from "@/lib/utils/paper-aggregation";

export const dynamic = "force-dynamic";

Expand All @@ -10,10 +12,6 @@ export async function GET(req: NextRequest) {
await connectToDatabase();
const url = req.nextUrl.searchParams;
const subject = url.get("subject");
const escapeRegExp = (text: string) => {
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
};
const escapedSubject = escapeRegExp(subject ?? "");

if (!subject) {
return NextResponse.json(
Expand All @@ -22,42 +20,17 @@ export async function GET(req: NextRequest) {
);
}

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

if (papers.length === 0) {
return NextResponse.json(
{
papers,
unique_years: [],
unique_slots: [],
unique_exams: [],
unique_campuses: [],
unique_semesters: [],
},
{ status: 200 },
);
}

const unique_years = Array.from(new Set(papers.map((paper) => paper.year)));
const unique_slots = Array.from(new Set(papers.map((paper) => paper.slot)));
const unique_exams = Array.from(new Set(papers.map((paper) => paper.exam)));
const unique_campuses = Array.from(
new Set(papers.map((paper) => paper.campus)),
);
const unique_semesters = Array.from(
new Set(papers.map((paper) => paper.semester)),
);
const uniqueValues = extractUniqueValues(papers);

return NextResponse.json(
{
papers,
unique_years,
unique_slots,
unique_exams,
unique_campuses,
unique_semesters,
...uniqueValues,
},
{ status: 200 },
);
Expand All @@ -67,4 +40,4 @@ export async function GET(req: NextRequest) {
{ status: 500 },
);
}
}
}
12 changes: 6 additions & 6 deletions src/app/api/related-subject/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NextResponse, type NextRequest } from "next/server";
import { connectToDatabase } from "@/lib/mongoose";
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";

Expand All @@ -10,20 +11,19 @@ export async function GET(req: NextRequest) {
await connectToDatabase();
const url = req.nextUrl.searchParams;
const subject = url.get("subject");
const escapeRegExp = (text: string) => {
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
};
const escapedSubject = escapeRegExp(subject ?? "");

if (!subject) {
return NextResponse.json(
{ message: "Subject query parameter is required" },
{ status: 400 },
);
}

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

console.log("realted", subjects);

return NextResponse.json(
Expand All @@ -38,4 +38,4 @@ export async function GET(req: NextRequest) {
{ status: 500 },
);
}
}
}
2 changes: 1 addition & 1 deletion src/app/api/request/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextResponse } from "next/server";
import { connectToDatabase } from "@/lib/mongoose";
import { connectToDatabase } from "@/lib/database/mongoose";
import PaperRequest from "@/db/paperRequest";

export async function POST(req: Request) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/selected-papers/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextResponse } from "next/server";
import { connectToDatabase } from "@/lib/mongoose";
import { connectToDatabase } from "@/lib/database/mongoose";
import Paper from "@/db/papers";

export const dynamic = "force-dynamic";
Expand Down
33 changes: 2 additions & 31 deletions src/app/api/subscribe/route.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
import { NextResponse } from "next/server";
import { google, sheets_v4 } from "googleapis";
import { JWT } from "google-auth-library";

const SCOPES = ["https://www.googleapis.com/auth/spreadsheets"];
const SHEET_ID = process.env.SHEET_ID;

async function getAuth(): Promise<JWT> {
return new google.auth.JWT({
email: process.env.GOOGLE_CLIENT_EMAIL,
key: process.env.GOOGLE_PRIVATE_KEY?.replace(/\\n/g, "\n"),
scopes: SCOPES,
});
}

async function appendEmailToSheet(email: string) {
const authClient = await getAuth();
const sheets: sheets_v4.Sheets = google.sheets({
version: "v4",
auth: authClient,
});

await sheets.spreadsheets.values.append({
spreadsheetId: SHEET_ID,
range: "Sheet1!A:A",
valueInputOption: "RAW",
requestBody: {
values: [[email]],
},
});
}
import { appendEmailToSheet } from "@/lib/services/google-sheets";

export async function POST(req: Request) {
try {
Expand All @@ -44,4 +15,4 @@ export async function POST(req: Request) {
console.error("Error adding email:", error);
return NextResponse.json({ error: "Failed to add email" }, { status: 500 });
}
}
}
16 changes: 7 additions & 9 deletions src/app/api/upcoming-papers/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NextResponse } from "next/server";
import { connectToDatabase } from "@/lib/mongoose";
import { connectToDatabase } from "@/lib/database/mongoose";
import UpcomingSlot from "@/db/upcoming-slot";
import UpcomingSubject from "@/db/upcoming-paper";
import { calculateCorrespondingSlots } from "@/lib/utils/slot-calculation";

export const dynamic = "force-dynamic";

Expand All @@ -10,6 +11,7 @@ export async function GET() {
await connectToDatabase();
const upcomingSlot = await UpcomingSlot.find();
const slot = upcomingSlot[0]?.slot;

if (!slot) {
return NextResponse.json(
{
Expand All @@ -18,16 +20,12 @@ export async function GET() {
{ status: 404 },
);
}
const nextSlot = String.fromCharCode(slot.charCodeAt(0) + 1);
const correspondingSlots = [
slot + "1",
slot + "2",
nextSlot + "1",
nextSlot + "2",
];

const correspondingSlots = calculateCorrespondingSlots(slot);
const selectedSubjects = await UpcomingSubject.find({
slots: { $in: correspondingSlots },
});

if (selectedSubjects.length === 0) {
return NextResponse.json(
{
Expand All @@ -49,4 +47,4 @@ export async function GET() {
{ status: 500 },
);
}
}
}
6 changes: 3 additions & 3 deletions src/app/api/upload/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NextResponse } from "next/server";
import { connectToDatabase } from "@/lib/mongoose";
import { connectToDatabase } from "@/lib/database/mongoose";
import { PaperAdmin } from "@/db/papers";
import { createPDFfromImages } from "@/lib/pdf";
import { uploadPDF, uploadThumbnail } from "@/lib/storage";
import { createPDFfromImages } from "@/lib/storage/pdf";
import { uploadPDF, uploadThumbnail } from "@/lib/storage/gcp";

export const runtime = "nodejs";

Expand Down
25 changes: 5 additions & 20 deletions src/app/api/user-papers/route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { NextResponse } from "next/server";
import { connectToDatabase } from "@/lib/mongoose";
import { connectToDatabase } from "@/lib/database/mongoose";
import Paper from "@/db/papers";
import { StoredSubjects, TransformedPaper } from "@/interface";
import { StoredSubjects } from "@/interface";
import { transformPapersToSubjectSlots } from "@/lib/services/paper-transform";

export const dynamic = "force-dynamic";


export async function POST(req: Request) {
try {
await connectToDatabase();
Expand All @@ -17,22 +17,7 @@ export async function POST(req: Request) {

console.log("Fetched user papers:", usersPapers);

const transformedPapers = usersPapers.reduce<TransformedPaper[]>(
(acc, paper) => {
const existing = acc.find((item) => item.subject === paper.subject);

if (existing) {
if (!existing.slots.includes(paper.slot)) {
existing.slots.push(paper.slot);
}
} else {
acc.push({ subject: paper.subject, slots: [paper.slot] });
}

return acc;
},
[],
);
const transformedPapers = transformPapersToSubjectSlots(usersPapers);

return NextResponse.json(transformedPapers, {
status: 200,
Expand All @@ -46,4 +31,4 @@ export async function POST(req: Request) {
{ status: 500 },
);
}
}
}
2 changes: 1 addition & 1 deletion src/app/paper/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PdfViewer from "@/components/pdfViewer";
import RelatedPapers from "@/components/RelatedPaper";
import Loader from "@/components/ui/loader";
import { type ErrorResponse, type PaperResponse } from "@/interface";
import { extractBracketContent } from "@/util/utils";
import { extractBracketContent } from "@/lib/utils/string";
import axios, { type AxiosResponse } from "axios";
import { type Metadata } from "next";
import { redirect } from "next/navigation";
Expand Down
4 changes: 2 additions & 2 deletions src/app/upload/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Dropzone from "react-dropzone";
import { Upload, XIcon } from "lucide-react";
import { getDocument, GlobalWorkerOptions } from "pdfjs-dist";

GlobalWorkerOptions.workerSrc =
GlobalWorkerOptions.workerSrc =
"https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.8.69/pdf.worker.min.mjs";

interface APIResponse {
Expand Down Expand Up @@ -288,7 +288,7 @@ export default function Page() {
setIsUploading(false);
}
};

return (
<main className="mx-auto max-w-3xl px-4 py-8">
<div className="flex h-[calc(100vh-90px)] flex-col justify-center px-6 font-play">
Expand Down
14 changes: 7 additions & 7 deletions src/components/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { type IPaper } from "@/interface";
import Image from "next/image";
import { Eye, Download, Check } from "lucide-react";
import {
capsule,
extractBracketContent,
extractWithoutBracketContent,
} from "@/util/utils";
} from "@/lib/utils/string";
import {
getSecureUrl,
generateFileName,
downloadFile,
} from "@/util/download_paper";
} from "@/lib/utils/download";
import { Capsule } from "@/components/ui/capsule";
import Link from "next/link";
import { cn } from "@/lib/utils";

Expand Down Expand Up @@ -88,10 +88,10 @@ const Card = ({ paper, onSelect, isSelected }: CardProps) => {
{extractWithoutBracketContent(paper.subject)}
</div>
<div className="flex flex-wrap gap-2">
{capsule(paper.exam)}
{capsule(paper.slot)}
{capsule(paper.year)}
{capsule(paper.semester)}
<Capsule>{paper.exam}</Capsule>
<Capsule>{paper.slot}</Capsule>
<Capsule>{paper.year}</Capsule>
<Capsule>{paper.semester}</Capsule>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/components/CatalogueContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
getSecureUrl,
generateFileName,
downloadFile,
} from "@/util/download_paper";
} from "@/lib/utils/download";
import type { ICourses } from "@/interface";
import JSZip from "jszip";
import { toast } from "react-hot-toast";
Expand Down
Loading