Skip to content

Commit

Permalink
updated tournament model and tournament route
Browse files Browse the repository at this point in the history
  • Loading branch information
Saurabhchaddha6 committed Jul 28, 2024
1 parent f750679 commit 84cdd8f
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 13 deletions.
96 changes: 85 additions & 11 deletions app/api/tournaments/route.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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')
Expand All @@ -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 });
}
}
}

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 });
}
}
30 changes: 28 additions & 2 deletions model/Tournament.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
module.exports = Tournament;

0 comments on commit 84cdd8f

Please sign in to comment.