Skip to content

Commit 1ff0a9d

Browse files
added server verification on form data and refactored years dropdown to show years upto current one
1 parent a57db0f commit 1ff0a9d

File tree

3 files changed

+38
-26
lines changed

3 files changed

+38
-26
lines changed

src/app/api/upload/route.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NextRequest, NextResponse } from "next/server";
22
import { PDFDocument } from "pdf-lib";
3-
3+
import {courses, slots, years} from "@/components/select_options"
44
import { connectToDatabase } from "@/lib/mongoose";
55
import cloudinary from "cloudinary";
66
import {
@@ -29,7 +29,14 @@ export async function POST(req: Request) {
2929
const year = formData.get("year") as string;
3030
const exam = formData.get("exam") as string;
3131
const isPdf = formData.get("isPdf") === "true"; // Convert string to boolean
32-
32+
if(!(courses.includes(subject) && slots.includes(slot) && years.includes(year)))
33+
{
34+
return NextResponse.json(
35+
{ message: "Bad Request" },
36+
37+
{ status: 400 },
38+
);
39+
}
3340
await connectToDatabase();
3441
let finalUrl: string | undefined = "";
3542
let public_id_cloudinary: string | undefined = "";

src/app/upload/page.tsx

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import {
2727
} from "@/components/ui/command";
2828
import Navbar from "@/components/Navbar";
2929
import Footer from "@/components/Footer";
30-
import {PostPDFToCloudinary} from "@/interface"
31-
import { courses, slots } from "@/components/select_options";
30+
import { PostPDFToCloudinary } from "@/interface";
31+
import { courses, slots, years } from "@/components/select_options";
3232
import SearchBar from "@/components/searchbarSubjectList";
3333
const Page = () => {
3434
const router = useRouter();
@@ -96,8 +96,7 @@ const Page = () => {
9696
let isPdf = false;
9797
if (files[0]?.type === "application/pdf") {
9898
isPdf = true;
99-
if(files.length > 1)
100-
{
99+
if (files.length > 1) {
101100
toast.error(`PDFs should be uploaded seperately`);
102101
return;
103102
}
@@ -137,8 +136,6 @@ const Page = () => {
137136
error: (err: ApiError) => err.message,
138137
},
139138
);
140-
141-
142139
};
143140

144141
const handleSubjectSelect = (value: string) => {
@@ -199,7 +196,7 @@ const Page = () => {
199196
<div>
200197
<label>Subject:</label>
201198
{/* setSubject */}
202-
<SearchBar setSubject={setSubject}></SearchBar>
199+
<SearchBar setSubject={setSubject}></SearchBar>
203200
{/* <Command className="rounded-lg border shadow-md md:min-w-[450px]">
204201
<CommandInput
205202
value={inputValue}
@@ -208,7 +205,7 @@ const Page = () => {
208205
}
209206
placeholder="Type a subject or search..."
210207
/> */}
211-
{/* <CommandList className="h-[100px]">
208+
{/* <CommandList className="h-[100px]">
212209
<CommandEmpty>No results found.</CommandEmpty>
213210
214211
<CommandGroup heading="Subjects">
@@ -235,21 +232,14 @@ const Page = () => {
235232
<SelectContent>
236233
<SelectGroup>
237234
<SelectLabel>Years</SelectLabel>
238-
{(() => {
239-
const options = [];
240-
for (
241-
let i = 2011;
242-
i <= Number(new Date().getFullYear());
243-
i++
244-
) {
245-
options.push(
246-
<SelectItem key={i} value={String(i)}>
247-
{i}
248-
</SelectItem>,
249-
);
250-
}
251-
return options;
252-
})()}
235+
{years.map((year)=>
236+
{
237+
return (<SelectItem key={year} value={String(year)}>
238+
{year}
239+
</SelectItem>)
240+
241+
}
242+
)}
253243
</SelectGroup>
254244
</SelectContent>
255245
</Select>

src/components/select_options.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,5 +245,20 @@ const slots: string[] = [
245245
"F2",
246246
"G2",
247247
];
248+
function getYears(startYear: number) {
249+
const currentYear = new Date().getFullYear(); // Get the current year
250+
const years = [];
248251

249-
export { slots, courses };
252+
// Loop from startYear to currentYear and add each year to the array
253+
for (let year = startYear; year <= currentYear; year++) {
254+
years.push(String(year));
255+
}
256+
257+
return years;
258+
}
259+
260+
// Example usage:
261+
const startYear = 2011;
262+
const years = getYears(startYear);
263+
264+
export { slots, courses, years };

0 commit comments

Comments
 (0)