refactored categories code, now a seperate modifiable file
style changes and refinements
This commit is contained in:
71
server.js
71
server.js
@@ -5,6 +5,7 @@ const sqlite3 = require('sqlite3').verbose();
|
||||
const path = require('path');
|
||||
const http = require('http');
|
||||
const { Server } = require('socket.io');
|
||||
const fs = require('fs');
|
||||
|
||||
const app = express();
|
||||
const server = http.createServer(app);
|
||||
@@ -63,6 +64,76 @@ app.post('/api/positions', (req, res) => {
|
||||
);
|
||||
});
|
||||
|
||||
// Serve categories and fruits from a JSON file
|
||||
app.get('/api/categories', (req, res) => {
|
||||
const categoriesPath = path.join(__dirname, 'categories.json');
|
||||
fs.readFile(categoriesPath, 'utf8', (err, data) => {
|
||||
if (err) return res.status(500).json({ error: 'Could not load categories' });
|
||||
try {
|
||||
const categories = JSON.parse(data);
|
||||
res.json(categories);
|
||||
} catch (e) {
|
||||
res.status(500).json({ error: 'Invalid categories file' });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Add a fruit to a category in categories.json
|
||||
app.post('/api/add-fruit', (req, res) => {
|
||||
const { category, fruit } = req.body;
|
||||
if (!category || !fruit) {
|
||||
return res.status(400).json({ error: 'category and fruit required' });
|
||||
}
|
||||
const categoriesPath = path.join(__dirname, 'categories.json');
|
||||
fs.readFile(categoriesPath, 'utf8', (err, data) => {
|
||||
if (err) return res.status(500).json({ error: 'Could not load categories' });
|
||||
let categories;
|
||||
try {
|
||||
categories = JSON.parse(data);
|
||||
} catch (e) {
|
||||
return res.status(500).json({ error: 'Invalid categories file' });
|
||||
}
|
||||
const cat = categories.find(c => c.name === category);
|
||||
if (!cat) return res.status(404).json({ error: 'Category not found' });
|
||||
// Prevent duplicates
|
||||
if (cat.fruits.includes(fruit)) {
|
||||
return res.status(400).json({ error: 'Fruit already exists in category' });
|
||||
}
|
||||
cat.fruits.push(fruit);
|
||||
fs.writeFile(categoriesPath, JSON.stringify(categories, null, 2), err2 => {
|
||||
if (err2) return res.status(500).json({ error: 'Could not save categories' });
|
||||
res.json({ success: true });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// Delete a fruit from a category in categories.json
|
||||
app.post('/api/delete-fruit', (req, res) => {
|
||||
const { category, fruit } = req.body;
|
||||
if (!category || !fruit) {
|
||||
return res.status(400).json({ error: 'category and fruit required' });
|
||||
}
|
||||
const categoriesPath = path.join(__dirname, 'categories.json');
|
||||
fs.readFile(categoriesPath, 'utf8', (err, data) => {
|
||||
if (err) return res.status(500).json({ error: 'Could not load categories' });
|
||||
let categories;
|
||||
try {
|
||||
categories = JSON.parse(data);
|
||||
} catch (e) {
|
||||
return res.status(500).json({ error: 'Invalid categories file' });
|
||||
}
|
||||
const cat = categories.find(c => c.name === category);
|
||||
if (!cat) return res.status(404).json({ error: 'Category not found' });
|
||||
const idx = cat.fruits.indexOf(fruit);
|
||||
if (idx === -1) return res.status(404).json({ error: 'Fruit not found in category' });
|
||||
cat.fruits.splice(idx, 1);
|
||||
fs.writeFile(categoriesPath, JSON.stringify(categories, null, 2), err2 => {
|
||||
if (err2) return res.status(500).json({ error: 'Could not save categories' });
|
||||
res.json({ success: true });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// start server
|
||||
const PORT = process.env.PORT || 3085;
|
||||
server.listen(PORT, () => {
|
||||
|
||||
Reference in New Issue
Block a user