Changes First Version

This commit is contained in:
Honey committed 2026-02-16 19:04:03 +01:00
1 parent 42660a1f9c
commit 83a79dd346
4 files changed
+743 -305

No files matched your search

+55 -43
View File
@@ -37,19 +37,27 @@ function initDatabase() {
size TEXT NOT NULL,
reward TEXT,
completed INTEGER DEFAULT 0,
archived INTEGER DEFAULT 0,
expand_description INTEGER DEFAULT 0,
position INTEGER,
subtasks TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`);
`, (err) => {
if (err) return;
// Migrate: add columns if missing on existing databases
db.run(`ALTER TABLE tasks ADD COLUMN archived INTEGER DEFAULT 0`, () => {});
db.run(`ALTER TABLE tasks ADD COLUMN expand_description INTEGER DEFAULT 0`, () => {});
});
}
// API Routes
// Get all tasks
// Get tasks — ?archived=1 returns archived, default returns active
app.get('/api/tasks', (req, res) => {
db.all('SELECT * FROM tasks ORDER BY position ASC, created_at ASC', [], (err, rows) => {
const archived = req.query.archived === '1' ? 1 : 0;
db.all('SELECT * FROM tasks WHERE archived = ? ORDER BY position ASC, created_at ASC', [archived], (err, rows) => {
if (err) {
console.error('Error fetching tasks:', err);
return res.status(500).json({ error: 'Failed to fetch tasks' });
@@ -62,6 +70,8 @@ app.get('/api/tasks', (req, res) => {
size: row.size,
reward: row.reward,
completed: row.completed === 1,
archived: row.archived === 1,
expandDescription: row.expand_description === 1,
subtasks: row.subtasks ? JSON.parse(row.subtasks) : []
}));
@@ -71,13 +81,13 @@ app.get('/api/tasks', (req, res) => {
// Create a new task
app.post('/api/tasks', (req, res) => {
const { title, description, size, reward } = req.body;
const { title, description, size, reward, expandDescription } = req.body;
const id = Date.now().toString();
db.run(
`INSERT INTO tasks (id, title, description, size, reward, position)
VALUES (?, ?, ?, ?, ?, (SELECT COALESCE(MAX(position), 0) + 1 FROM tasks))`,
[id, title, description || '', size, reward || ''],
`INSERT INTO tasks (id, title, description, size, reward, expand_description, archived, position)
VALUES (?, ?, ?, ?, ?, ?, 0, (SELECT COALESCE(MAX(position), 0) + 1 FROM tasks WHERE archived = 0))`,
[id, title, description || '', size, reward || '', expandDescription ? 1 : 0],
function(err) {
if (err) {
console.error('Error creating task:', err);
@@ -91,6 +101,8 @@ app.post('/api/tasks', (req, res) => {
size,
reward: reward || '',
completed: false,
archived: false,
expandDescription: !!expandDescription,
subtasks: []
});
}
@@ -100,31 +112,47 @@ app.post('/api/tasks', (req, res) => {
// Update a task
app.put('/api/tasks/:id', (req, res) => {
const { id } = req.params;
const { title, description, size, reward, completed, subtasks } = req.body;
const { title, description, size, reward, completed, expandDescription, subtasks } = req.body;
db.run(
`UPDATE tasks
SET title = ?, description = ?, size = ?, reward = ?, completed = ?, subtasks = ?, updated_at = CURRENT_TIMESTAMP
SET title = ?, description = ?, size = ?, reward = ?, completed = ?, expand_description = ?, subtasks = ?, updated_at = CURRENT_TIMESTAMP
WHERE id = ?`,
[
title,
description || '',
size,
reward || '',
completed ? 1 : 0,
JSON.stringify(subtasks || []),
id
],
[title, description || '', size, reward || '', completed ? 1 : 0, expandDescription ? 1 : 0, JSON.stringify(subtasks || []), id],
function(err) {
if (err) {
console.error('Error updating task:', err);
return res.status(500).json({ error: 'Failed to update task' });
}
if (this.changes === 0) return res.status(404).json({ error: 'Task not found' });
res.json({ success: true });
}
);
});
if (this.changes === 0) {
return res.status(404).json({ error: 'Task not found' });
}
// Archive a task
app.put('/api/tasks/:id/archive', (req, res) => {
const { id } = req.params;
db.run(
`UPDATE tasks SET archived = 1, updated_at = CURRENT_TIMESTAMP WHERE id = ?`,
[id],
function(err) {
if (err) return res.status(500).json({ error: 'Failed to archive task' });
if (this.changes === 0) return res.status(404).json({ error: 'Task not found' });
res.json({ success: true });
}
);
});
// Unarchive (restore) a task
app.put('/api/tasks/:id/unarchive', (req, res) => {
const { id } = req.params;
db.run(
`UPDATE tasks SET archived = 0, position = (SELECT COALESCE(MAX(position), 0) + 1 FROM tasks WHERE archived = 0), updated_at = CURRENT_TIMESTAMP WHERE id = ?`,
[id],
function(err) {
if (err) return res.status(500).json({ error: 'Failed to unarchive task' });
if (this.changes === 0) return res.status(404).json({ error: 'Task not found' });
res.json({ success: true });
}
);
@@ -133,17 +161,9 @@ app.put('/api/tasks/:id', (req, res) => {
// Delete a task
app.delete('/api/tasks/:id', (req, res) => {
const { id } = req.params;
db.run('DELETE FROM tasks WHERE id = ?', [id], function(err) {
if (err) {
console.error('Error deleting task:', err);
return res.status(500).json({ error: 'Failed to delete task' });
}
if (this.changes === 0) {
return res.status(404).json({ error: 'Task not found' });
}
if (err) return res.status(500).json({ error: 'Failed to delete task' });
if (this.changes === 0) return res.status(404).json({ error: 'Task not found' });
res.json({ success: true });
});
});
@@ -151,10 +171,7 @@ app.delete('/api/tasks/:id', (req, res) => {
// Reorder tasks
app.post('/api/tasks/reorder', (req, res) => {
const taskIds = req.body;
if (!Array.isArray(taskIds)) {
return res.status(400).json({ error: 'Invalid task order data' });
}
if (!Array.isArray(taskIds)) return res.status(400).json({ error: 'Invalid task order data' });
const promises = taskIds.map((id, index) => {
return new Promise((resolve, reject) => {
@@ -178,19 +195,14 @@ app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '../public/index.html'));
});
// Start server
app.listen(PORT, () => {
console.log(`🚀 Learning Progress Tracker running on http://localhost:${PORT}`);
console.log(`Learning Progress Tracker running on http://localhost:${PORT}`);
});
// Graceful shutdown
process.on('SIGINT', () => {
db.close((err) => {
if (err) {
console.error('Error closing database:', err);
} else {
console.log('Database connection closed');
}
if (err) console.error('Error closing database:', err);
else console.log('Database connection closed');
process.exit(0);
});
});