// API Routes สำหรับระบบฐานข้อมูล SEA Games 2025 // เป็นตัวอย่าง endpoints ที่จะใช้ในการพัฒนา web application /** * เส้นทาง API สำหรับข้อมูลนักกีฬา (Athletes) */ // GET /api/athletes - ดึงข้อมูลนักกีฬาทั้งหมด app.get('/api/athletes', async (req, res) => { try { const connection = await pool.getConnection(); const [athletes] = await connection.query('SELECT * FROM Athlete'); connection.release(); res.json(athletes); } catch (error) { res.status(500).json({ error: error.message }); } }); // GET /api/athletes/:id - ดึงข้อมูลนักกีฬารายคน app.get('/api/athletes/:id', async (req, res) => { try { const connection = await pool.getConnection(); const [athlete] = await connection.query('SELECT * FROM Athlete WHERE athlete_id = ?', [req.params.id]); connection.release(); if (athlete.length === 0) { return res.status(404).json({ message: 'ไม่พบข้อมูลนักกีฬา' }); } res.json(athlete[0]); } catch (error) { res.status(500).json({ error: error.message }); } }); // POST /api/athletes - เพิ่มข้อมูลนักกีฬาใหม่ app.post('/api/athletes', async (req, res) => { const { athlete_name, birth_date, country, gender } = req.body; try { const connection = await pool.getConnection(); const [result] = await connection.query( 'INSERT INTO Athlete (athlete_name, birth_date, country, gender) VALUES (?, ?, ?, ?)', [athlete_name, birth_date, country, gender] ); connection.release(); res.status(201).json({ message: 'สร้างข้อมูลนักกีฬาสำเร็จ', athlete_id: result.insertId }); } catch (error) { res.status(500).json({ error: error.message }); } }); /** * เส้นทาง API สำหรับข้อมูลกีฬา (Sports) */ // GET /api/sports - ดึงข้อมูลกีฬาทั้งหมด app.get('/api/sports', async (req, res) => { try { const connection = await pool.getConnection(); const [sports] = await connection.query('SELECT * FROM Sport'); connection.release(); res.json(sports); } catch (error) { res.status(500).json({ error: error.message }); } }); // GET /api/sports/:id/events - ดึงข้อมูลการแข่งขันในกีฬาที่กำหนด app.get('/api/sports/:id/events', async (req, res) => { try { const connection = await pool.getConnection(); const [events] = await connection.query( 'SELECT e.*, v.venue_name FROM Event e JOIN Venue v ON e.venue_id = v.venue_id WHERE e.sport_id = ?', [req.params.id] ); connection.release(); res.json(events); } catch (error) { res.status(500).json({ error: error.message }); } }); /** * เส้นทาง API สำหรับข้อมูลการแข่งขัน (Events) */ // GET /api/events - ดึงข้อมูลการแข่งขันทั้งหมด app.get('/api/events', async (req, res) => { try { const connection = await pool.getConnection(); const [events] = await connection.query(` SELECT e.*, s.sport_name, v.venue_name FROM Event e `); connection.release(); res.json(events); } catch (error) { res.status(500).json({ error: error.message }); } }); // GET /api/events/:id/participants - ดึงรายชื่อนักกีฬาที่เข้าร่วมการแข่งขัน app.get('/api/events/:id/participants', async (req, res) => { try { const connection = await pool.getConnection(); const [participants] = await connection.query(` SELECT a.* FROM Participation p WHERE p.event_id = ? `, [req.params.id]); connection.release(); res.json(participants); } catch (error) { res.status(500).json({ error: error.message }); } }); /** * เส้นทาง API สำหรับข้อมูลเหรียญรางวัล (Medals) */ // GET /api/medals - ดึงข้อมูลเหรียญรางวัลทั้งหมด app.get('/api/medals', async (req, res) => { try { const connection = await pool.getConnection(); const [medals] = await connection.query(` SELECT m.*, a.athlete_name, e.event_name, s.sport_name FROM Medal m `); connection.release(); res.json(medals); } catch (error) { res.status(500).json({ error: error.message }); } }); // GET /api/medals/tally - ดึงข้อมูลตารางเหรียญรางวัลตามประเทศ app.get('/api/medals/tally', async (req, res) => { try { const connection = await pool.getConnection(); const [tally] = await connection.query('SELECT * FROM MedalTally ORDER BY gold_count DESC, silver_count DESC, bronze_count DESC'); connection.release(); res.json(tally); } catch (error) { res.status(500).json({ error: error.message }); } }); // POST /api/medals - เพิ่มข้อมูลเหรียญรางวัลใหม่ app.post('/api/medals', async (req, res) => { const { medal_type, athlete_id, event_id, country } = req.body; try { const connection = await pool.getConnection(); const [result] = await connection.query( 'INSERT INTO Medal (medal_type, athlete_id, event_id, country) VALUES (?, ?, ?, ?)', [medal_type, athlete_id, event_id, country] ); connection.release(); res.status(201).json({ message: 'บันทึกข้อมูลเหรียญรางวัลสำเร็จ', medal_id: result.insertId }); } catch (error) { res.status(500).json({ error: error.message }); } }); /** * เส้นทาง API สำหรับข้อมูลสถิติการแข่งขัน (Records) */ // GET /api/records - ดึงข้อมูลสถิติการแข่งขันทั้งหมด app.get('/api/records', async (req, res) => { try { const connection = await pool.getConnection(); const [records] = await connection.query(` SELECT r.*, s.sport_name, e.event_name FROM Record r `); connection.release(); res.json(records); } catch (error) { res.status(500).json({ error: error.message }); } }); /** * เส้นทาง API สำหรับข้อมูลสถานที่แข่งขัน (Venues) */ // GET /api/venues - ดึงข้อมูลสถานที่แข่งขันทั้งหมด app.get('/api/venues', async (req, res) => { try { const connection = await pool.getConnection(); const [venues] = await connection.query('SELECT * FROM Venue'); connection.release(); res.json(venues); } catch (error) { res.status(500).json({ error: error.message }); } }); // GET /api/venues/:id/events - ดึงข้อมูลการแข่งขันในสถานที่ที่กำหนด app.get('/api/venues/:id/events', async (req, res) => { try { const connection = await pool.getConnection(); const [events] = await connection.query(` SELECT e.*, s.sport_name FROM Event e WHERE e.venue_id = ? `, [req.params.id]); connection.release(); res.json(events); } catch (error) { res.status(500).json({ error: error.message }); } }); /** * เส้นทาง API สำหรับข้อมูลข่าวสาร (News) */ // GET /api/news - ดึงข้อมูลข่าวสารทั้งหมด app.get('/api/news', async (req, res) => { try { const connection = await pool.getConnection(); const [news] = await connection.query('SELECT * FROM News ORDER BY publish_date DESC'); connection.release(); res.json(news); } catch (error) { res.status(500).json({ error: error.message }); } }); // GET /api/news/featured - ดึงข่าวสารที่แนะนำ app.get('/api/news/featured', async (req, res) => { try { const connection = await pool.getConnection(); const [news] = await connection.query('SELECT * FROM News WHERE is_featured = 1 ORDER BY publish_date DESC'); connection.release(); res.json(news); } catch (error) { res.status(500).json({ error: error.message }); } });
Standard input is empty
// API Routes สำหรับระบบฐานข้อมูล SEA Games 2025
// เป็นตัวอย่าง endpoints ที่จะใช้ในการพัฒนา web application
/**
* เส้นทาง API สำหรับข้อมูลนักกีฬา (Athletes)
*/
// GET /api/athletes - ดึงข้อมูลนักกีฬาทั้งหมด
app.get('/api/athletes', async (req, res) => {
try {
const connection = await pool.getConnection();
const [athletes] = await connection.query('SELECT * FROM Athlete');
connection.release();
res.json(athletes);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// GET /api/athletes/:id - ดึงข้อมูลนักกีฬารายคน
app.get('/api/athletes/:id', async (req, res) => {
try {
const connection = await pool.getConnection();
const [athlete] = await connection.query('SELECT * FROM Athlete WHERE athlete_id = ?', [req.params.id]);
connection.release();
if (athlete.length === 0) {
return res.status(404).json({ message: 'ไม่พบข้อมูลนักกีฬา' });
}
res.json(athlete[0]);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// POST /api/athletes - เพิ่มข้อมูลนักกีฬาใหม่
app.post('/api/athletes', async (req, res) => {
const { athlete_name, birth_date, country, gender } = req.body;
try {
const connection = await pool.getConnection();
const [result] = await connection.query(
'INSERT INTO Athlete (athlete_name, birth_date, country, gender) VALUES (?, ?, ?, ?)',
[athlete_name, birth_date, country, gender]
);
connection.release();
res.status(201).json({
message: 'สร้างข้อมูลนักกีฬาสำเร็จ',
athlete_id: result.insertId
});
} catch (error) {
res.status(500).json({ error: error.message });
}
});
/**
* เส้นทาง API สำหรับข้อมูลกีฬา (Sports)
*/
// GET /api/sports - ดึงข้อมูลกีฬาทั้งหมด
app.get('/api/sports', async (req, res) => {
try {
const connection = await pool.getConnection();
const [sports] = await connection.query('SELECT * FROM Sport');
connection.release();
res.json(sports);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// GET /api/sports/:id/events - ดึงข้อมูลการแข่งขันในกีฬาที่กำหนด
app.get('/api/sports/:id/events', async (req, res) => {
try {
const connection = await pool.getConnection();
const [events] = await connection.query(
'SELECT e.*, v.venue_name FROM Event e JOIN Venue v ON e.venue_id = v.venue_id WHERE e.sport_id = ?',
[req.params.id]
);
connection.release();
res.json(events);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
/**
* เส้นทาง API สำหรับข้อมูลการแข่งขัน (Events)
*/
// GET /api/events - ดึงข้อมูลการแข่งขันทั้งหมด
app.get('/api/events', async (req, res) => {
try {
const connection = await pool.getConnection();
const [events] = await connection.query(`
SELECT e.*, s.sport_name, v.venue_name
FROM Event e
JOIN Sport s ON e.sport_id = s.sport_id
JOIN Venue v ON e.venue_id = v.venue_id
`);
connection.release();
res.json(events);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// GET /api/events/:id/participants - ดึงรายชื่อนักกีฬาที่เข้าร่วมการแข่งขัน
app.get('/api/events/:id/participants', async (req, res) => {
try {
const connection = await pool.getConnection();
const [participants] = await connection.query(`
SELECT a.*
FROM Participation p
JOIN Athlete a ON p.athlete_id = a.athlete_id
WHERE p.event_id = ?
`, [req.params.id]);
connection.release();
res.json(participants);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
/**
* เส้นทาง API สำหรับข้อมูลเหรียญรางวัล (Medals)
*/
// GET /api/medals - ดึงข้อมูลเหรียญรางวัลทั้งหมด
app.get('/api/medals', async (req, res) => {
try {
const connection = await pool.getConnection();
const [medals] = await connection.query(`
SELECT m.*, a.athlete_name, e.event_name, s.sport_name
FROM Medal m
JOIN Athlete a ON m.athlete_id = a.athlete_id
JOIN Event e ON m.event_id = e.event_id
JOIN Sport s ON e.sport_id = s.sport_id
`);
connection.release();
res.json(medals);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// GET /api/medals/tally - ดึงข้อมูลตารางเหรียญรางวัลตามประเทศ
app.get('/api/medals/tally', async (req, res) => {
try {
const connection = await pool.getConnection();
const [tally] = await connection.query('SELECT * FROM MedalTally ORDER BY gold_count DESC, silver_count DESC, bronze_count DESC');
connection.release();
res.json(tally);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// POST /api/medals - เพิ่มข้อมูลเหรียญรางวัลใหม่
app.post('/api/medals', async (req, res) => {
const { medal_type, athlete_id, event_id, country } = req.body;
try {
const connection = await pool.getConnection();
const [result] = await connection.query(
'INSERT INTO Medal (medal_type, athlete_id, event_id, country) VALUES (?, ?, ?, ?)',
[medal_type, athlete_id, event_id, country]
);
connection.release();
res.status(201).json({
message: 'บันทึกข้อมูลเหรียญรางวัลสำเร็จ',
medal_id: result.insertId
});
} catch (error) {
res.status(500).json({ error: error.message });
}
});
/**
* เส้นทาง API สำหรับข้อมูลสถิติการแข่งขัน (Records)
*/
// GET /api/records - ดึงข้อมูลสถิติการแข่งขันทั้งหมด
app.get('/api/records', async (req, res) => {
try {
const connection = await pool.getConnection();
const [records] = await connection.query(`
SELECT r.*, s.sport_name, e.event_name
FROM Record r
JOIN Sport s ON r.sport_id = s.sport_id
JOIN Event e ON r.event_id = e.event_id
`);
connection.release();
res.json(records);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
/**
* เส้นทาง API สำหรับข้อมูลสถานที่แข่งขัน (Venues)
*/
// GET /api/venues - ดึงข้อมูลสถานที่แข่งขันทั้งหมด
app.get('/api/venues', async (req, res) => {
try {
const connection = await pool.getConnection();
const [venues] = await connection.query('SELECT * FROM Venue');
connection.release();
res.json(venues);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// GET /api/venues/:id/events - ดึงข้อมูลการแข่งขันในสถานที่ที่กำหนด
app.get('/api/venues/:id/events', async (req, res) => {
try {
const connection = await pool.getConnection();
const [events] = await connection.query(`
SELECT e.*, s.sport_name
FROM Event e
JOIN Sport s ON e.sport_id = s.sport_id
WHERE e.venue_id = ?
`, [req.params.id]);
connection.release();
res.json(events);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
/**
* เส้นทาง API สำหรับข้อมูลข่าวสาร (News)
*/
// GET /api/news - ดึงข้อมูลข่าวสารทั้งหมด
app.get('/api/news', async (req, res) => {
try {
const connection = await pool.getConnection();
const [news] = await connection.query('SELECT * FROM News ORDER BY publish_date DESC');
connection.release();
res.json(news);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// GET /api/news/featured - ดึงข่าวสารที่แนะนำ
app.get('/api/news/featured', async (req, res) => {
try {
const connection = await pool.getConnection();
const [news] = await connection.query('SELECT * FROM News WHERE is_featured = 1 ORDER BY publish_date DESC');
connection.release();
res.json(news);
} catch (error) {
res.status(500).json({ error: error.message });
}
});