Files
record-app-next/app/api/recordings/[id]/route.ts
2025-07-31 17:05:07 +08:00

99 lines
2.5 KiB
TypeScript

import { NextRequest } from "next/server";
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/lib/auth";
import { RecordingService } from "@/lib/services/recording.service";
import { ApiResponseHandler } from "@/lib/utils/api-response";
import {
AuthenticationError,
NotFoundError,
ValidationError,
} from "@/lib/errors/app-error";
export async function PUT(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
throw new AuthenticationError();
}
const { id } = await params;
if (!id) {
throw new NotFoundError("录音ID不能为空");
}
const body = await request.json();
const { title } = body;
console.log(`PUT /api/recordings/${id} - 请求数据:`, {
title,
userId: session.user.id,
});
if (!title || typeof title !== "string" || title.trim().length === 0) {
throw new ValidationError("录音标题不能为空");
}
if (title.length > 100) {
throw new ValidationError("录音标题不能超过100个字符");
}
console.log(
`Attempting to update recording: ${id} for user: ${
session.user.id
} with title: "${title.trim()}"`
);
const updatedRecording = await RecordingService.updateRecording(
id,
session.user.id,
{ title: title.trim() }
);
console.log(
`Successfully updated recording: ${id}, new title: "${updatedRecording.title}"`
);
return ApiResponseHandler.success(updatedRecording);
} catch (error) {
console.error(`Failed to update recording ${params}:`, error);
return ApiResponseHandler.error(error as Error);
}
}
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
throw new AuthenticationError();
}
const { id } = await params;
if (!id) {
throw new NotFoundError("录音ID不能为空");
}
console.log(
`Attempting to delete recording: ${id} for user: ${session.user.id}`
);
await RecordingService.deleteRecording(id, session.user.id);
console.log(`Successfully deleted recording: ${id}`);
return ApiResponseHandler.noContent();
} catch (error) {
console.error(`Failed to delete recording ${params}:`, error);
return ApiResponseHandler.error(error as Error);
}
}