diff --git a/app/api/participants/route.js b/app/api/participants/route.js index 3299da5..8c57ac5 100644 --- a/app/api/participants/route.js +++ b/app/api/participants/route.js @@ -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(); } } diff --git a/app/api/tournaments/[id]/registration/route.js b/app/api/tournaments/[id]/registration/route.js index 77608a2..f8bd1b6 100644 --- a/app/api/tournaments/[id]/registration/route.js +++ b/app/api/tournaments/[id]/registration/route.js @@ -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" }); } -} \ No newline at end of file +} diff --git a/app/api/tournaments/[id]/structure/route.js b/app/api/tournaments/[id]/structure/route.js index 995a79f..fc536fa 100644 --- a/app/api/tournaments/[id]/structure/route.js +++ b/app/api/tournaments/[id]/structure/route.js @@ -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", @@ -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(); } }