-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
1,743 additions
and
619 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 |
---|---|---|
@@ -1,29 +1,118 @@ | ||
import { NextResponse } from 'next/server'; | ||
import { db } from '@/Firebase'; | ||
import { collection, addDoc } from 'firebase/firestore'; | ||
import { | ||
collection, addDoc, getDocs, DocumentData, | ||
DocumentSnapshot, | ||
doc, | ||
updateDoc, | ||
deleteDoc | ||
} from 'firebase/firestore'; | ||
|
||
export async function GET() { | ||
try { | ||
const querySnapshot = await getDocs(collection(db, "members")); | ||
const membersRaw = querySnapshot.docs.map( | ||
(doc: DocumentSnapshot<DocumentData>) => ({ | ||
id: doc.id, | ||
...doc.data(), | ||
}) | ||
); | ||
|
||
const members = membersRaw.map((member: any) => ({ | ||
id: member.id, | ||
name: member.name, | ||
domain: member.domain, | ||
company: member.company || '', | ||
year: member.year, | ||
})); | ||
|
||
return NextResponse.json(members); | ||
} catch (error) { | ||
console.error("Error fetching members:", error); | ||
return NextResponse.json( | ||
{ | ||
error: "An error occurred while fetching members", | ||
details: error, | ||
}, | ||
{ status: 500 } | ||
); | ||
} | ||
} | ||
|
||
export async function POST(request: Request) { | ||
try { | ||
const data = await request.json(); | ||
|
||
if (!Array.isArray(data)) { | ||
return NextResponse.json({ message: 'Expected an array of JSON objects', error: true }); | ||
if (!data.name || !data.domain || !data.year) { | ||
return NextResponse.json( | ||
{ message: 'Missing required fields', error: true }, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
const savedData = []; | ||
for (const item of data) { | ||
// Save each item to Firebase Firestore | ||
const docRef = await addDoc(collection(db, "members"), item); | ||
console.log("Document written with ID: ", docRef.id); | ||
const docRef = await addDoc(collection(db, "members"), data); | ||
console.log("Document written with ID: ", docRef.id); | ||
|
||
return NextResponse.json( | ||
{ message: 'Member added successfully', data }, | ||
{ status: 201 } | ||
); | ||
} catch (error) { | ||
console.error('Error adding member:', error); | ||
return NextResponse.json( | ||
{ message: 'Failed to add member', error: error }, | ||
{ status: 500 } | ||
); | ||
} | ||
} | ||
|
||
export async function PUT(request: Request) { | ||
try { | ||
const data = await request.json(); | ||
const { id, ...updatedData } = data; | ||
|
||
if (!id || !updatedData.name || !updatedData.domain || !updatedData.year) { | ||
return NextResponse.json( | ||
{ message: 'Missing required fields or member ID', error: true }, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
const memberRef = doc(db, "members", id); // Use the document ID | ||
await updateDoc(memberRef, updatedData); | ||
|
||
return NextResponse.json( | ||
{ message: 'Member updated successfully', data: updatedData }, | ||
{ status: 200 } | ||
); | ||
} catch (error) { | ||
console.error('Error updating member:', error); | ||
return NextResponse.json( | ||
{ message: 'Failed to update member', error }, | ||
{ status: 500 } | ||
); | ||
} | ||
} | ||
|
||
|
||
export async function DELETE(request: Request) { | ||
try { | ||
const { id } = await request.json(); | ||
|
||
// Collect saved data for response | ||
savedData.push({ id: docRef.id, ...item }); | ||
if (!id) { | ||
return NextResponse.json( | ||
{ message: 'Missing member ID', error: true }, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
// Return a success response | ||
return NextResponse.json({ message: 'Registration successful', data: savedData }); | ||
await deleteDoc(doc(db, 'members', id)); | ||
return NextResponse.json({ message: 'Member deleted successfully' }, { status: 200 }); | ||
} catch (error) { | ||
console.error('An error occurred:', error); | ||
return NextResponse.json({ message: 'An error occurred', error }); | ||
console.error('Error deleting member:', error); | ||
return NextResponse.json( | ||
{ message: 'Failed to delete member', error: error }, | ||
{ status: 500 } | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.