Spaces:
Runtime error
Runtime error
| import { saveAnnotation, deleteAnnotation, updateAnnotation } from '../../../utils/storage.js'; | |
| import { NextResponse } from 'next/server'; | |
| export async function POST(request) { | |
| try { | |
| const body = await request.json(); | |
| const { | |
| dataset_name, | |
| dataset_tag, | |
| document_index, | |
| page_number, | |
| } = body; | |
| if ( | |
| document_index === undefined || document_index === null || | |
| page_number === undefined || | |
| !dataset_name?.text || | |
| !dataset_tag | |
| ) { | |
| return NextResponse.json( | |
| { error: 'Missing required fields: dataset_name.text, dataset_tag, document_index, page_number' }, | |
| { status: 400 } | |
| ); | |
| } | |
| await saveAnnotation(body); | |
| return NextResponse.json({ success: true }); | |
| } catch (error) { | |
| console.error('Annotation error:', error); | |
| return NextResponse.json({ error: 'Failed to save annotation: ' + error.message }, { status: 500 }); | |
| } | |
| } | |
| export async function DELETE(request) { | |
| try { | |
| const { searchParams } = new URL(request.url); | |
| const timestamp = searchParams.get('timestamp'); | |
| const docIndex = parseInt(searchParams.get('doc'), 10); | |
| const pageNumber = parseInt(searchParams.get('page'), 10); | |
| if (!timestamp || isNaN(docIndex) || isNaN(pageNumber)) { | |
| return NextResponse.json( | |
| { error: 'Missing timestamp, doc, or page parameter' }, | |
| { status: 400 } | |
| ); | |
| } | |
| const deleted = await deleteAnnotation(timestamp, docIndex, pageNumber); | |
| if (deleted) { | |
| return NextResponse.json({ success: true }); | |
| } | |
| return NextResponse.json({ error: 'Annotation not found' }, { status: 404 }); | |
| } catch (error) { | |
| console.error('Delete annotation error:', error); | |
| return NextResponse.json({ error: 'Failed to delete annotation: ' + error.message }, { status: 500 }); | |
| } | |
| } | |
| export async function PUT(request) { | |
| try { | |
| const body = await request.json(); | |
| const { timestamp, document_index, page_number, updates } = body; | |
| if (!timestamp || document_index === undefined || page_number === undefined || !updates) { | |
| return NextResponse.json( | |
| { error: 'Missing timestamp, document_index, page_number, or updates' }, | |
| { status: 400 } | |
| ); | |
| } | |
| const updated = await updateAnnotation(timestamp, document_index, page_number, updates); | |
| if (updated) { | |
| return NextResponse.json({ success: true, annotation: updated }); | |
| } | |
| return NextResponse.json({ error: 'Annotation not found' }, { status: 404 }); | |
| } catch (error) { | |
| console.error('Update annotation error:', error); | |
| return NextResponse.json({ error: 'Failed to update annotation: ' + error.message }, { status: 500 }); | |
| } | |
| } | |