fork download
  1. // API Routes สำหรับระบบฐานข้อมูล SEA Games 2025
  2. // เป็นตัวอย่าง endpoints ที่จะใช้ในการพัฒนา web application
  3.  
  4. /**
  5.  * เส้นทาง API สำหรับข้อมูลนักกีฬา (Athletes)
  6.  */
  7. // GET /api/athletes - ดึงข้อมูลนักกีฬาทั้งหมด
  8. app.get('/api/athletes', async (req, res) => {
  9. try {
  10. const connection = await pool.getConnection();
  11. const [athletes] = await connection.query('SELECT * FROM Athlete');
  12. connection.release();
  13. res.json(athletes);
  14. } catch (error) {
  15. res.status(500).json({ error: error.message });
  16. }
  17. });
  18.  
  19. // GET /api/athletes/:id - ดึงข้อมูลนักกีฬารายคน
  20. app.get('/api/athletes/:id', async (req, res) => {
  21. try {
  22. const connection = await pool.getConnection();
  23. const [athlete] = await connection.query('SELECT * FROM Athlete WHERE athlete_id = ?', [req.params.id]);
  24. connection.release();
  25.  
  26. if (athlete.length === 0) {
  27. return res.status(404).json({ message: 'ไม่พบข้อมูลนักกีฬา' });
  28. }
  29.  
  30. res.json(athlete[0]);
  31. } catch (error) {
  32. res.status(500).json({ error: error.message });
  33. }
  34. });
  35.  
  36. // POST /api/athletes - เพิ่มข้อมูลนักกีฬาใหม่
  37. app.post('/api/athletes', async (req, res) => {
  38. const { athlete_name, birth_date, country, gender } = req.body;
  39.  
  40. try {
  41. const connection = await pool.getConnection();
  42. const [result] = await connection.query(
  43. 'INSERT INTO Athlete (athlete_name, birth_date, country, gender) VALUES (?, ?, ?, ?)',
  44. [athlete_name, birth_date, country, gender]
  45. );
  46. connection.release();
  47.  
  48. res.status(201).json({
  49. message: 'สร้างข้อมูลนักกีฬาสำเร็จ',
  50. athlete_id: result.insertId
  51. });
  52. } catch (error) {
  53. res.status(500).json({ error: error.message });
  54. }
  55. });
  56.  
  57. /**
  58.  * เส้นทาง API สำหรับข้อมูลกีฬา (Sports)
  59.  */
  60. // GET /api/sports - ดึงข้อมูลกีฬาทั้งหมด
  61. app.get('/api/sports', async (req, res) => {
  62. try {
  63. const connection = await pool.getConnection();
  64. const [sports] = await connection.query('SELECT * FROM Sport');
  65. connection.release();
  66. res.json(sports);
  67. } catch (error) {
  68. res.status(500).json({ error: error.message });
  69. }
  70. });
  71.  
  72. // GET /api/sports/:id/events - ดึงข้อมูลการแข่งขันในกีฬาที่กำหนด
  73. app.get('/api/sports/:id/events', async (req, res) => {
  74. try {
  75. const connection = await pool.getConnection();
  76. const [events] = await connection.query(
  77. 'SELECT e.*, v.venue_name FROM Event e JOIN Venue v ON e.venue_id = v.venue_id WHERE e.sport_id = ?',
  78. [req.params.id]
  79. );
  80. connection.release();
  81. res.json(events);
  82. } catch (error) {
  83. res.status(500).json({ error: error.message });
  84. }
  85. });
  86.  
  87. /**
  88.  * เส้นทาง API สำหรับข้อมูลการแข่งขัน (Events)
  89.  */
  90. // GET /api/events - ดึงข้อมูลการแข่งขันทั้งหมด
  91. app.get('/api/events', async (req, res) => {
  92. try {
  93. const connection = await pool.getConnection();
  94. const [events] = await connection.query(`
  95. SELECT e.*, s.sport_name, v.venue_name
  96. FROM Event e
  97. JOIN Sport s ON e.sport_id = s.sport_id
  98. JOIN Venue v ON e.venue_id = v.venue_id
  99. `);
  100. connection.release();
  101. res.json(events);
  102. } catch (error) {
  103. res.status(500).json({ error: error.message });
  104. }
  105. });
  106.  
  107. // GET /api/events/:id/participants - ดึงรายชื่อนักกีฬาที่เข้าร่วมการแข่งขัน
  108. app.get('/api/events/:id/participants', async (req, res) => {
  109. try {
  110. const connection = await pool.getConnection();
  111. const [participants] = await connection.query(`
  112. SELECT a.*
  113. FROM Participation p
  114. JOIN Athlete a ON p.athlete_id = a.athlete_id
  115. WHERE p.event_id = ?
  116. `, [req.params.id]);
  117. connection.release();
  118. res.json(participants);
  119. } catch (error) {
  120. res.status(500).json({ error: error.message });
  121. }
  122. });
  123.  
  124. /**
  125.  * เส้นทาง API สำหรับข้อมูลเหรียญรางวัล (Medals)
  126.  */
  127. // GET /api/medals - ดึงข้อมูลเหรียญรางวัลทั้งหมด
  128. app.get('/api/medals', async (req, res) => {
  129. try {
  130. const connection = await pool.getConnection();
  131. const [medals] = await connection.query(`
  132. SELECT m.*, a.athlete_name, e.event_name, s.sport_name
  133. FROM Medal m
  134. JOIN Athlete a ON m.athlete_id = a.athlete_id
  135. JOIN Event e ON m.event_id = e.event_id
  136. JOIN Sport s ON e.sport_id = s.sport_id
  137. `);
  138. connection.release();
  139. res.json(medals);
  140. } catch (error) {
  141. res.status(500).json({ error: error.message });
  142. }
  143. });
  144.  
  145. // GET /api/medals/tally - ดึงข้อมูลตารางเหรียญรางวัลตามประเทศ
  146. app.get('/api/medals/tally', async (req, res) => {
  147. try {
  148. const connection = await pool.getConnection();
  149. const [tally] = await connection.query('SELECT * FROM MedalTally ORDER BY gold_count DESC, silver_count DESC, bronze_count DESC');
  150. connection.release();
  151. res.json(tally);
  152. } catch (error) {
  153. res.status(500).json({ error: error.message });
  154. }
  155. });
  156.  
  157. // POST /api/medals - เพิ่มข้อมูลเหรียญรางวัลใหม่
  158. app.post('/api/medals', async (req, res) => {
  159. const { medal_type, athlete_id, event_id, country } = req.body;
  160.  
  161. try {
  162. const connection = await pool.getConnection();
  163. const [result] = await connection.query(
  164. 'INSERT INTO Medal (medal_type, athlete_id, event_id, country) VALUES (?, ?, ?, ?)',
  165. [medal_type, athlete_id, event_id, country]
  166. );
  167. connection.release();
  168.  
  169. res.status(201).json({
  170. message: 'บันทึกข้อมูลเหรียญรางวัลสำเร็จ',
  171. medal_id: result.insertId
  172. });
  173. } catch (error) {
  174. res.status(500).json({ error: error.message });
  175. }
  176. });
  177.  
  178. /**
  179.  * เส้นทาง API สำหรับข้อมูลสถิติการแข่งขัน (Records)
  180.  */
  181. // GET /api/records - ดึงข้อมูลสถิติการแข่งขันทั้งหมด
  182. app.get('/api/records', async (req, res) => {
  183. try {
  184. const connection = await pool.getConnection();
  185. const [records] = await connection.query(`
  186. SELECT r.*, s.sport_name, e.event_name
  187. FROM Record r
  188. JOIN Sport s ON r.sport_id = s.sport_id
  189. JOIN Event e ON r.event_id = e.event_id
  190. `);
  191. connection.release();
  192. res.json(records);
  193. } catch (error) {
  194. res.status(500).json({ error: error.message });
  195. }
  196. });
  197.  
  198. /**
  199.  * เส้นทาง API สำหรับข้อมูลสถานที่แข่งขัน (Venues)
  200.  */
  201. // GET /api/venues - ดึงข้อมูลสถานที่แข่งขันทั้งหมด
  202. app.get('/api/venues', async (req, res) => {
  203. try {
  204. const connection = await pool.getConnection();
  205. const [venues] = await connection.query('SELECT * FROM Venue');
  206. connection.release();
  207. res.json(venues);
  208. } catch (error) {
  209. res.status(500).json({ error: error.message });
  210. }
  211. });
  212.  
  213. // GET /api/venues/:id/events - ดึงข้อมูลการแข่งขันในสถานที่ที่กำหนด
  214. app.get('/api/venues/:id/events', async (req, res) => {
  215. try {
  216. const connection = await pool.getConnection();
  217. const [events] = await connection.query(`
  218. SELECT e.*, s.sport_name
  219. FROM Event e
  220. JOIN Sport s ON e.sport_id = s.sport_id
  221. WHERE e.venue_id = ?
  222. `, [req.params.id]);
  223. connection.release();
  224. res.json(events);
  225. } catch (error) {
  226. res.status(500).json({ error: error.message });
  227. }
  228. });
  229.  
  230. /**
  231.  * เส้นทาง API สำหรับข้อมูลข่าวสาร (News)
  232.  */
  233. // GET /api/news - ดึงข้อมูลข่าวสารทั้งหมด
  234. app.get('/api/news', async (req, res) => {
  235. try {
  236. const connection = await pool.getConnection();
  237. const [news] = await connection.query('SELECT * FROM News ORDER BY publish_date DESC');
  238. connection.release();
  239. res.json(news);
  240. } catch (error) {
  241. res.status(500).json({ error: error.message });
  242. }
  243. });
  244.  
  245. // GET /api/news/featured - ดึงข่าวสารที่แนะนำ
  246. app.get('/api/news/featured', async (req, res) => {
  247. try {
  248. const connection = await pool.getConnection();
  249. const [news] = await connection.query('SELECT * FROM News WHERE is_featured = 1 ORDER BY publish_date DESC');
  250. connection.release();
  251. res.json(news);
  252. } catch (error) {
  253. res.status(500).json({ error: error.message });
  254. }
  255. });
Success #stdin #stdout 0.03s 25568KB
stdin
Standard input is empty
stdout
// 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 });
  }
});