2026-01-01 09:32:38 +09:00
|
|
|
import express from "express";
|
|
|
|
|
import pool from "../lib/db.js";
|
|
|
|
|
|
|
|
|
|
const router = express.Router();
|
|
|
|
|
|
2026-01-01 17:23:29 +09:00
|
|
|
// 앨범 상세 정보 조회 헬퍼 함수 (트랙, 티저, 컨셉포토 포함)
|
|
|
|
|
async function getAlbumDetails(album) {
|
|
|
|
|
// 트랙 정보 조회 (가사 포함)
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
return album;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-01 09:32:38 +09:00
|
|
|
// 전체 앨범 조회 (트랙 포함)
|
|
|
|
|
router.get("/", async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const [albums] = await pool.query(
|
2026-01-01 14:15:39 +09:00
|
|
|
"SELECT id, title, album_type, album_type_short, release_date, cover_url FROM albums ORDER BY release_date DESC"
|
2026-01-01 09:32:38 +09:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 각 앨범에 트랙 정보 추가
|
|
|
|
|
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: "앨범 정보를 가져오는데 실패했습니다." });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-01 17:23:29 +09:00
|
|
|
// 앨범명으로 조회
|
2026-01-01 17:20:36 +09:00
|
|
|
router.get("/by-name/:name", async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
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: "앨범을 찾을 수 없습니다." });
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-01 17:23:29 +09:00
|
|
|
const album = await getAlbumDetails(albums[0]);
|
2026-01-01 17:20:36 +09:00
|
|
|
res.json(album);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("앨범 조회 오류:", error);
|
|
|
|
|
res.status(500).json({ error: "앨범 정보를 가져오는데 실패했습니다." });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-01 17:23:29 +09:00
|
|
|
// ID로 앨범 조회
|
2026-01-01 09:32:38 +09:00
|
|
|
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: "앨범을 찾을 수 없습니다." });
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-01 17:23:29 +09:00
|
|
|
const album = await getAlbumDetails(albums[0]);
|
2026-01-01 09:32:38 +09:00
|
|
|
res.json(album);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("앨범 조회 오류:", error);
|
|
|
|
|
res.status(500).json({ error: "앨범 정보를 가져오는데 실패했습니다." });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export default router;
|