-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from pbdsce/fixingItAll
feat : refactored all db calls to backend and removed recruitment banner
- Loading branch information
Showing
26 changed files
with
735 additions
and
976 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
|
Oops, something went wrong.