83 lines
2.7 KiB
JavaScript
83 lines
2.7 KiB
JavaScript
const express = require('express');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const app = express();
|
|
const DATA_DIR = process.env.DATA_DIR || __dirname;
|
|
const DATA_FILE = path.join(DATA_DIR, 'data.json');
|
|
|
|
// Seed data.json if it does not exist
|
|
if (!fs.existsSync(DATA_FILE)) {
|
|
fs.mkdirSync(DATA_DIR, { recursive: true });
|
|
fs.writeFileSync(DATA_FILE, JSON.stringify({ notes: [], nextId: 1 }, null, 2));
|
|
}
|
|
|
|
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) {
|
|
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 });
|
|
});
|
|
|
|
app.put('/api/reorder', (req, res) => {
|
|
const data = readData();
|
|
const { items } = req.body;
|
|
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'));
|