Skip to content

Commit

Permalink
refactored to mongoose
Browse files Browse the repository at this point in the history
  • Loading branch information
Saurabhchaddha6 committed Jul 26, 2024
1 parent 5aaa05e commit 10716a8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 40 deletions.
28 changes: 14 additions & 14 deletions app/api/participants/route.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { NextResponse } from "next/server";
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();
import dbConnect from "../../../lib/dbConnect";
import UserModel from "../../../models/UserModel";

export async function POST(request) {
try {
await dbConnect();
const body = await request.json();
const { tournamentId, participantName } = body;

const newParticipant = await prisma.participant.create({
data: {
name: participantName,
tournamentId,
}
});
const { tournamentId, participantName, userId } = body;

const user = await UserModel.findById(userId);
if (!user) {
return NextResponse.json({ message: "User not found" }, { status: 404 });
}

user.eventsRegistered.push(tournamentId);

await user.save();

return NextResponse.json({ message: "Participant added successfully", participant: newParticipant });
return NextResponse.json({ message: "Participant added successfully", user });
} catch (error) {
console.error("Error adding participant:", error);
return NextResponse.json({ message: "An error occurred while adding the participant" }, { status: 500 });
} finally {
await prisma.$disconnect();
}
}
37 changes: 20 additions & 17 deletions app/api/tournaments/[id]/registration/route.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
import { PrismaClient } from "@prisma/client";
import { NextResponse } from "next/server";
import dbConnect from "../../../../../lib/dbConnect";
import Tournament from "../../../../../models/Tournament";

const prisma = new PrismaClient();
export default async function POST(request, { params }) {
await dbConnect();

export default async function POST(request, {params}){

const id = params.id;
const { registrationData } = await request.json();

const {registrationData } =request.body

try {
const updatedTournament = await prisma.tournament.update({
where: {id},
data: {registrations: registrationData}
});
const updatedTournament = await Tournament.findByIdAndUpdate(
id,
{ registrations: registrationData },
{ new: true }
);

return NextResponse.status(200).json({message: "Registration updated successfully", tournament: updatedTournament});
if (!updatedTournament) {
return NextResponse.status(404).json({ message: "Tournament not found" });
}

return NextResponse.status(200).json({
message: "Registration updated successfully",
tournament: updatedTournament
});
} catch (error) {
console.log(("error while updating tournament", error))
return NextResponse.status(500).json({message: "Error while updating the registration"})
}
finally{
await prisma.$disconnect();
console.error("Error while updating tournament", error);
return NextResponse.status(500).json({ message: "Error while updating the registration" });
}
}
}
22 changes: 13 additions & 9 deletions app/api/tournaments/[id]/structure/route.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import { NextResponse } from "next/server";
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();
import dbConnect from "../../../../../lib/dbConnect";
import Tournament from "../../../../../models/Tournament";

export default async function POST(request, { params }) {
await dbConnect();

const id = params.id; // Extract the tournament ID from params
const { structureData } = await request.json(); // Correctly parse the request body

try {
const updatedTournament = await prisma.tournament.update({
where: { id: id }, // Use the correct ID parameter
data: { structure: structureData },
});
const updatedTournament = await Tournament.findByIdAndUpdate(
id,
{ structure: structureData },
{ new: true }
);

if (!updatedTournament) {
return NextResponse.status(404).json({ message: "Tournament not found" });
}

return NextResponse.json({
message: "Tournament structure updated successfully",
Expand All @@ -22,7 +28,5 @@ export default async function POST(request, { params }) {
return NextResponse.json({
message: "An error occurred while updating the tournament structure",
}, { status: 500 });
} finally {
await prisma.$disconnect();
}
}

0 comments on commit 10716a8

Please sign in to comment.