Files
LunaTV/src/app/api/playrecords/route.ts
2025-06-19 21:00:47 +08:00

73 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* eslint-disable no-console */
import { NextRequest, NextResponse } from 'next/server';
import { db } from '@/lib/db';
import { PlayRecord } from '@/lib/db';
export async function GET() {
try {
const records = await db.getAllPlayRecords();
return NextResponse.json(records, { status: 200 });
} catch (err) {
console.error('获取播放记录失败', err);
return NextResponse.json(
{ error: 'Internal Server Error' },
{ status: 500 }
);
}
}
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { key, record }: { key: string; record: PlayRecord } = body;
if (!key || !record) {
return NextResponse.json(
{ error: 'Missing key or record' },
{ status: 400 }
);
}
// 验证播放记录数据
if (!record.title || !record.source_name || record.index < 1) {
return NextResponse.json(
{ error: 'Invalid record data' },
{ status: 400 }
);
}
// 从key中解析source和id
const [source, id] = key.split('+');
if (!source || !id) {
return NextResponse.json(
{ error: 'Invalid key format' },
{ status: 400 }
);
}
// 保存播放记录不包含user_id将由savePlayRecord自动添加
const recordWithoutUserId = {
title: record.title,
source_name: record.source_name,
cover: record.cover,
index: record.index,
total_episodes: record.total_episodes,
play_time: record.play_time,
total_time: record.total_time,
save_time: record.save_time,
};
await db.savePlayRecord(source, id, recordWithoutUserId);
return NextResponse.json({ success: true }, { status: 200 });
} catch (err) {
console.error('保存播放记录失败', err);
return NextResponse.json(
{ error: 'Internal Server Error' },
{ status: 500 }
);
}
}