feat: implement initial categories crud for human-in-the-loop norm builder
This commit is contained in:
@@ -76,6 +76,65 @@ const server = http.createServer((req, res) => {
|
||||
return;
|
||||
}
|
||||
|
||||
// --- CATEGORIES CRUD ---
|
||||
const CATEGORIES_FILE = path.join(CONFIG_DIR, 'categories.json');
|
||||
const readCategories = () => {
|
||||
if (fs.existsSync(CATEGORIES_FILE)) {
|
||||
try { return JSON.parse(fs.readFileSync(CATEGORIES_FILE, 'utf-8')); } catch (e) {}
|
||||
}
|
||||
return [];
|
||||
};
|
||||
|
||||
if (req.url.startsWith('/api/categories')) {
|
||||
if (req.method === 'GET') {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify(readCategories()));
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.method === 'POST' || req.method === 'PUT') {
|
||||
let body = '';
|
||||
req.on('data', chunk => body += chunk.toString());
|
||||
req.on('end', () => {
|
||||
try {
|
||||
const category = JSON.parse(body);
|
||||
let categories = readCategories();
|
||||
|
||||
if (req.method === 'POST') {
|
||||
category.id = Date.now().toString();
|
||||
categories.push(category);
|
||||
} else {
|
||||
const index = categories.findIndex(c => c.id === category.id);
|
||||
if (index !== -1) categories[index] = { ...categories[index], ...category };
|
||||
}
|
||||
|
||||
fs.writeFileSync(CATEGORIES_FILE, JSON.stringify(categories, null, 2));
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify(category));
|
||||
} catch (err) {
|
||||
res.writeHead(400); res.end(JSON.stringify({ error: 'Invalid payload' }));
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.method === 'DELETE') {
|
||||
const urlParams = new URL(req.url, `http://${req.headers.host}`);
|
||||
const id = urlParams.searchParams.get('id');
|
||||
if (id) {
|
||||
let categories = readCategories();
|
||||
categories = categories.filter(c => c.id !== id);
|
||||
fs.writeFileSync(CATEGORIES_FILE, JSON.stringify(categories, null, 2));
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: true }));
|
||||
} else {
|
||||
res.writeHead(400); res.end(JSON.stringify({ error: 'Missing id' }));
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
// --- END CATEGORIES CRUD ---
|
||||
|
||||
// Serve static files
|
||||
let filePath = path.join(DIST_DIR, req.url === '/' ? 'index.html' : req.url);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user