Skip to content

Commit

Permalink
Merge pull request #117 from pbdsce/fixingItAll
Browse files Browse the repository at this point in the history
feat : refactored all db calls to backend and removed recruitment banner
  • Loading branch information
SkySingh04 authored Nov 22, 2024
2 parents 5e6b09c + c62b335 commit 4807170
Show file tree
Hide file tree
Showing 26 changed files with 735 additions and 976 deletions.
10 changes: 3 additions & 7 deletions app/(default)/achievements/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import { useEffect, useState } from "react";
import axios from "axios";
import { onAuthStateChanged } from "firebase/auth";
import { auth } from "@/Firebase";
import { db } from "@/Firebase";
import { doc, getDoc } from "firebase/firestore";
import Image from "next/image";
import AchievementCard from "@/components/AchievementCard";

interface Achiever {
Expand Down Expand Up @@ -40,9 +37,9 @@ export default function AchievementsPage() {
if (user) {
const uid = user.uid;
try {
const adminDocRef = await doc(db, "admin", uid);
const adminDocSnap = await getDoc(adminDocRef);
if (adminDocSnap.exists()) {
const resp = await fetch(`/api/admin?uid=${uid}`);
const data = await resp.json();
if (data.isAdmin) {
setIsAdmin(true);
}
} catch (error) {
Expand Down Expand Up @@ -168,7 +165,6 @@ export default function AchievementsPage() {
}

const response = await axios.put("/api/achievements", formData);
console.log(response.data);
setIsEditModalOpen(false);
} catch (error) {
console.error("Error updating achievements:", error);
Expand Down
71 changes: 71 additions & 0 deletions app/(default)/api/admin/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { db } from "@/Firebase";
import {
doc,
getDoc,
setDoc
} from "firebase/firestore";
import { NextResponse } from "next/server";


export async function GET(request: Request) {
try {
const { searchParams } = new URL(request.url);
const uid = searchParams.get("uid");
if (!uid) {
return NextResponse.json(
{ error: "UID is required" },
{ status: 400 }
);
}
const adminDocRef = doc(db, "admin", uid);
const adminDocSnap = await getDoc(adminDocRef);
if (!adminDocSnap.exists()) {
return NextResponse.json(
{ error: "User is not an admin" , isAdmin: false },
{ status: 403 }
);
}
else {
return NextResponse.json(
{ message: "User is an admin" , isAdmin: true },
{ status: 200 }
);
}
} catch (error) {
if (error instanceof Error) {
console.error("Error details:", error.message);
return NextResponse.json(
{ error: "An error occurred", details: error.message },
{ status: 500 }
);
} else {
console.error("Unknown error:", error);
return NextResponse.json(
{ error: "An unknown error occurred" },
{ status: 500 }
);
}
}
}

export async function POST(request: Request) {
const { email, role , userId} = await request.json();
try{await setDoc(doc(db, 'admin', userId), {
email,
role,
});}catch (error) {
if (error instanceof Error) {
console.error("Error details:", error.message);
return NextResponse.json(
{ error: "An error occurred", details: error.message },
{ status: 500 }
);
} else {
console.error("Unknown error:", error);
return NextResponse.json(
{ error: "An unknown error occurred" },
{ status: 500 }
);
}
}
}
148 changes: 148 additions & 0 deletions app/(default)/api/events/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { db } from "@/Firebase";
import {
getDocs,
collection,
updateDoc,
doc,
deleteDoc,
setDoc
} from "firebase/firestore";
import { NextResponse } from "next/server";
import { v4 as uuidv4 } from 'uuid';


export async function GET(request: Request) {
try {
const eventsCollection = collection(db, "events");
const eventSnapshot = await getDocs(eventsCollection);
const data = eventSnapshot.docs.map((doc) => doc.data());
const eventsList = data.map((event : any) => {
return {
id: event.id,
eventName: event.eventName,
description: event.description,
eventDate: event.eventDate,
lastDateOfRegistration: event.lastDateOfRegistration,
dateCreated: event.dateCreated,
dateModified: event.dateModified,
imageURL: event.imageURL,
registrationLink: event.registrationLink,
};
});
return NextResponse.json({ events: eventsList }, { status: 200 });
} catch (error) {
if (error instanceof Error) {
console.error("Error details:", error.message);
return NextResponse.json(
{ error: "An error occurred", details: error.message },
{ status: 500 }
);
} else {
console.error("Unknown error:", error);
return NextResponse.json(
{ error: "An unknown error occurred" },
{ status: 500 }
);
}
}
}

export async function POST(request: Request) {
try {
const newEvent = await request.json();
const eventId = uuidv4();

const currentDate = new Date().toISOString();
const { eventName, eventDate, lastDateOfRegistration, description, imageURL, registrationLink } = newEvent;
await setDoc( doc(db , "events" , eventId), {
id : eventId,
eventName,
eventDate,
lastDateOfRegistration,
description,
imageURL,
registrationLink,
dateCreated: currentDate,
dateModified: currentDate,
});
return NextResponse.json({ id: eventId }, { status: 201 });
} catch (error) {
if (error instanceof Error) {
console.error("Error details:", error.message);
return NextResponse.json(
{ error: "An error occurred", details: error.message },
{ status: 500 }
);
} else {
console.error("Unknown error:", error);
return NextResponse.json(
{ error: "An unknown error occurred" },
{ status: 500 }
);
}
}
}

export async function PUT(request: Request) {
try{
const { searchParams } = new URL(request.url);
const eventid = searchParams.get("eventid");
if (!eventid) {
return NextResponse.json(
{ error: "Event ID is required" },
{ status: 400 }
);
}
const updatedEvent = await request.json();
await updateDoc(doc(db, "events", eventid), updatedEvent);
return NextResponse.json({ id: eventid }, { status: 200 });

}catch (error) {
if (error instanceof Error) {
console.error("Error details:", error.message);
return NextResponse.json(
{ error: "An error occurred", details: error.message },
{ status: 500 }
);
} else {
console.error("Unknown error:", error);
return NextResponse.json(
{ error: "An unknown error occurred" },
{ status: 500 }
);
}
}
}

export async function DELETE(request: Request) {
try{
const { searchParams } = new URL(request.url);
const eventid = searchParams.get("eventid");
if (!eventid) {
return NextResponse.json(
{ error: "Event ID is required" },
{ status: 400 }
);
}
await deleteDoc(doc(db, "events", eventid));
}
catch (error) {
if (error instanceof Error) {
console.error("Error details:", error.message);
return NextResponse.json(
{ error: "An error occurred", details: error.message },
{ status: 500 }
);
} else {
console.error("Unknown error:", error);
return NextResponse.json(
{ error: "An unknown error occurred" },
{ status: 500 }
);
}
}
}




Loading

0 comments on commit 4807170

Please sign in to comment.