58 lines
1.6 KiB
JavaScript
58 lines
1.6 KiB
JavaScript
|
|
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, 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: "앨범 정보를 가져오는데 실패했습니다." });
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
// 특정 앨범 조회 (트랙 및 상세 정보 포함)
|
||
|
|
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;
|
||
|
|
|
||
|
|
res.json(album);
|
||
|
|
} catch (error) {
|
||
|
|
console.error("앨범 조회 오류:", error);
|
||
|
|
res.status(500).json({ error: "앨범 정보를 가져오는데 실패했습니다." });
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
export default router;
|