FODASSE QUE BORRAÇO
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
// app/api/events/register/route.ts
|
||||
// API endpoint for event registration
|
||||
|
||||
import { NextResponse } from 'next/server';
|
||||
import { query, queryOne } from '@/lib/db';
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { eventId, steamId, carModel, carSkin, teamName } = body;
|
||||
|
||||
// Validate required fields
|
||||
if (!eventId || !steamId || !carModel) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: 'Missing required fields' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Convert steamId to number (BIGINT)
|
||||
const driverGuid = parseInt(steamId);
|
||||
if (isNaN(driverGuid)) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: 'Invalid Steam ID format' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Check if user exists in database
|
||||
const userCheck = await queryOne(
|
||||
'SELECT driver_guid FROM users WHERE driver_guid = $1',
|
||||
[driverGuid]
|
||||
);
|
||||
|
||||
if (!userCheck) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: 'Steam ID not found in database. Please join a server first.' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
// Check if event exists and is open
|
||||
const eventCheck: any = await queryOne(
|
||||
`SELECT event_id, event_status, max_participants,
|
||||
(SELECT COUNT(*) FROM event_registrations
|
||||
WHERE event_id = $1 AND status = 'REGISTERED') as current_registrations
|
||||
FROM events WHERE event_id = $1`,
|
||||
[eventId]
|
||||
);
|
||||
|
||||
if (!eventCheck) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: 'Event not found' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
if (eventCheck.event_status !== 'OPEN') {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: 'Event registration is closed' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (eventCheck.current_registrations >= eventCheck.max_participants) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: 'Event is full' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Check if already registered
|
||||
const existingReg = await queryOne(
|
||||
'SELECT registration_id FROM event_registrations WHERE event_id = $1 AND driver_guid = $2',
|
||||
[eventId, driverGuid]
|
||||
);
|
||||
|
||||
if (existingReg) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: 'You are already registered for this event' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Insert registration
|
||||
await query(
|
||||
`INSERT INTO event_registrations
|
||||
(event_id, driver_guid, car_model, car_skin, team_name, status)
|
||||
VALUES ($1, $2, $3, $4, $5, 'REGISTERED')`,
|
||||
[eventId, driverGuid, carModel, carSkin || null, teamName || null]
|
||||
);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Registration successful',
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Registration error:', error);
|
||||
return NextResponse.json(
|
||||
{ success: false, error: 'Registration failed. Please try again.' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user