-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
279 lines (240 loc) · 8.18 KB
/
index.js
File metadata and controls
279 lines (240 loc) · 8.18 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
const express = require('express');
const bodyParser = require('body-parser');
const db = require('./database');
const app = express();
// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
// Set view engine
app.set('view engine', 'ejs');
app.set('views', './views');
// Initialize database
db.initialize();
// Validation helpers
const validateTitle = (title) => {
if (!title || typeof title !== 'string') {
return { valid: false, error: 'Title is required and must be a string' };
}
title = title.trim();
if (title.length === 0) {
return { valid: false, error: 'Title cannot be empty' };
}
if (title.length > 255) {
return { valid: false, error: 'Title cannot exceed 255 characters' };
}
return { valid: true };
};
const validateDescription = (description) => {
if (description && typeof description !== 'string') {
return { valid: false, error: 'Description must be a string' };
}
if (description && description.length > 1000) {
return { valid: false, error: 'Description cannot exceed 1000 characters' };
}
return { valid: true };
};
const validatePriority = (priority) => {
if (priority && !['low', 'medium', 'high'].includes(priority)) {
return { valid: false, error: 'Priority must be low, medium, or high' };
}
return { valid: true };
};
const validateCompleted = (completed) => {
if (completed !== undefined && completed !== null) {
if (![0, 1, '0', '1'].includes(completed)) {
return { valid: false, error: 'Completed must be 0 or 1' };
}
}
return { valid: true };
};
const validateTaskId = (id) => {
if (!id || isNaN(parseInt(id))) {
return { valid: false, error: 'Invalid task ID' };
}
return { valid: true };
};
// Routes
app.get('/', (req, res) => {
db.getAllTasks((err, tasks) => {
if (err) {
console.error('Error retrieving tasks:', err);
res.status(500).render('error', { message: 'Error retrieving tasks' });
return;
}
res.render('index', { tasks });
});
});
// Add routes for tasks: GET all tasks, POST new task
app.get('/tasks', (req, res) => {
db.getAllTasks((err, tasks) => {
if (err) {
console.error('Error retrieving tasks:', err);
res.status(500).render('error', { message: 'Error retrieving tasks' });
return;
}
res.render('index', { tasks });
});
});
app.post('/tasks', (req, res) => {
const { title, description, priority } = req.body;
// Validate title
const titleValidation = validateTitle(title);
if (!titleValidation.valid) {
return res.status(400).render('error', { message: titleValidation.error });
}
// Validate description
const descriptionValidation = validateDescription(description);
if (!descriptionValidation.valid) {
return res.status(400).render('error', { message: descriptionValidation.error });
}
// Validate priority
const priorityValidation = validatePriority(priority);
if (!priorityValidation.valid) {
return res.status(400).render('error', { message: priorityValidation.error });
}
db.createTask(title.trim(), description ? description.trim() : '', priority || 'medium', (err, id) => {
if (err) {
console.error('Error creating task:', err);
return res.status(500).render('error', { message: 'Error creating task' });
}
res.redirect('/tasks');
});
});
// API Routes
app.post('/api/tasks', (req, res) => {
const { title, description, priority } = req.body;
// Validate title
const titleValidation = validateTitle(title);
if (!titleValidation.valid) {
return res.status(400).json({ error: titleValidation.error });
}
// Validate description
const descriptionValidation = validateDescription(description);
if (!descriptionValidation.valid) {
return res.status(400).json({ error: descriptionValidation.error });
}
// Validate priority
const priorityValidation = validatePriority(priority);
if (!priorityValidation.valid) {
return res.status(400).json({ error: priorityValidation.error });
}
db.createTask(title.trim(), description ? description.trim() : '', priority || 'medium', (err, id) => {
if (err) {
console.error('Error creating task:', err);
return res.status(500).json({ error: 'Error creating task', details: err.message });
}
res.status(201).json({
id,
title: title.trim(),
description: description ? description.trim() : '',
priority: priority || 'medium',
completed: 0,
created_at: new Date().toISOString()
});
});
});
app.get('/api/tasks', (req, res) => {
db.getAllTasks((err, tasks) => {
if (err) {
console.error('Error retrieving tasks:', err);
return res.status(500).json({ error: 'Error retrieving tasks', details: err.message });
}
res.json(tasks);
});
});
app.get('/api/tasks/:id', (req, res) => {
// Validate task ID
const idValidation = validateTaskId(req.params.id);
if (!idValidation.valid) {
return res.status(400).json({ error: idValidation.error });
}
db.getTaskById(req.params.id, (err, task) => {
if (err) {
console.error('Error retrieving task:', err);
return res.status(500).json({ error: 'Error retrieving task', details: err.message });
}
if (!task) {
return res.status(404).json({ error: 'Task not found' });
}
res.json(task);
});
});
app.put('/api/tasks/:id', (req, res) => {
const { title, description, priority, completed } = req.body;
// Validate task ID
const idValidation = validateTaskId(req.params.id);
if (!idValidation.valid) {
return res.status(400).json({ error: idValidation.error });
}
// Validate at least one field is provided
if (!title && !description && !priority && completed === undefined) {
return res.status(400).json({ error: 'At least one field (title, description, priority, or completed) must be provided' });
}
// Validate title if provided
if (title !== undefined) {
const titleValidation = validateTitle(title);
if (!titleValidation.valid) {
return res.status(400).json({ error: titleValidation.error });
}
}
// Validate description if provided
if (description !== undefined) {
const descriptionValidation = validateDescription(description);
if (!descriptionValidation.valid) {
return res.status(400).json({ error: descriptionValidation.error });
}
}
// Validate priority if provided
if (priority !== undefined) {
const priorityValidation = validatePriority(priority);
if (!priorityValidation.valid) {
return res.status(400).json({ error: priorityValidation.error });
}
}
// Validate completed if provided
if (completed !== undefined) {
const completedValidation = validateCompleted(completed);
if (!completedValidation.valid) {
return res.status(400).json({ error: completedValidation.error });
}
}
db.updateTask(req.params.id, title, description, priority, completed, (err) => {
if (err) {
console.error('Error updating task:', err);
return res.status(500).json({ error: 'Error updating task', details: err.message });
}
res.json({ message: 'Task updated successfully' });
});
});
app.delete('/api/tasks/:id', (req, res) => {
// Validate task ID
const idValidation = validateTaskId(req.params.id);
if (!idValidation.valid) {
return res.status(400).json({ error: idValidation.error });
}
db.deleteTask(req.params.id, (err) => {
if (err) {
console.error('Error deleting task:', err);
return res.status(500).json({ error: 'Error deleting task', details: err.message });
}
res.json({ message: 'Task deleted successfully' });
});
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error('Unhandled error:', err);
res.status(500).json({
error: 'Internal server error',
details: process.env.NODE_ENV === 'development' ? err.message : undefined
});
});
// 404 handler
app.use((req, res) => {
res.status(404).json({ error: 'Route not found' });
});
// Start server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Task Manager app running on http://localhost:${PORT}`);
});