Przebudowa strony
This commit is contained in:
82
src/pages/api/internet/addons.js
Normal file
82
src/pages/api/internet/addons.js
Normal file
@@ -0,0 +1,82 @@
|
||||
import Database from "better-sqlite3";
|
||||
|
||||
const DB_PATH = "./src/data/ServicesRange.db";
|
||||
|
||||
export async function GET() {
|
||||
const db = new Database(DB_PATH, { readonly: true });
|
||||
|
||||
try {
|
||||
const addonsRows = db
|
||||
.prepare(
|
||||
`
|
||||
SELECT id, name, type, description
|
||||
FROM internet_addons
|
||||
ORDER BY id
|
||||
`
|
||||
)
|
||||
.all();
|
||||
|
||||
const optionsRows = db
|
||||
.prepare(
|
||||
`
|
||||
SELECT id, addon_id, code, name, price
|
||||
FROM internet_addon_options
|
||||
ORDER BY addon_id, id
|
||||
`
|
||||
)
|
||||
.all();
|
||||
|
||||
const byAddon = new Map();
|
||||
|
||||
for (const addon of addonsRows) {
|
||||
byAddon.set(addon.id, {
|
||||
id: addon.id,
|
||||
name: addon.name,
|
||||
type: addon.type, // 'checkbox' / 'select'
|
||||
description: addon.description || "",
|
||||
options: [],
|
||||
});
|
||||
}
|
||||
|
||||
for (const opt of optionsRows) {
|
||||
const parent = byAddon.get(opt.addon_id);
|
||||
if (!parent) continue;
|
||||
parent.options.push({
|
||||
id: opt.id,
|
||||
code: opt.code,
|
||||
name: opt.name,
|
||||
price: opt.price,
|
||||
});
|
||||
}
|
||||
|
||||
const data = Array.from(byAddon.values());
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
ok: true,
|
||||
count: data.length,
|
||||
data,
|
||||
}),
|
||||
{
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/json; charset=utf-8",
|
||||
},
|
||||
}
|
||||
);
|
||||
} catch (err) {
|
||||
console.error("❌ Błąd w /api/internet/addons:", err);
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
ok: false,
|
||||
error: err.message || "DB_ERROR",
|
||||
}),
|
||||
{
|
||||
status: 500,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
}
|
||||
);
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user