-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.js
More file actions
97 lines (88 loc) · 3.09 KB
/
tasks.js
File metadata and controls
97 lines (88 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// src/routes/tasks.js
const express = require('express');
const router = express.Router();
const db = require('../db');
// Helper to run DB queries as promises
const run = (sql, params=[]) => new Promise((resolve, reject) => {
db.run(sql, params, function(err) {
if (err) return reject(err);
resolve({ id: this.lastID, changes: this.changes });
});
});
const get = (sql, params=[]) => new Promise((resolve, reject) => {
db.get(sql, params, (err, row) => {
if (err) return reject(err);
resolve(row);
});
});
const all = (sql, params=[]) => new Promise((resolve, reject) => {
db.all(sql, params, (err, rows) => {
if (err) return reject(err);
resolve(rows);
});
});
// GET /api/tasks
router.get('/', async (req, res) => {
try {
const tasks = await all('SELECT * FROM tasks ORDER BY datetime(createdAt) DESC');
res.json(tasks);
} catch (err) {
res.status(500).json({ error: 'Failed to fetch tasks', details: err.message });
}
});
// GET /api/tasks/:id
router.get('/:id', async (req, res) => {
try {
const task = await get('SELECT * FROM tasks WHERE id = ?', [req.params.id]);
if (!task) return res.status(404).json({ error: 'Task not found' });
res.json(task);
} catch (err) {
res.status(500).json({ error: 'Failed to fetch task', details: err.message });
}
});
// POST /api/tasks
router.post('/', async (req, res) => {
try {
const { title, description = '', status = 'open' } = req.body;
if (!title || typeof title !== 'string') {
return res.status(400).json({ error: 'Title is required and must be a string' });
}
const result = await run(
'INSERT INTO tasks (title, description, status) VALUES (?, ?, ?)',
[title.trim(), description, status]
);
const created = await get('SELECT * FROM tasks WHERE id = ?', [result.id]);
res.status(201).json(created);
} catch (err) {
res.status(500).json({ error: 'Failed to create task', details: err.message });
}
});
// PUT /api/tasks/:id
router.put('/:id', async (req, res) => {
try {
const id = req.params.id;
const existing = await get('SELECT * FROM tasks WHERE id = ?', [id]);
if (!existing) return res.status(404).json({ error: 'Task not found' });
const { title = existing.title, description = existing.description, status = existing.status } = req.body;
await run(
'UPDATE tasks SET title = ?, description = ?, status = ?, updatedAt = CURRENT_TIMESTAMP WHERE id = ?',
[title, description, status, id]
);
const updated = await get('SELECT * FROM tasks WHERE id = ?', [id]);
res.json(updated);
} catch (err) {
res.status(500).json({ error: 'Failed to update task', details: err.message });
}
});
// DELETE /api/tasks/:id
router.delete('/:id', async (req, res) => {
try {
const id = req.params.id;
const result = await run('DELETE FROM tasks WHERE id = ?', [id]);
if (result.changes === 0) return res.status(404).json({ error: 'Task not found' });
res.status(204).send();
} catch (err) {
res.status(500).json({ error: 'Failed to delete task', details: err.message });
}
});
module.exports = router;