Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated tournament model and tournament route #74

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading