Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions data/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,17 @@
"subtasks": [],
"createdAt": "2025-07-23T04:22:47.898Z",
"updatedAt": "2025-07-23T04:22:47.899Z"
},
{
"id": "6",
"title": "Tast",
"description": "Tast",
"status": "pending",
"priority": "medium",
"dueDate": null,
"assignedTo": null,
"subtasks": [],
"createdAt": "2025-08-10T07:28:07.185Z",
"updatedAt": "2025-08-10T07:28:07.185Z"
}
]
131 changes: 109 additions & 22 deletions routes/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,49 @@ router.get("/tasks/:id", async (req, res) => {
// req.body contains the data sent in the request body
router.post("/tasks", async (req, res) => {
try {
// TODO: Implement task creation
// 1. Extract data from req.body (title, description, status, priority, etc.)
// 1. Extract data from req.body
const { title, description, status, priority, dueDate, assignedTo, subtasks } = req.body;

// 2. Validate the data using validateTaskData function
// 3. Get all existing tasks using getAllTasks()
const validation = validateTaskData({ title, description, status, priority });
if (!validation.isValid) {
return res.status(400).json({
success: false,
error: validation.error,
});
}

// 3. Get all existing tasks
const tasks = await getAllTasks();

// 4. Generate a new ID for the task
const newId = (tasks.length > 0 ? Math.max(...tasks.map(task => parseInt(task.id))) + 1 : 1).toString();

// 5. Create a new task object with all required fields
const newTask = {
id: newId,
title,
description,
status,
priority,
dueDate: dueDate || null,
assignedTo: assignedTo || null,
subtasks: subtasks || [],
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString()
};

// 6. Add the task to the tasks array
tasks.push(newTask);

// 7. Save to file using writeTasks()
// 8. Send success response with status 201
await writeTasks(tasks);

// Temporary response - remove this when you implement the above
res.status(501).json({
success: false,
error:
"POST endpoint not implemented yet - implement task creation above",
// 8. Send success response with status 201
res.status(201).json({
success: true,
message: "Task created successfully",
data: newTask,
});
} catch (error) {
res.status(500).json({
Expand All @@ -180,20 +208,64 @@ router.post("/tasks", async (req, res) => {
// The entire resource is replaced with the new data
router.put("/tasks/:id", async (req, res) => {
try {
// TODO: Implement task update
// 1. Extract the task ID from req.params
const { id } = req.params;

// 2. Get the update data from req.body
const { title, description, status, priority, dueDate, assignedTo, subtasks } = req.body;

// 3. Validate the data if status or priority is being updated
if (status || priority) {
const validation = validateTaskData({
title: title || "temp",
description: description || "temp",
status: status || "pending",
priority: priority || "low"
});
if (!validation.isValid) {
return res.status(400).json({
success: false,
error: validation.error,
});
}
}

// 4. Get all tasks and find the task by ID
const tasks = await getAllTasks();
const taskIndex = tasks.findIndex((task) => task.id === id);

// 5. Check if task exists, return 404 if not found
if (taskIndex === -1) {
return res.status(404).json({
success: false,
error: "Task not found",
});
}

// 6. Update the task with new data
const existingTask = tasks[taskIndex];
const updatedTask = {
...existingTask,
title: title !== undefined ? title : existingTask.title,
description: description !== undefined ? description : existingTask.description,
status: status !== undefined ? status : existingTask.status,
priority: priority !== undefined ? priority : existingTask.priority,
dueDate: dueDate !== undefined ? dueDate : existingTask.dueDate,
assignedTo: assignedTo !== undefined ? assignedTo : existingTask.assignedTo,
subtasks: subtasks !== undefined ? subtasks : existingTask.subtasks,
updatedAt: new Date().toISOString()
};

tasks[taskIndex] = updatedTask;

// 7. Save to file using writeTasks()
// 8. Send success response with the updated task
await writeTasks(tasks);

// Temporary response - remove this when you implement the above
res.status(501).json({
success: false,
error: "PUT endpoint not implemented yet - implement task update above",
// 8. Send success response with the updated task
res.json({
success: true,
message: "Task updated successfully",
data: updatedTask,
});
} catch (error) {
res.status(500).json({
Expand All @@ -207,20 +279,35 @@ router.put("/tasks/:id", async (req, res) => {
// DELETE requests are used to remove resources
router.delete("/tasks/:id", async (req, res) => {
try {
// TODO: Implement task deletion
// 1. Extract the task ID from req.params
const { id } = req.params;

// 2. Get all tasks and find the task by ID
const tasks = await getAllTasks();
const taskIndex = tasks.findIndex((task) => task.id === id);

// 3. Check if task exists, return 404 if not found
if (taskIndex === -1) {
return res.status(404).json({
success: false,
error: "Task not found",
});
}

// 4. Store the task before deletion (for response)
const deletedTask = tasks[taskIndex];

// 5. Remove the task from the array
tasks.splice(taskIndex, 1);

// 6. Save to file using writeTasks()
// 7. Send success response with the deleted task
await writeTasks(tasks);

// Temporary response - remove this when you implement the above
res.status(501).json({
success: false,
error:
"DELETE endpoint not implemented yet - implement task deletion above",
// 7. Send success response with the deleted task
res.json({
success: true,
message: "Task deleted successfully",
data: deletedTask,
});
} catch (error) {
res.status(500).json({
Expand Down