Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
KBui4 committed Dec 13, 2024
2 parents c18b7c3 + 52ba777 commit 9da05a9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
7 changes: 7 additions & 0 deletions app/(pages)/Donations/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ export default function DonationsList() {
const response = await fetch("/api/donations", {
method: "GET",
});

if (!response.ok) {
const errorData = await response.json();
const message = errorData?.message || "Something went wrong";
throw new Error(message);
}

const result = await response.json();

setData(result.data);
Expand Down
7 changes: 7 additions & 0 deletions app/(pages)/Donors/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ export default function DonorsList() {
const response = await fetch("/api/donors", {
method: "GET",
});

if (!response.ok) {
const errorData = await response.json();
const message = errorData?.message || "Something went wrong";
throw new Error(message);
}

const result = await response.json();

setData(result.data);
Expand Down
9 changes: 7 additions & 2 deletions app/api/donations/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ export async function POST(req: NextRequest) {

// Read
export async function GET() {
const data = await prisma.donation.findMany();
try {
const data = await prisma.donation.findMany();

return NextResponse.json({ message: "Successful fetch", data: data }, { status: 200 });
return NextResponse.json({ message: "Successful fetch", data: data }, { status: 200 });
} catch (error) {
console.error("Error fetching donations:", error);
return NextResponse.json({ message: "Failed to fetch donations", error: error }, { status: 500 });
}
}
9 changes: 7 additions & 2 deletions app/api/donors/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ export async function POST(req: NextRequest) {

// Read
export async function GET() {
const data = await prisma.donor.findMany();
try {
const data = await prisma.donor.findMany();

return NextResponse.json({ message: "GET REQUEST", data: data }, { status: 200 });
return NextResponse.json({ message: "Successful fetch", data: data }, { status: 200 });
} catch (error) {
console.error("Error fetching donations:", error);
return NextResponse.json({ message: "Failed to fetch donors", error: error }, { status: 500 });
}
}

0 comments on commit 9da05a9

Please sign in to comment.