-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
104 lines (76 loc) · 2.6 KB
/
index.js
File metadata and controls
104 lines (76 loc) · 2.6 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
// Import required libraries
const express = require('express');
const { v4: uuidv4 } = require('uuid'); // Used to generate unique IDs
// Create an Express app
const app = express();
// app.use here means that we are using middleware to parse JSON bodies. Middleware is a function that runs before the request reaches the route handler.
app.use(express.json());
// In-memory task storage (data is lost when server restarts)
let tasks = [];
// Create a new task (POST /tasks)
app.post('/tasks', (req, res) => {
const { title, description, status } = req.body;
// Validation: title must exist
if (!title) {
return res.status(400).send('Title is required');
}
// Validation: status must be either 'pending' or 'completed'
if (status && status !== 'pending' && status !== 'completed') {
return res.status(400).send('Status must be "pending" or "completed"');
}
// Create the task object
const newTask = {
id: uuidv4(),
title,
description: description || '',
status: status || 'pending',
createdAt: new Date().toISOString()
};
// Save to memory
tasks.push(newTask);
// Return the newly created task
res.status(201).send(newTask);
});
// Get all tasks (GET /tasks)
app.get('/tasks', (req, res) => {
res.send(tasks);
});
// Get a single task by ID (GET /tasks/:id)
app.get('/tasks/:id', (req, res) => {
const task = tasks.find(t => t.id === req.params.id);
if (!task) {
return res.status(404).send('Task not found');
}
res.send(task);
});
// Update a task by ID (PUT /tasks/:id)
app.put('/tasks/:id', (req, res) => {
const task = tasks.find(t => t.id === req.params.id);
if (!task) {
return res.status(404).send('Task not found');
}
const { title, description, status } = req.body;
// Optional updates if values are sent
if (title !== undefined) task.title = title;
if (description !== undefined) task.description = description;
if (status !== undefined) {
if (status !== 'pending' && status !== 'completed') {
return res.status(400).send('Status must be "pending" or "completed"');
}
task.status = status;
}
res.send(task);
});
// Delete a task by ID (DELETE /tasks/:id)
app.delete('/tasks/:id', (req, res) => {
const index = tasks.findIndex(t => t.id === req.params.id);
if (index === -1) {
return res.status(404).send('Task not found'); // just in case agar task mojod na ho. not neccesarry wesy hi add kiya
}
tasks.splice(index, 1); // Remove task from array
res.send('Task deleted');
});
// Start the server on port 3000
app.listen(3000, () => {
console.log('Server running at http://localhost:3000');
});