From 84cdd8f0dee8cdcd8e070fff4c25d6dd15fa609b Mon Sep 17 00:00:00 2001 From: saurabhchaddha6 Date: Sun, 28 Jul 2024 23:29:41 +0530 Subject: [PATCH] updated tournament model and tournament route --- app/api/tournaments/route.js | 96 +++++++++++++++++++++++++++++++----- model/Tournament.js | 30 ++++++++++- 2 files changed, 113 insertions(+), 13 deletions(-) diff --git a/app/api/tournaments/route.js b/app/api/tournaments/route.js index b674e26..a08f897 100644 --- a/app/api/tournaments/route.js +++ b/app/api/tournaments/route.js @@ -1,5 +1,5 @@ import { NextResponse } from 'next/server'; -import dbConnect from "../../../lib/dbConnect"; +import dbConnect from '../../../lib/dbConnect'; import Tournament from '../../../model/Tournament'; import Games from '../../../model/Games'; import Organizer from '../../../model/Organizer'; @@ -20,12 +20,6 @@ export async function GET(request) { if (organizerId) query.organizerId = organizerId; try { - console.log('Fetching tournaments with params:', { page, limit, gameType, organizerId }); - - // Ensure models are registered - Games; - Organizer; - const tournaments = await Tournament.find(query) .populate('gameId', 'name category gameBannerPhoto') .populate('organizerId', 'orgName bannerPhoto') @@ -35,18 +29,98 @@ export async function GET(request) { const total = await Tournament.countDocuments(query); - console.log(`Found ${tournaments.length} tournaments out of ${total} total`); - return NextResponse.json({ tournaments, totalPages: Math.ceil(total / limit), currentPage: page, }); } catch (error) { - console.error('Error fetching tournaments:', error); return NextResponse.json({ error: 'Internal Server Error', details: error.message }, { status: 500 }); } -} \ No newline at end of file +} + +export async function POST(request) { + await dbConnect(); + + const { + tournamentName, + tournamentFormat, + registrationEndDate, + tournamentStartDate, + tournamentEndDate, + maxTeamMembers, + minTeamMembers, + maxTeams, + minTeams, + tournamentVisibility, + inviteCode, + prizeConfig, + rules, + sponsors, + gameParameter, + parameterPoints, + roundType, + numberOfMatches, + qualifyingTeamsPerGroup, + wildcardPlayers, + teamsPerGroup, + roundName, + tournamentIcon, + tournamentBanner, + selectedPlatform, + participantType, + selectedTimezone, + size, + gameId, + organizerId + } = await request.json(); + + try { + const newTournament = new Tournament({ + tournamentName, + tournamentFormat, + registrationEndDate, + tournamentStartDate, + tournamentEndDate, + maxTeamMembers, + minTeamMembers, + maxTeams, + minTeams, + tournamentVisibility, + inviteCode, + prizeConfig, + rules, + sponsors, + gameParameter, + parameterPoints, + roundType, + numberOfMatches, + qualifyingTeamsPerGroup, + wildcardPlayers, + teamsPerGroup, + roundName, + tournamentIcon, + tournamentBanner, + selectedPlatform, + participantType, + selectedTimezone, + size, + gameId, + organizerId + }); + + await newTournament.save(); + + return NextResponse.json({ + message: 'Tournament created successfully' + }, { status: 201 }); + } catch (error) { + return NextResponse.json({ + error: 'Internal Server Error', + details: error.message + }, { status: 500 }); + } +} diff --git a/model/Tournament.js b/model/Tournament.js index afb7055..145c2f0 100644 --- a/model/Tournament.js +++ b/model/Tournament.js @@ -27,9 +27,35 @@ const TournamentSchema = new Schema({ rules: String, slots: Number, email: String, - registeredNumber: { type: Number, default: 0 } + registeredNumber: { type: Number, default: 0 }, + tournamentFormat: String, + registrationEndDate: Date, + tournamentStartDate: Date, + tournamentEndDate: Date, + maxTeamMembers: Number, + minTeamMembers: Number, + maxTeams: Number, + minTeams: Number, + tournamentVisibility: { type: String, enum: ['public', 'private'] }, + inviteCode: String, + prizeConfig: [Schema.Types.Mixed], + sponsors: [Schema.Types.Mixed], + gameParameter: String, + parameterPoints: String, + roundType: String, + numberOfMatches: Number, + qualifyingTeamsPerGroup: Number, + wildcardPlayers: Number, + teamsPerGroup: Number, + roundName: String, + tournamentIcon: String, + tournamentBanner: String, + selectedPlatform: String, + participantType: String, + selectedTimezone: String, + size: String }); const Tournament = mongoose.models.Tournament || mongoose.model('Tournament', TournamentSchema); -module.exports = Tournament; \ No newline at end of file +module.exports = Tournament;