feat: add database schema, migrations, and API endpoints for span annotations
- Add span_label_types and span_annotations tables to schema - Seed default span label types (bull_flag, bear_flag, etc.) - Implement CRUD API endpoints for span label types - Implement CRUD API endpoints for span annotations - Add time swap validation in POST endpoint (start_time <= end_time)
This commit is contained in:
parent
8a7eb1fb08
commit
dadf515406
11 changed files with 1131 additions and 0 deletions
87
src/app/api/span-annotations/[id]/route.ts
Normal file
87
src/app/api/span-annotations/[id]/route.ts
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/db';
|
||||
import { spanAnnotations } from '@/lib/db/schema';
|
||||
import { eq } from 'drizzle-orm';
|
||||
|
||||
// PATCH - Update span annotation
|
||||
export async function PATCH(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const body = await request.json();
|
||||
|
||||
const { label, confidence, outcome, notes, sub_spans } = body;
|
||||
|
||||
// Check if the span exists
|
||||
const existing = await db
|
||||
.select()
|
||||
.from(spanAnnotations)
|
||||
.where(eq(spanAnnotations.id, parseInt(id)))
|
||||
.limit(1);
|
||||
|
||||
if (existing.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Span annotation not found' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
// Build update object with only provided fields
|
||||
const updates: any = {};
|
||||
if (label !== undefined) updates.label = label;
|
||||
if (confidence !== undefined) updates.confidence = confidence;
|
||||
if (outcome !== undefined) updates.outcome = outcome;
|
||||
if (notes !== undefined) updates.notes = notes;
|
||||
if (sub_spans !== undefined) updates.sub_spans = sub_spans ? JSON.stringify(sub_spans) : null;
|
||||
|
||||
const result = await db
|
||||
.update(spanAnnotations)
|
||||
.set(updates)
|
||||
.where(eq(spanAnnotations.id, parseInt(id)))
|
||||
.returning();
|
||||
|
||||
return NextResponse.json(result[0]);
|
||||
} catch (error: any) {
|
||||
console.error('Error updating span annotation:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to update span annotation' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE - Delete span annotation
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
|
||||
// Check if the span exists
|
||||
const existing = await db
|
||||
.select()
|
||||
.from(spanAnnotations)
|
||||
.where(eq(spanAnnotations.id, parseInt(id)))
|
||||
.limit(1);
|
||||
|
||||
if (existing.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Span annotation not found' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
await db.delete(spanAnnotations).where(eq(spanAnnotations.id, parseInt(id)));
|
||||
|
||||
return NextResponse.json({ message: 'Span annotation deleted successfully' });
|
||||
} catch (error) {
|
||||
console.error('Error deleting span annotation:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to delete span annotation' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue