feat: add annotation types management system
- Created annotation_types table with name, display_name, color, category, icon - Implemented CRUD API endpoints for annotation types management - Built annotation types management UI page at /annotation-types - Updated Toolbox component to dynamically load and display annotation types - Updated CandleChart component to use dynamic annotation types for markers - Added seed functionality for default types (break_up, break_down, line) - Cleaned up duplicate migration files
This commit is contained in:
parent
50229e2ccf
commit
974d9f5598
13 changed files with 942 additions and 79 deletions
65
src/app/api/annotation-types/[id]/route.ts
Normal file
65
src/app/api/annotation-types/[id]/route.ts
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/db';
|
||||
import { annotationTypes } from '@/lib/db/schema';
|
||||
import { eq } from 'drizzle-orm';
|
||||
|
||||
type RouteContext = {
|
||||
params: Promise<{ id: string }>;
|
||||
};
|
||||
|
||||
// PATCH - Update annotation type
|
||||
export async function PATCH(
|
||||
request: NextRequest,
|
||||
context: RouteContext
|
||||
) {
|
||||
try {
|
||||
const { id } = await context.params;
|
||||
const body = await request.json();
|
||||
|
||||
const { name, display_name, color, category, icon, is_active } = body;
|
||||
|
||||
const updateData: any = {};
|
||||
if (name !== undefined) updateData.name = name;
|
||||
if (display_name !== undefined) updateData.display_name = display_name;
|
||||
if (color !== undefined) updateData.color = color;
|
||||
if (category !== undefined) updateData.category = category;
|
||||
if (icon !== undefined) updateData.icon = icon;
|
||||
if (is_active !== undefined) updateData.is_active = is_active;
|
||||
|
||||
if (Object.keys(updateData).length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'No fields to update' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const result = await db
|
||||
.update(annotationTypes)
|
||||
.set(updateData)
|
||||
.where(eq(annotationTypes.id, parseInt(id)))
|
||||
.returning();
|
||||
|
||||
if (result.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Annotation type not found' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(result[0]);
|
||||
} catch (error: any) {
|
||||
console.error('Error updating annotation type:', error);
|
||||
|
||||
if (error.message?.includes('UNIQUE constraint failed')) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Annotation type with this name already exists' },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to update annotation type' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
160
src/app/api/annotation-types/route.ts
Normal file
160
src/app/api/annotation-types/route.ts
Normal file
|
|
@ -0,0 +1,160 @@
|
|||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/db';
|
||||
import { annotationTypes, annotations } from '@/lib/db/schema';
|
||||
import { eq } from 'drizzle-orm';
|
||||
|
||||
// GET - List all annotation types
|
||||
export async function GET() {
|
||||
try {
|
||||
const types = await db.select().from(annotationTypes);
|
||||
return NextResponse.json(types);
|
||||
} catch (error) {
|
||||
console.error('Error fetching annotation types:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch annotation types' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// POST - Create new annotation type or seed defaults
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
|
||||
// Special case: seed default types
|
||||
if (body.action === 'seed') {
|
||||
const existing = await db.select().from(annotationTypes);
|
||||
|
||||
if (existing.length > 0) {
|
||||
return NextResponse.json({ message: 'Types already seeded' });
|
||||
}
|
||||
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const defaultTypes = [
|
||||
{
|
||||
name: 'break_up',
|
||||
display_name: 'Break Up',
|
||||
color: '#10b981',
|
||||
category: 'marker',
|
||||
icon: 'arrowUp',
|
||||
is_active: 1,
|
||||
created_at: now,
|
||||
},
|
||||
{
|
||||
name: 'break_down',
|
||||
display_name: 'Break Down',
|
||||
color: '#ef4444',
|
||||
category: 'marker',
|
||||
icon: 'arrowDown',
|
||||
is_active: 1,
|
||||
created_at: now,
|
||||
},
|
||||
{
|
||||
name: 'line',
|
||||
display_name: 'Line',
|
||||
color: '#3b82f6',
|
||||
category: 'line',
|
||||
icon: 'line',
|
||||
is_active: 1,
|
||||
created_at: now,
|
||||
},
|
||||
];
|
||||
|
||||
await db.insert(annotationTypes).values(defaultTypes);
|
||||
return NextResponse.json({ message: 'Default types seeded successfully' });
|
||||
}
|
||||
|
||||
// Regular create
|
||||
const { name, display_name, color, category, icon } = body;
|
||||
|
||||
if (!name || !display_name || !color || !category) {
|
||||
return NextResponse.json(
|
||||
{ error: 'name, display_name, color, and category are required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const result = await db
|
||||
.insert(annotationTypes)
|
||||
.values({
|
||||
name,
|
||||
display_name,
|
||||
color,
|
||||
category,
|
||||
icon: icon || null,
|
||||
is_active: 1,
|
||||
created_at: Math.floor(Date.now() / 1000),
|
||||
})
|
||||
.returning();
|
||||
|
||||
return NextResponse.json(result[0], { status: 201 });
|
||||
} catch (error: any) {
|
||||
console.error('Error creating annotation type:', error);
|
||||
|
||||
if (error.message?.includes('UNIQUE constraint failed')) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Annotation type with this name already exists' },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to create annotation type' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE - Delete annotation type (only if no annotations use it)
|
||||
export async function DELETE(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = request.nextUrl;
|
||||
const id = searchParams.get('id');
|
||||
|
||||
if (!id) {
|
||||
return NextResponse.json(
|
||||
{ error: 'id parameter is required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Check if the type exists
|
||||
const type = await db
|
||||
.select()
|
||||
.from(annotationTypes)
|
||||
.where(eq(annotationTypes.id, parseInt(id)))
|
||||
.limit(1);
|
||||
|
||||
if (type.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Annotation type not found' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
// Check if any annotations use this type
|
||||
const existingAnnotations = await db
|
||||
.select()
|
||||
.from(annotations)
|
||||
.where(eq(annotations.label_type, type[0].name))
|
||||
.limit(1);
|
||||
|
||||
if (existingAnnotations.length > 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Cannot delete annotation type: annotations exist with this type' },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
await db.delete(annotationTypes).where(eq(annotationTypes.id, parseInt(id)));
|
||||
|
||||
return NextResponse.json({ message: 'Annotation type deleted successfully' });
|
||||
} catch (error) {
|
||||
console.error('Error deleting annotation type:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to delete annotation type' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue