2026-01-04 21:58:59 -05:00
|
|
|
import { NextResponse } from "next/server";
|
2026-02-21 04:42:46 -05:00
|
|
|
import { getCombos, createCombo, getComboByName } from "@/lib/localDb";
|
2026-01-04 21:58:59 -05:00
|
|
|
|
2026-03-06 04:41:02 -05:00
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
|
|
2026-01-04 21:58:59 -05:00
|
|
|
// Validate combo name: only a-z, A-Z, 0-9, -, _
|
2026-03-19 11:47:32 -04:00
|
|
|
const VALID_NAME_REGEX = /^[a-zA-Z0-9_.\-]+$/;
|
2026-01-04 21:58:59 -05:00
|
|
|
|
|
|
|
|
// GET /api/combos - Get all combos
|
|
|
|
|
export async function GET() {
|
|
|
|
|
try {
|
|
|
|
|
const combos = await getCombos();
|
|
|
|
|
return NextResponse.json({ combos });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log("Error fetching combos:", error);
|
|
|
|
|
return NextResponse.json({ error: "Failed to fetch combos" }, { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST /api/combos - Create new combo
|
|
|
|
|
export async function POST(request) {
|
|
|
|
|
try {
|
|
|
|
|
const body = await request.json();
|
2026-04-28 06:28:57 -04:00
|
|
|
const { name, models, kind } = body;
|
2026-01-04 21:58:59 -05:00
|
|
|
|
|
|
|
|
if (!name) {
|
|
|
|
|
return NextResponse.json({ error: "Name is required" }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate name format
|
|
|
|
|
if (!VALID_NAME_REGEX.test(name)) {
|
2026-03-19 11:47:32 -04:00
|
|
|
return NextResponse.json({ error: "Name can only contain letters, numbers, -, _ and ." }, { status: 400 });
|
2026-01-04 21:58:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if name already exists
|
|
|
|
|
const existing = await getComboByName(name);
|
|
|
|
|
if (existing) {
|
|
|
|
|
return NextResponse.json({ error: "Combo name already exists" }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-28 06:28:57 -04:00
|
|
|
const combo = await createCombo({ name, models: models || [], kind: kind || null });
|
2026-01-04 21:58:59 -05:00
|
|
|
|
|
|
|
|
return NextResponse.json(combo, { status: 201 });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log("Error creating combo:", error);
|
|
|
|
|
return NextResponse.json({ error: "Failed to create combo" }, { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
}
|