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

99 lines
2.7 KiB
TypeScript

import { NextResponse } from 'next/server';
import { getDatabase } from '@/db';
/**
* GET /api/clusters
* Returns all clusters with statistics
*/
export async function GET() {
try {
const db = getDatabase();
const clusters = db.prepare(`
SELECT
c.*,
COUNT(DISTINCT lcm.library_id) as library_count,
COUNT(DISTINCT CASE WHEN m.type = 'video' THEN m.id END) as video_count,
COUNT(DISTINCT CASE WHEN m.type = 'photo' THEN m.id END) as photo_count,
COUNT(DISTINCT CASE WHEN m.type = 'text' THEN m.id END) as text_count,
COUNT(DISTINCT m.id) as media_count,
COALESCE(SUM(m.size), 0) as total_size
FROM clusters c
LEFT JOIN library_cluster_mapping lcm ON c.id = lcm.cluster_id
LEFT JOIN libraries l ON lcm.library_id = l.id
LEFT JOIN media m ON l.id = m.library_id
GROUP BY c.id
ORDER BY c.name
`).all();
return NextResponse.json(clusters);
} catch (error: any) {
console.error('Error fetching clusters:', error);
return NextResponse.json(
{ error: error.message },
{ status: 500 }
);
}
}
/**
* POST /api/clusters
* Creates a new cluster
*/
export async function POST(request: Request) {
try {
const db = getDatabase();
const { name, description, color, icon } = await request.json();
// Validation
if (!name || name.trim().length === 0) {
return NextResponse.json(
{ error: 'Cluster name is required' },
{ status: 400 }
);
}
if (name.length > 100) {
return NextResponse.json(
{ error: 'Cluster name must be 100 characters or less' },
{ status: 400 }
);
}
// Validate color format (basic hex validation)
if (color && !/^#[0-9A-Fa-f]{6}$/.test(color)) {
return NextResponse.json(
{ error: 'Invalid color format. Use hex format (#RRGGBB)' },
{ status: 400 }
);
}
const result = db.prepare(`
INSERT INTO clusters (name, description, color, icon)
VALUES (?, ?, ?, ?)
`).run(
name.trim(),
description || null,
color || '#6366f1',
icon || 'folder'
);
const cluster = db.prepare('SELECT * FROM clusters WHERE id = ?')
.get(result.lastInsertRowid);
return NextResponse.json(cluster, { status: 201 });
} catch (error: any) {
console.error('Error creating cluster:', error);
if (error.code === 'SQLITE_CONSTRAINT_UNIQUE') {
return NextResponse.json(
{ error: 'A cluster with this name already exists' },
{ status: 409 }
);
}
return NextResponse.json(
{ error: error.message },
{ status: 500 }
);
}
}