Initial Priority Horizon board
This commit is contained in:
77
server.js
Normal file
77
server.js
Normal file
@@ -0,0 +1,77 @@
|
||||
const express = require('express');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const app = express();
|
||||
const DATA_FILE = path.join(__dirname, 'data.json');
|
||||
|
||||
app.use(express.json());
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
|
||||
function readData() {
|
||||
return JSON.parse(fs.readFileSync(DATA_FILE, 'utf8'));
|
||||
}
|
||||
function writeData(data) {
|
||||
fs.writeFileSync(DATA_FILE, JSON.stringify(data, null, 2));
|
||||
}
|
||||
|
||||
app.get('/api/notes', (req, res) => {
|
||||
const data = readData();
|
||||
const grouped = { now: [], soon: [], eventually: [] };
|
||||
data.notes.sort((a, b) => a.order - b.order).forEach(n => {
|
||||
if (grouped[n.column]) grouped[n.column].push(n);
|
||||
});
|
||||
res.json(grouped);
|
||||
});
|
||||
|
||||
app.post('/api/notes', (req, res) => {
|
||||
const { column, text } = req.body;
|
||||
if (!column || !text) return res.status(400).json({ error: 'column and text required' });
|
||||
const data = readData();
|
||||
const note = { id: data.nextId++, column, text, order: data.notes.filter(n => n.column === column).length };
|
||||
data.notes.push(note);
|
||||
writeData(data);
|
||||
res.status(201).json(note);
|
||||
});
|
||||
|
||||
app.patch('/api/notes/:id', (req, res) => {
|
||||
const data = readData();
|
||||
const note = data.notes.find(n => n.id === parseInt(req.params.id));
|
||||
if (!note) return res.status(404).json({ error: 'not found' });
|
||||
if (req.body.text !== undefined) note.text = req.body.text;
|
||||
if (req.body.column !== undefined) note.column = req.body.column;
|
||||
if (req.body.order !== undefined) note.order = req.body.order;
|
||||
if (req.body.reorder) {
|
||||
// reorder: [{id, order}, ...]
|
||||
req.body.reorder.forEach(r => {
|
||||
const n = data.notes.find(x => x.id === r.id);
|
||||
if (n) { n.order = r.order; if (r.column) n.column = r.column; }
|
||||
});
|
||||
}
|
||||
writeData(data);
|
||||
res.json(note);
|
||||
});
|
||||
|
||||
app.delete('/api/notes/:id', (req, res) => {
|
||||
const data = readData();
|
||||
const idx = data.notes.findIndex(n => n.id === parseInt(req.params.id));
|
||||
if (idx === -1) return res.status(404).json({ error: 'not found' });
|
||||
data.notes.splice(idx, 1);
|
||||
writeData(data);
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
// Bulk reorder endpoint
|
||||
app.put('/api/reorder', (req, res) => {
|
||||
const data = readData();
|
||||
const { items } = req.body; // [{id, column, order}]
|
||||
if (!items) return res.status(400).json({ error: 'items required' });
|
||||
items.forEach(r => {
|
||||
const n = data.notes.find(x => x.id === r.id);
|
||||
if (n) { n.order = r.order; n.column = r.column; }
|
||||
});
|
||||
writeData(data);
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
app.listen(3333, '0.0.0.0', () => console.log('Priority Horizon running on :3333'));
|
||||
Reference in New Issue
Block a user