import express from "express"; import pool from "../lib/db.js"; const router = express.Router(); // 전체 앨범 조회 (트랙 포함) router.get("/", async (req, res) => { try { // 앨범 목록 조회 const [albums] = await pool.query( "SELECT id, title, album_type, album_type_short, release_date, cover_url FROM albums ORDER BY release_date DESC" ); // 각 앨범에 트랙 정보 추가 for (const album of albums) { const [tracks] = await pool.query( "SELECT id, track_number, title, is_title_track, duration, lyricist, composer, arranger FROM tracks WHERE album_id = ? ORDER BY track_number", [album.id] ); album.tracks = tracks; } res.json(albums); } catch (error) { console.error("앨범 조회 오류:", error); res.status(500).json({ error: "앨범 정보를 가져오는데 실패했습니다." }); } }); // 앨범명(slug)으로 조회 router.get("/by-name/:name", async (req, res) => { try { // URL 디코딩된 앨범명으로 조회 const albumName = decodeURIComponent(req.params.name); const [albums] = await pool.query("SELECT * FROM albums WHERE title = ?", [ albumName, ]); if (albums.length === 0) { return res.status(404).json({ error: "앨범을 찾을 수 없습니다." }); } const album = albums[0]; // 트랙 정보 조회 (가사 포함) const [tracks] = await pool.query( "SELECT * FROM tracks WHERE album_id = ? ORDER BY track_number", [album.id] ); album.tracks = tracks; // 티저 이미지 조회 const [teasers] = await pool.query( "SELECT image_url FROM album_teasers WHERE album_id = ? ORDER BY sort_order", [album.id] ); album.teasers = teasers.map((t) => t.image_url); // 컨셉 포토 조회 (멤버 정보 포함) const [photos] = await pool.query( `SELECT p.id, p.photo_url, p.photo_type, p.concept_name, p.sort_order, GROUP_CONCAT(m.name ORDER BY m.id SEPARATOR ', ') as members FROM album_photos p LEFT JOIN album_photo_members pm ON p.id = pm.photo_id LEFT JOIN members m ON pm.member_id = m.id WHERE p.album_id = ? GROUP BY p.id ORDER BY p.sort_order`, [album.id] ); // 컨셉별로 그룹화 const conceptPhotos = {}; for (const photo of photos) { const concept = photo.concept_name || "Default"; if (!conceptPhotos[concept]) { conceptPhotos[concept] = []; } conceptPhotos[concept].push({ id: photo.id, url: photo.photo_url, type: photo.photo_type, members: photo.members, sortOrder: photo.sort_order, }); } album.conceptPhotos = conceptPhotos; res.json(album); } catch (error) { console.error("앨범 조회 오류:", error); res.status(500).json({ error: "앨범 정보를 가져오는데 실패했습니다." }); } }); // 특정 앨범 조회 (트랙 및 상세 정보 포함) router.get("/:id", async (req, res) => { try { const [albums] = await pool.query("SELECT * FROM albums WHERE id = ?", [ req.params.id, ]); if (albums.length === 0) { return res.status(404).json({ error: "앨범을 찾을 수 없습니다." }); } const album = albums[0]; // 트랙 정보 조회 (가사 포함) const [tracks] = await pool.query( "SELECT * FROM tracks WHERE album_id = ? ORDER BY track_number", [album.id] ); album.tracks = tracks; // 티저 이미지 조회 const [teasers] = await pool.query( "SELECT image_url FROM album_teasers WHERE album_id = ? ORDER BY sort_order", [album.id] ); album.teasers = teasers.map((t) => t.image_url); // 컨셉 포토 조회 (멤버 정보 포함) const [photos] = await pool.query( `SELECT p.id, p.photo_url, p.photo_type, p.concept_name, p.sort_order, GROUP_CONCAT(m.name ORDER BY m.id SEPARATOR ', ') as members FROM album_photos p LEFT JOIN album_photo_members pm ON p.id = pm.photo_id LEFT JOIN members m ON pm.member_id = m.id WHERE p.album_id = ? GROUP BY p.id ORDER BY p.sort_order`, [album.id] ); // 컨셉별로 그룹화 const conceptPhotos = {}; for (const photo of photos) { const concept = photo.concept_name || "Default"; if (!conceptPhotos[concept]) { conceptPhotos[concept] = []; } conceptPhotos[concept].push({ id: photo.id, url: photo.photo_url, type: photo.photo_type, members: photo.members, sortOrder: photo.sort_order, }); } album.conceptPhotos = conceptPhotos; res.json(album); } catch (error) { console.error("앨범 조회 오류:", error); res.status(500).json({ error: "앨범 정보를 가져오는데 실패했습니다." }); } }); export default router;