Skip to content

Commit

Permalink
Did some UI changes in Blogs
Browse files Browse the repository at this point in the history
  • Loading branch information
Vipul-045 committed Jul 27, 2024
2 parents 1e6dc70 + fdbcc0a commit 76d249a
Show file tree
Hide file tree
Showing 27 changed files with 1,164 additions and 186 deletions.
59 changes: 29 additions & 30 deletions app/api/create-tournament/route.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
import { PrismaClient } from '@prisma/client';
import { NextResponse } from 'next/server';
import dbConnect from '../../../lib/dbConnect';
import Tournament from '../../../model/Tournament';

const prisma = new PrismaClient();
export async function POST(req) {
try {
await dbConnect();

export async function POST(req, res) {
{
const { tournamentName, selectedPlatform, participantType, selectedTimezone, size } = req.body;
const { tournamentName, selectedPlatform, participantType, selectedTimezone, size } = await req.json();

try {
await dbConnect();
const existingTournament = await Tournament.findOne({ tournamentName });

const existingTournament = await prisma.tournament.findFirst({
where: { tournamentName },
});
if (existingTournament) {
return NextResponse.json({ message: 'Tournament already exists' }, { status: 409 });
}

if (existingTournament) {
return res.status(409).json({ message: 'Tournament already exists' });
}
const newTournament = new Tournament({
tournamentName,
platform: selectedPlatform,
participantType,
timezone: selectedTimezone,
size: parseInt(size, 10),
});

const newTournament = await prisma.tournament.create({
data: {
tournamentName,
platform: selectedPlatform,
participantType,
timezone: selectedTimezone,
size: parseInt(size, 10),
},
});
await newTournament.save();

res.status(200).json({ message: 'Tournament created successfully', tournament: newTournament });
} catch (error) {
console.error('Error creating tournament:', error);
res.status(500).json({ message: 'An error occurred while creating the tournament' });
} finally {
await prisma.$disconnect();
}
return NextResponse.json(
{ message: 'Tournament created successfully', tournament: newTournament },
{ status: 201 }
);
} catch (error) {
console.error('Error creating tournament:', error);
return NextResponse.json(
{ message: 'An error occurred while creating the tournament' },
{ status: 500 }
);
}
}
}
24 changes: 7 additions & 17 deletions app/api/games/route.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
import { PrismaClient } from '@prisma/client';
import { NextResponse } from 'next/server';
import dbConnect from '../../../lib/dbConnect';
import Games from '../../../model/Games';

const prisma = new PrismaClient();

export async function GET(req, res) {
export async function GET(req) {
await dbConnect();

try {
const gameData = await prisma.games.findMany();
const existingGames = [];

for (const game of gameData) {
const existingGame = await prisma.games.findUnique({
where: { name: game.name },
});
existingGames.push(existingGame);
}
const gameData = await Games.find().lean();

res.status(200).json(existingGames);
return NextResponse.json(gameData, { status: 200 });
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
} finally {
await prisma.$disconnect();
console.error('Error fetching games:', error);
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
}
}
25 changes: 25 additions & 0 deletions app/api/participants/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { NextResponse } from "next/server";
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, 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", user });
} catch (error) {
console.error("Error adding participant:", error);
return NextResponse.json({ message: "An error occurred while adding the participant" }, { status: 500 });
}
}
30 changes: 30 additions & 0 deletions app/api/tournaments/[id]/registration/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { NextResponse } from "next/server";
import dbConnect from "../../../../../lib/dbConnect";
import Tournament from "../../../../../models/Tournament";

export default async function POST(request, { params }) {
await dbConnect();

const id = params.id;
const { registrationData } = await request.json();

try {
const updatedTournament = await Tournament.findByIdAndUpdate(
id,
{ registrations: registrationData },
{ new: true }
);

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.error("Error while updating tournament", error);
return NextResponse.status(500).json({ message: "Error while updating the registration" });
}
}
27 changes: 16 additions & 11 deletions app/api/tournaments/[id]/route.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { NextResponse } from 'next/server';
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();
import dbConnect from "../../../../lib/dbConnect";
import Tournament from '../../../../model/Tournament';
import Games from '../../../../model/Games';
import Organizer from '../../../../model/Organizer';

export async function GET(request, { params }) {
await dbConnect();

try {
const id = params.id;
const tournament = await prisma.tournament.findUnique({
where: { id },
include: {
game: true,
organizer: true,
},
});

// Ensure all models are registered
Games;
Organizer;

const tournament = await Tournament.findById(id)
.populate('gameId')
.populate('organizerId')
.lean();

if (tournament) {
return NextResponse.json(tournament);
Expand All @@ -21,6 +26,6 @@ export async function GET(request, { params }) {
}
} catch (error) {
console.error('Error fetching tournament:', error);
return NextResponse.json({ error: 'Error fetching tournament' }, { status: 500 });
return NextResponse.json({ error: 'Error fetching tournament', details: error.message }, { status: 500 });
}
}
32 changes: 32 additions & 0 deletions app/api/tournaments/[id]/structure/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { NextResponse } from "next/server";
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 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",
tournament: updatedTournament,
});
} catch (error) {
console.error("Error updating tournament structure", error);
return NextResponse.json({
message: "An error occurred while updating the tournament structure",
}, { status: 500 });
}
}
36 changes: 20 additions & 16 deletions app/api/tournaments/route.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { NextResponse } from 'next/server';
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();
import dbConnect from "../../../lib/dbConnect";
import Tournament from '../../../model/Tournament';
import Games from '../../../model/Games';
import Organizer from '../../../model/Organizer';

export async function GET(request) {
await dbConnect();

const { searchParams } = new URL(request.url);
const page = parseInt(searchParams.get('page') || '1');
const limit = parseInt(searchParams.get('limit') || '10');
Expand All @@ -12,24 +15,25 @@ export async function GET(request) {

const skip = (page - 1) * limit;

const where = {};
if (gameType) where.gameType = gameType;
if (organizerId) where.organizerId = organizerId;
const query = {};
if (gameType) query.gameType = gameType;
if (organizerId) query.organizerId = organizerId;

try {
console.log('Fetching tournaments with params:', { page, limit, gameType, organizerId });

const tournaments = await prisma.tournament.findMany({
where,
include: {
game: true,
organizer: true,
},
skip,
take: limit,
});
// Ensure models are registered
Games;
Organizer;

const tournaments = await Tournament.find(query)
.populate('gameId', 'name category gameBannerPhoto')
.populate('organizerId', 'orgName bannerPhoto')
.skip(skip)
.limit(limit)
.lean();

const total = await prisma.tournament.count({ where });
const total = await Tournament.countDocuments(query);

console.log(`Found ${tournaments.length} tournaments out of ${total} total`);

Expand Down
Loading

0 comments on commit 76d249a

Please sign in to comment.