uup
This commit is contained in:
44
app/routes/api.car-dataset.ts
Normal file
44
app/routes/api.car-dataset.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import type { LoaderFunctionArgs } from "@remix-run/node";
|
||||
import { json } from "@remix-run/node";
|
||||
import { getManufacturers, getModelsByManufacturer, getBodyType } from "~/lib/car-dataset-management.server";
|
||||
|
||||
export async function loader({ request }: LoaderFunctionArgs) {
|
||||
const url = new URL(request.url);
|
||||
const action = url.searchParams.get("action");
|
||||
const manufacturer = url.searchParams.get("manufacturer");
|
||||
const model = url.searchParams.get("model");
|
||||
|
||||
try {
|
||||
switch (action) {
|
||||
case "manufacturers": {
|
||||
const manufacturers = await getManufacturers();
|
||||
return json({ success: true, data: manufacturers });
|
||||
}
|
||||
|
||||
case "models": {
|
||||
if (!manufacturer) {
|
||||
return json({ success: false, error: "Manufacturer is required" }, { status: 400 });
|
||||
}
|
||||
const models = await getModelsByManufacturer(manufacturer);
|
||||
return json({ success: true, data: models });
|
||||
}
|
||||
|
||||
case "bodyType": {
|
||||
if (!manufacturer || !model) {
|
||||
return json({ success: false, error: "Manufacturer and model are required" }, { status: 400 });
|
||||
}
|
||||
const bodyType = await getBodyType(manufacturer, model);
|
||||
return json({ success: true, data: bodyType });
|
||||
}
|
||||
|
||||
default:
|
||||
return json({ success: false, error: "Invalid action" }, { status: 400 });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Car dataset API error:", error);
|
||||
return json(
|
||||
{ success: false, error: "Internal server error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user