From 02015542950a9f3722d3fd6e1f0360c182b46fd1 Mon Sep 17 00:00:00 2001 From: eng-ayzer Date: Sun, 10 Aug 2025 00:30:47 -0700 Subject: [PATCH] create post and put and delete opertion --- data/tasks.json | 12 +++++ routes/tasks.js | 131 ++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 121 insertions(+), 22 deletions(-) diff --git a/data/tasks.json b/data/tasks.json index 3747a16..aca6c61 100644 --- a/data/tasks.json +++ b/data/tasks.json @@ -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" } ] \ No newline at end of file diff --git a/routes/tasks.js b/routes/tasks.js index b2bcfcb..171f1a8 100644 --- a/routes/tasks.js +++ b/routes/tasks.js @@ -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({ @@ -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({ @@ -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({