Skip to content

Commit

Permalink
Merge pull request #154 from vaibhavgarg25/main
Browse files Browse the repository at this point in the history
feat : transfer database from firebase to mongodb
  • Loading branch information
SkySingh04 authored Dec 28, 2024
2 parents 1b4f9f5 + f26706c commit 9339869
Show file tree
Hide file tree
Showing 10 changed files with 1,231 additions and 377 deletions.
2 changes: 1 addition & 1 deletion app/(default)/achievements/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default function AchievementsPage() {
}
}
});
});
},[]);

useEffect(() => {
async function fetchAchievers() {
Expand Down
214 changes: 70 additions & 144 deletions app/(default)/api/achievements/route.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import { db, storage } from "@/Firebase";
import {
doc,
updateDoc,
getDoc,
collection,
addDoc,
getDocs,
query,
where,
DocumentData,
DocumentSnapshot,
} from "firebase/firestore";
import { ref, uploadBytes, getDownloadURL } from "firebase/storage";
import { NextRequest, NextResponse } from "next/server";
import { ref, uploadBytes, getDownloadURL } from "firebase/storage";
import connectMongoDB from "@/lib/dbConnect";
import Achievementmodel from "@/models/Achievements";
import { db, storage } from "@/Firebase";

// POST method: Create or add a new achievement
export async function POST(request: Request) {
try {
await connectMongoDB();
const formData = await request.formData();

// Extract data from the form
Expand All @@ -25,26 +17,19 @@ export async function POST(request: Request) {
const portfolio = formData.get("portfolio") as string;
const internship = formData.get("internship") as string;
const companyPosition = formData.get("companyPosition") as string;
const achievements = JSON.parse(
formData.get("achievements") as string
) as string[];
const achievements = JSON.parse(formData.get("achievements") as string) as string[];
const image = formData.get("image") as File;

// Check if a person with the same name already exists
const existingMembersQuery = query(
collection(db, "achievements"),
where("Name", "==", name)
);
const querySnapshot = await getDocs(existingMembersQuery);

if (!querySnapshot.empty) {
// Check if a person with the same name already exists in MongoDB
const existingMember = await Achievementmodel.findOne({ name });
if (existingMember) {
return NextResponse.json(
{ error: `A member with the name ${name} already exists.` },
{ status: 400 }
);
}

// Handle image upload
// Handle image upload to Firebase Storage
if (!image) {
return NextResponse.json(
{ error: "Image file is required" },
Expand All @@ -56,180 +41,121 @@ export async function POST(request: Request) {
await uploadBytes(storageRef, image);
const imageUrl = await getDownloadURL(storageRef);

// Save data to Firestore without Timestamp
const docRef = await addDoc(collection(db, "achievements"), {
Name: name,
Email: email,
Batch: batch,
Portfolio: portfolio,
Internship: internship,
CompanyPosition: companyPosition,
achievements: achievements,
imageUrl: imageUrl,
});
return NextResponse.json({
id: docRef.id,
imageUrl: imageUrl,

const newAchievement = new Achievementmodel({
name,
email,
batch,
portfolio,
internship,
companyPosition,
achievements,
imageUrl,
});

const result = await newAchievement.save();
return NextResponse.json(result);
} 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 }
);
}
console.error("Error creating member:", error);
return NextResponse.json(
{ error: "An error occurred", details:(error as Error).message},
{ status: 500 }
);
}
}

// GET method: Fetch achievements based on name or fetch all if no name is provided
export async function GET(request: NextRequest) {
await connectMongoDB();

try {
const { searchParams } = new URL(request.url);
const name = searchParams.get("name");

let querySnapshot;

if (name) {
// Query Firestore by name to find the document
const memberQuery = query(
collection(db, "achievements"),
where("Name", "==", name)
);
querySnapshot = await getDocs(memberQuery);
querySnapshot = await Achievementmodel.find({ name });
} else {
// Fetch all documents from the "achievements" collection
querySnapshot = await getDocs(collection(db, "achievements"));
querySnapshot = await Achievementmodel.find();
}

// Map through the documents and extract the data
const membersRaw = querySnapshot.docs.map(
(doc: DocumentSnapshot<DocumentData>) => ({
id: doc.id,
...doc.data(),
})
);

const members = membersRaw.map((member: any) => {
const members = querySnapshot.map((member: any) => {
return {
id: member.id,
name: member.Name,
email: member.Email,
batch: member.Batch,
portfolio: member.Portfolio,
internship: member.Internship,
companyPosition: member.CompanyPosition,
achievements: member.achievements,
imageUrl: member.imageUrl,
id: member._id,
name: member.name,
email: member.email || null,
batch: member.batch || null,
portfolio: member.portfolio || null,
internship: member.internship || null,
companyPosition: member.companyPosition || null,
achievements: member.achievements || [],
imageUrl: member.imageUrl || null,
};
});

// Return the members data
return NextResponse.json(members);
} catch (error) {
if (error instanceof Error) {
console.error("Error fetching members:", error.message);
return NextResponse.json(
{
error: "An error occurred while fetching members",
details: error.message,
},
{ status: 500 }
);
} else {
console.error("Unknown error:", error);
return NextResponse.json(
{ error: "An unknown error occurred while fetching members" },
{ status: 500 }
);
}
console.error("Error fetching members:", error);
return NextResponse.json(
{ error: "An error occurred while fetching members", details: (error as Error).message },
{ status: 500 }
);
}
}

// PUT method: Update an existing achievement based on name
export async function PUT(request: Request) {
try {
const formData = await request.formData();
const name = formData.get("name") as string;

// Fetch the existing document
const memberQuery = query(
collection(db, "achievements"),
where("Name", "==", name)
);
const querySnapshot = await getDocs(memberQuery);

if (querySnapshot.empty) {
// Fetch the existing document by name
const existingMember = await Achievementmodel.findOne({ name });
if (!existingMember) {
return NextResponse.json(
{ error: `No member found with the name ${name}` },
{ status: 404 }
);
}

const docRef = querySnapshot.docs[0].ref;
const existingData = querySnapshot.docs[0].data();

// Extract data from the form
const email = (formData.get("email") as string) || existingData.Email;
const batch = (formData.get("batch") as string) || existingData.Batch;
const portfolio =
(formData.get("portfolio") as string) || existingData.Portfolio;
const internship =
(formData.get("internship") as string) || existingData.Internship;
const companyPosition =
(formData.get("companyPosition") as string) ||
existingData.CompanyPosition;
// Extract data from the form, using existing values if new data is not provided
const email = (formData.get("email") as string) || existingMember.email;
const batch = (formData.get("batch") as string) || existingMember.batch;
const portfolio = (formData.get("portfolio") as string) || existingMember.portfolio;
const internship = (formData.get("internship") as string) || existingMember.internship;
const companyPosition = (formData.get("companyPosition") as string) || existingMember.companyPosition;
const achievements = formData.get("achievements")
? JSON.parse(formData.get("achievements") as string)
: existingData.achievements;
: existingMember.achievements;
const image = formData.get("image") as File;

let imageUrl = existingData.imageUrl;
let imageUrl = existingMember.imageUrl;

// Handle image upload if a new image is provided
if (image) {
const storageRef = ref(storage, `images/${image.name}`);
await uploadBytes(storageRef, image);
imageUrl = await getDownloadURL(storageRef);
}

const updateData = {
Name: name,
Email: email,
Batch: batch,
Portfolio: portfolio,
Internship: internship,
CompanyPosition: companyPosition,
achievements: achievements,
imageUrl: imageUrl,
};
// Update the member data
existingMember.email = email;
existingMember.batch = batch;
existingMember.portfolio = portfolio;
existingMember.internship = internship;
existingMember.companyPosition = companyPosition;
existingMember.achievements = achievements;
existingMember.imageUrl = imageUrl;

await updateDoc(docRef, updateData);
await existingMember.save(); // Save the updated document

return NextResponse.json(updateData);
return NextResponse.json(existingMember);
} catch (error) {
if (error instanceof Error) {
console.error("Error updating member:", error.message);
return NextResponse.json(
{ error: "An error occurred while updating", details: error.message },
{ status: 500 }
);
} else {
console.error("Unknown error:", error);
return NextResponse.json(
{ error: "An unknown error occurred while updating" },
{ status: 500 }
);
}
console.error("Error updating member:", error);
return NextResponse.json(
{ error: "An error occurred while updating", details: (error as Error).message },
{ status: 500 }
);
}
}
26 changes: 26 additions & 0 deletions lib/dbConnect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import mongoose from 'mongoose';

const MONGODB_URI = process.env.NEXT_PUBLIC_MONGODB_URI;

type ConnectionObject={
isConnected?:number
}

const connection:ConnectionObject={}

async function connectMongoDB():Promise<void> {
if(connection.isConnected){
console.log("already connected to database")
return
}
try {
const db=await mongoose.connect(MONGODB_URI || '')
connection.isConnected=db.connections[0].readyState
console.log("db connected successfully")
} catch (error) {
console.log("database connection failed",error)
process.exit(0)
}
}

export default connectMongoDB;
56 changes: 56 additions & 0 deletions models/Achievements.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import mongoose, { Document, Schema } from "mongoose";

// Define the Member interface
export interface Achievement extends Document {
name: string;
batch: string | null;
email: string | null;
portfolio: string | null;
internship: string | null;
companyPosition: string | null;
achievements: string[];
imageUrl: string | null;
}

// Create the schema for the Member model
const AchievementSchema: Schema<Achievement> = new Schema(
{
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
batch: {
type: String,
required: true
},
portfolio: {
type: String,
required: true
},
internship: {
type:String,
required: true,
},
companyPosition: {
type: String,
required: true,
},
achievements: {
type: [String], // Array of strings
required: true,
},
imageUrl: {
type: String,
required: true,
},
},
);

// Create a model from the schema
const AchievementModel = mongoose.models.achievements || mongoose.model<Achievement>("achievements", AchievementSchema);

export default AchievementModel;
Loading

0 comments on commit 9339869

Please sign in to comment.