2026-01-17 13:01:35 +09:00
|
|
|
import {
|
|
|
|
|
deleteAlbumPhoto,
|
|
|
|
|
deleteAlbumVideo,
|
|
|
|
|
} from '../../services/image.js';
|
2026-01-21 14:58:07 +09:00
|
|
|
import { withTransaction } from '../../utils/transaction.js';
|
2026-01-17 13:01:35 +09:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 앨범 티저 라우트
|
|
|
|
|
* GET: 공개, DELETE: 인증 필요
|
|
|
|
|
*/
|
|
|
|
|
export default async function teasersRoutes(fastify) {
|
|
|
|
|
const { db } = fastify;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GET /api/albums/:albumId/teasers
|
|
|
|
|
*/
|
|
|
|
|
fastify.get('/:albumId/teasers', {
|
|
|
|
|
schema: {
|
|
|
|
|
tags: ['albums'],
|
|
|
|
|
summary: '앨범 티저 목록',
|
|
|
|
|
},
|
|
|
|
|
}, async (request, reply) => {
|
|
|
|
|
const { albumId } = request.params;
|
|
|
|
|
|
|
|
|
|
const [albums] = await db.query('SELECT folder_name FROM albums WHERE id = ?', [albumId]);
|
|
|
|
|
if (albums.length === 0) {
|
|
|
|
|
return reply.code(404).send({ error: '앨범을 찾을 수 없습니다.' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [teasers] = await db.query(
|
|
|
|
|
`SELECT id, original_url, medium_url, thumb_url, video_url, sort_order, media_type
|
|
|
|
|
FROM album_teasers
|
|
|
|
|
WHERE album_id = ?
|
|
|
|
|
ORDER BY sort_order ASC`,
|
|
|
|
|
[albumId]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return teasers;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* DELETE /api/albums/:albumId/teasers/:teaserId
|
|
|
|
|
*/
|
|
|
|
|
fastify.delete('/:albumId/teasers/:teaserId', {
|
|
|
|
|
schema: {
|
|
|
|
|
tags: ['albums'],
|
|
|
|
|
summary: '티저 삭제',
|
|
|
|
|
security: [{ bearerAuth: [] }],
|
|
|
|
|
},
|
|
|
|
|
preHandler: [fastify.authenticate],
|
|
|
|
|
}, async (request, reply) => {
|
|
|
|
|
const { albumId, teaserId } = request.params;
|
|
|
|
|
|
2026-01-21 14:58:07 +09:00
|
|
|
// 티저 존재 여부 먼저 확인
|
|
|
|
|
const [teasers] = await db.query(
|
|
|
|
|
`SELECT t.*, a.folder_name
|
|
|
|
|
FROM album_teasers t
|
|
|
|
|
JOIN albums a ON t.album_id = a.id
|
|
|
|
|
WHERE t.id = ? AND t.album_id = ?`,
|
|
|
|
|
[teaserId, albumId]
|
|
|
|
|
);
|
2026-01-17 13:01:35 +09:00
|
|
|
|
2026-01-21 14:58:07 +09:00
|
|
|
if (teasers.length === 0) {
|
|
|
|
|
return reply.code(404).send({ error: '티저를 찾을 수 없습니다.' });
|
|
|
|
|
}
|
2026-01-17 13:01:35 +09:00
|
|
|
|
2026-01-21 14:58:07 +09:00
|
|
|
const teaser = teasers[0];
|
|
|
|
|
const filename = teaser.original_url.split('/').pop();
|
2026-01-17 13:01:35 +09:00
|
|
|
|
2026-01-21 14:58:07 +09:00
|
|
|
return withTransaction(db, async (connection) => {
|
2026-01-17 13:01:35 +09:00
|
|
|
await deleteAlbumPhoto(teaser.folder_name, 'teaser', filename);
|
|
|
|
|
|
|
|
|
|
if (teaser.video_url) {
|
|
|
|
|
const videoFilename = teaser.video_url.split('/').pop();
|
|
|
|
|
await deleteAlbumVideo(teaser.folder_name, videoFilename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await connection.query('DELETE FROM album_teasers WHERE id = ?', [teaserId]);
|
|
|
|
|
|
|
|
|
|
return { message: '티저가 삭제되었습니다.' };
|
2026-01-21 14:58:07 +09:00
|
|
|
});
|
2026-01-17 13:01:35 +09:00
|
|
|
});
|
|
|
|
|
}
|