Skip to content
Open
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
100 changes: 46 additions & 54 deletions routes/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,83 +151,75 @@ 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.)
// 2. Validate the data using validateTaskData function
// 3. Get all existing tasks using getAllTasks()
// 4. Generate a new ID for the task
// 5. Create a new task object with all required fields
// 6. Add the task to the tasks array
// 7. Save to file using writeTasks()
// 8. Send success response with status 201

// 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",
const taskData = req.body;
const newTask = await createTask(taskData);

res.status(201).json({
success: true,
data: newTask,
});
} catch (error) {
res.status(500).json({
res.status(400).json({
success: false,
error: "Error creating task",
}); // 500 = Server Error
});
}
});

// PUT /api/tasks/:id - Update task
// PUT requests are used to update existing resources
// 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
// 2. Get the update data from req.body
// 3. Validate the data if status or priority is being updated
// 4. Get all tasks and find the task by ID
// 5. Check if task exists, return 404 if not found
// 6. Update the task with new data
// 7. Save to file using writeTasks()
// 8. Send success response with the updated task

// 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",
const { id } = req.params;
const updateData = req.body;
const updatedTask = await updateTask(id, updateData);

res.json({
success: true,
data: updatedTask,
});
} catch (error) {
res.status(500).json({
success: false,
error: "Error updating task",
}); // 500 = Server Error
if (error.message === "Task not found") {
res.status(404).json({
success: false,
error: error.message,
});
} else {
res.status(400).json({
success: false,
error: error.message,
});
}
}
});


// DELETE /api/tasks/:id - Delete task
// 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
// 2. Get all tasks and find the task by ID
// 3. Check if task exists, return 404 if not found
// 4. Store the task before deletion (for response)
// 5. Remove the task from the array
// 6. Save to file using writeTasks()
// 7. Send success response with the deleted task

// 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",
const { id } = req.params;
const deletedTask = await deleteTask(id);

res.json({
success: true,
data: deletedTask,
});
} catch (error) {
res.status(500).json({
success: false,
error: "Error deleting task",
}); // 500 = Server Error
if (error.message === "Task not found") {
res.status(404).json({
success: false,
error: error.message,
});
} else {
res.status(500).json({
success: false,
error: "Error deleting task",
});
}
}
});

export default router;