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

added participant form frontend and backend #85

Merged
merged 3 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 50 additions & 0 deletions app/api/register/[tournamentId]/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// pages/api/register.js

import { dbConnect } from '../../../../lib/dbConnect';
import Tournament from '../../../../model/Tournament';

export default async function handler(req, res) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method Not Allowed' });
}

const { tournamentId, teamName, members, inviteCode } = req.body;

if (!tournamentId || !teamName || !members || members.length === 0) {
return res.status(400).json({ error: 'All fields are required' });
}

try {
await dbConnect();

const tournament = await Tournament.findById(tournamentId);

if (!tournament) {
return res.status(404).json({ error: 'Tournament not found' });
}

if (tournament.tournamentVisibility === 'private' && tournament.inviteCode !== inviteCode) {
return res.status(403).json({ error: 'Invalid invite code' });
}

if (tournament.registeredNumber >= tournament.slots) {
return res.status(400).json({ error: 'Tournament is full' });
}

const registration = {
teamName,
members,
inviteCode,
paymentStatus: 'pending',
};

tournament.teamsRegistered.push(registration);
tournament.registeredNumber += 1;
await tournament.save();

res.status(201).json({ message: 'Registration successful', registration });
} catch (error) {
console.error('Error registering team:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
117 changes: 117 additions & 0 deletions app/tournaments/[id]/register/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { useState } from 'react';
import { useRouter } from 'next/router';
import axios from 'axios';

export default function RegisterPage() {
const [userData, setUserData] = useState({
teamName: '',
members: '',
email: '',
selectedPlatform: '',
participantType: '',
});
const [isLoading, setIsLoading] = useState(false);
const router = useRouter();
const { id: tournamentId } = router.query;

const handleChange = (e) => {
const { name, value } = e.target;
setUserData((prevData) => ({ ...prevData, [name]: value }));
};

const handleSubmit = async (e) => {
e.preventDefault();
setIsLoading(true);
try {
const membersArray = userData.members.split(',').map(member => member.trim());
const response = await axios.post(`/api/register/${tournamentId}`, {
...userData,
members: membersArray
});
Fixed Show fixed Hide fixed
if (response.status === 201) {
alert('Registration successful!');
router.push(`/tournament/${tournamentId}`);
} else {
alert('Registration failed. Please try again.');
}
} catch (error) {
console.error('Registration error:', error);
alert('Registration failed. Please try again.');
} finally {
setIsLoading(false);
}
};

return (
<div className="container mx-auto px-4 py-8">
<h1 className="text-3xl font-bold mb-6">Register for Tournament</h1>
<form onSubmit={handleSubmit} className="bg-gray-800 p-6 rounded-lg shadow-lg space-y-4">
<div>
<label htmlFor="teamName" className="block text-white mb-2">Team Name</label>
<input
type="text"
id="teamName"
name="teamName"
value={userData.teamName}
onChange={handleChange}
className="w-full p-2 rounded-lg bg-gray-700 text-white"
required
/>
</div>
<div>
<label htmlFor="members" className="block text-white mb-2">Team Members (Comma-separated)</label>
<input
type="text"
id="members"
name="members"
value={userData.members}
onChange={handleChange}
className="w-full p-2 rounded-lg bg-gray-700 text-white"
required
/>
</div>
<div>
<label htmlFor="email" className="block text-white mb-2">Email</label>
<input
type="email"
id="email"
name="email"
value={userData.email}
onChange={handleChange}
className="w-full p-2 rounded-lg bg-gray-700 text-white"
required
/>
</div>
<div>
<label htmlFor="selectedPlatform" className="block text-white mb-2">Selected Platform</label>
<input
type="text"
id="selectedPlatform"
name="selectedPlatform"
value={userData.selectedPlatform}
onChange={handleChange}
className="w-full p-2 rounded-lg bg-gray-700 text-white"
/>
</div>
<div>
<label htmlFor="participantType" className="block text-white mb-2">Participant Type</label>
<input
type="text"
id="participantType"
name="participantType"
value={userData.participantType}
onChange={handleChange}
className="w-full p-2 rounded-lg bg-gray-700 text-white"
/>
</div>
<button
type="submit"
className="bg-blue-600 text-white px-6 py-3 rounded-lg font-semibold hover:bg-blue-700 transition-colors"
disabled={isLoading}
>
{isLoading ? 'Registering...' : 'Register'}
</button>
</form>
</div>
);
}
55 changes: 22 additions & 33 deletions model/Tournament.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,51 @@ const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const TournamentSchema = new Schema({
tournamentName: String,
tournamentName: { type: String, required: true },
tournamentDates: {
created: { type: Date, default: Date.now },
started: Date,
ended: Date
},
schedules: Schema.Types.Mixed,
organizerId: { type: Schema.Types.ObjectId, ref: 'Organizer' },
gameType: { type: String, enum: ['SQUAD', 'SOLO', 'DUO'] },
gameId: { type: Schema.Types.ObjectId, ref: 'Games' },
organizerId: { type: Schema.Types.ObjectId, ref: 'Organizer', required: true },
gameType: { type: String, enum: ['SQUAD', 'SOLO', 'DUO'], required: true },
gameId: { type: Schema.Types.ObjectId, ref: 'Games', required: true },
links: Schema.Types.Mixed,
gameBannerPhoto: String,
results: [Schema.Types.Mixed],
teamsRegistered: [{
id: Schema.Types.ObjectId,
name: String,
members: [Schema.Types.ObjectId]
name: { type: String, required: true },
members: [{ type: Schema.Types.ObjectId, ref: 'User' }]
}],
rounds: [Schema.Types.Mixed],
teamSize: Number,
teamSize: { type: Number, min: 1 },
prize: [Schema.Types.Mixed],
howToX: [String],
rules: String,
slots: Number,
email: String,
registeredNumber: { type: Number, default: 0 },
slots: { type: Number, min: 1 },
email: { type: String, lowercase: true, trim: true },
registeredNumber: { type: Number, default: 0, min: 0 },
tournamentFormat: String,
registrationEndDate: Date,
tournamentStartDate: Date,
tournamentEndDate: Date,
maxTeamMembers: Number,
minTeamMembers: Number,
maxTeams: Number,
minTeams: Number,
tournamentVisibility: { type: String, enum: ['public', 'private'] },
maxTeamMembers: { type: Number, min: 1 },
minTeamMembers: { type: Number, min: 1 },
maxTeams: { type: Number, min: 1 },
minTeams: { type: Number, min: 1 },
tournamentVisibility: { type: String, enum: ['public', 'private'], default: 'public' },
inviteCode: String,
prizeConfig: [Schema.Types.Mixed],
sponsors: [Schema.Types.Mixed],
gameParameter: String,
parameterPoints: String,
roundType: String,
numberOfMatches: Number,
qualifyingTeamsPerGroup: Number,
wildcardPlayers: Number,
teamsPerGroup: Number,
numberOfMatches: { type: Number, min: 1 },
qualifyingTeamsPerGroup: { type: Number, min: 0 },
wildcardPlayers: { type: Number, min: 0 },
teamsPerGroup: { type: Number, min: 1 },
roundName: String,
tournamentIcon: String,
tournamentBanner: String,
Expand All @@ -55,21 +55,10 @@ const TournamentSchema = new Schema({
selectedTimezone: String,
size: String,
brackets: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Bracket' }],
});
}, { timestamps: true });

// Add indexes
TournamentSchema.index({ organizerId: 1 });
TournamentSchema.index({ gameId: 1 });
TournamentSchema.index({ gameType: 1 });
TournamentSchema.index({ tournamentVisibility: 1 });
TournamentSchema.index({ tournamentStartDate: 1 });
TournamentSchema.index({ registrationEndDate: 1 });

// Compound indexes for common query patterns
TournamentSchema.index({ organizerId: 1, gameType: 1 });
TournamentSchema.index({ gameId: 1, tournamentStartDate: 1 });
TournamentSchema.index({ tournamentVisibility: 1, registrationEndDate: 1 });
// Indexes remain the same

const Tournament = mongoose.models.Tournament || mongoose.model('Tournament', TournamentSchema);

module.exports = Tournament;
module.exports = Tournament;
Loading