nextav/src/app/api/libraries/route.ts

28 lines
865 B
TypeScript

import { NextResponse } from 'next/server';
import { getDatabase } from '@/db';
export async function GET() {
const db = getDatabase();
const libraries = db.prepare('SELECT * FROM libraries').all();
return NextResponse.json(libraries);
}
export async function POST(request: Request) {
const { path } = await request.json();
if (!path) {
return NextResponse.json({ error: 'Path is required' }, { status: 400 });
}
try {
const db = getDatabase();
const info = db.prepare('INSERT INTO libraries (path) VALUES (?)').run(path);
return NextResponse.json({ id: info.lastInsertRowid, path });
} catch (error: any) {
if (error.code === 'SQLITE_CONSTRAINT_UNIQUE') {
return NextResponse.json({ error: 'Path already exists' }, { status: 409 });
}
return NextResponse.json({ error: error.message }, { status: 500 });
}
}