CHORE(merge): merge from develop
- Initial setup and all features from develop branch - Includes: auth, deploy, docker, style fixes - K3S deployment configuration
This commit is contained in:
79
nextjs/app/api/disciple-videos/reorder/route.ts
Normal file
79
nextjs/app/api/disciple-videos/reorder/route.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
// PUT: 영상 순서 변경 (두 비디오의 order만 교환)
|
||||
export async function PUT(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { videoId1, videoId2 } = body;
|
||||
|
||||
// 입력 검증
|
||||
if (!videoId1 || !videoId2) {
|
||||
return NextResponse.json(
|
||||
{ success: false, message: "두 개의 비디오 ID가 필요합니다." },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (videoId1 === videoId2) {
|
||||
return NextResponse.json(
|
||||
{ success: false, message: "동일한 비디오는 교환할 수 없습니다." },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// 두 비디오 조회
|
||||
const [video1, video2] = await Promise.all([
|
||||
prisma.discipleVideo.findUnique({ where: { id: Number(videoId1) } }),
|
||||
prisma.discipleVideo.findUnique({ where: { id: Number(videoId2) } }),
|
||||
]);
|
||||
|
||||
if (!video1 || !video2) {
|
||||
return NextResponse.json(
|
||||
{ success: false, message: "비디오를 찾을 수 없습니다." },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
// 같은 stage/step인지 확인
|
||||
if (video1.stage !== video2.stage || video1.step !== video2.step) {
|
||||
return NextResponse.json(
|
||||
{ success: false, message: "같은 stage/step의 비디오만 교환할 수 있습니다." },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
await prisma.$transaction([
|
||||
prisma.discipleVideo.update({
|
||||
where: { id: video1.id },
|
||||
data: { order: -1 }, // 임시 음수 값 (unique constraint 회피)
|
||||
}),
|
||||
prisma.discipleVideo.update({
|
||||
where: { id: video2.id },
|
||||
data: { order: video1.order },
|
||||
}),
|
||||
prisma.discipleVideo.update({
|
||||
where: { id: video1.id },
|
||||
data: { order: video2.order },
|
||||
}),
|
||||
]);
|
||||
|
||||
// 업데이트된 stage/step의 모든 비디오 반환
|
||||
const updatedVideos = await prisma.discipleVideo.findMany({
|
||||
where: { stage: video1.stage, step: video1.step },
|
||||
orderBy: { order: 'desc' },
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: updatedVideos,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error reordering disciple videos:", error);
|
||||
const errorMessage = error instanceof Error ? error.message : "제자훈련 영상 순서 변경에 실패했습니다.";
|
||||
return NextResponse.json(
|
||||
{ success: false, message: errorMessage },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user