Refactor socket handling and integrate database for event management
- Removed old socket handling code and replaced it with a new implementation in `app/api/socketio/route.js`. - Added MySQL database integration for managing active sessions and queue entries. - Implemented event retrieval and ticket purchasing APIs in `app/api/events/route.js` and `app/api/purchase/route.js`. - Created database schema for events, tickets, active sessions, and orders in `database/schema.sql`. - Updated front-end to handle event data fetching and ticket purchasing with improved UI components. - Removed unused SVG files from the public directory.
This commit is contained in:
59
app/api/events/route.js
Normal file
59
app/api/events/route.js
Normal file
@@ -0,0 +1,59 @@
|
||||
import mysql from 'mysql2/promise'
|
||||
|
||||
async function getDbConnection() {
|
||||
if (!process.env.MYSQL_HOST) return null
|
||||
return await mysql.createConnection({
|
||||
host: process.env.MYSQL_HOST,
|
||||
user: process.env.MYSQL_USER,
|
||||
password: process.env.MYSQL_PASSWORD,
|
||||
database: process.env.MYSQL_DATABASE,
|
||||
})
|
||||
}
|
||||
|
||||
export async function GET(request) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url)
|
||||
const eventId = searchParams.get('id')
|
||||
|
||||
const connection = await getDbConnection()
|
||||
if (!connection) {
|
||||
return Response.json({ error: 'Database not configured' }, { status: 500 })
|
||||
}
|
||||
|
||||
if (eventId) {
|
||||
// Get specific event with tickets
|
||||
const [eventRows] = await connection.execute(
|
||||
'SELECT * FROM events WHERE id = ?',
|
||||
[eventId]
|
||||
)
|
||||
|
||||
const [ticketRows] = await connection.execute(
|
||||
'SELECT * FROM tickets WHERE event_id = ? ORDER BY price ASC',
|
||||
[eventId]
|
||||
)
|
||||
|
||||
if (eventRows.length === 0) {
|
||||
return Response.json({ error: 'Event not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
await connection.end()
|
||||
|
||||
return Response.json({
|
||||
event: eventRows[0],
|
||||
tickets: ticketRows
|
||||
})
|
||||
} else {
|
||||
// Get all events
|
||||
const [eventRows] = await connection.execute(
|
||||
'SELECT e.*, COUNT(t.id) as ticket_types FROM events e LEFT JOIN tickets t ON e.id = t.event_id GROUP BY e.id ORDER BY e.created_at DESC'
|
||||
)
|
||||
|
||||
await connection.end()
|
||||
|
||||
return Response.json({ events: eventRows })
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Events API error:', error)
|
||||
return Response.json({ error: 'Internal server error' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user