diff --git a/data/tasks.json b/data/tasks.json index 3747a16..1882cda 100644 --- a/data/tasks.json +++ b/data/tasks.json @@ -1,8 +1,8 @@ [ { "id": "1", - "title": "Learn Node.js Fundamentals", - "description": "Complete the Node.js basics course and build a simple server", + "title": "soco", + "description": "school", "status": "in-progress", "priority": "high", "dueDate": "2024-01-15", @@ -152,5 +152,89 @@ "subtasks": [], "createdAt": "2025-07-23T04:22:47.898Z", "updatedAt": "2025-07-23T04:22:47.899Z" + }, + { + "id": "5", + "title": "footbal", + "description": "play footbal", + "status": "player", + "priority": "lin up", + "dueDate": null, + "assignedTo": null, + "subtasks": [], + "createdAt": "2025-07-26T07:55:28.436Z", + "updatedAt": "2025-07-26T07:55:28.436Z" + }, + { + "id": "5", + "title": "player", + "description": "play footbal", + "status": "player", + "priority": "lin up", + "dueDate": null, + "assignedTo": null, + "subtasks": [], + "createdAt": "2025-07-26T07:57:46.716Z", + "updatedAt": "2025-07-26T07:57:46.718Z" + }, + { + "id": "5", + "title": "player", + "description": "play footbal", + "status": "player", + "priority": "lin up", + "dueDate": null, + "assignedTo": null, + "subtasks": [], + "createdAt": "2025-07-26T14:16:14.228Z", + "updatedAt": "2025-07-26T14:16:14.230Z" + }, + { + "id": 6, + "title": "sports", + "description": "team", + "status": "goal", + "priority": "you", + "dueDate": null, + "assignedTo": null, + "subtasks": [], + "createdAt": "2025-07-26T18:09:37.967Z", + "updatedAt": "2025-07-26T18:09:37.970Z" + }, + { + "id": 7, + "title": "sheeko", + "description": "team", + "status": "goal", + "priority": "you", + "dueDate": null, + "assignedTo": null, + "subtasks": [], + "createdAt": "2025-07-26T19:40:36.642Z", + "updatedAt": "2025-07-26T19:40:36.643Z" + }, + { + "id": 8, + "title": "waxprasho", + "description": "school", + "status": "true", + "priority": "english", + "dueDate": null, + "assignedTo": null, + "subtasks": [], + "createdAt": "2025-08-06T05:52:57.780Z", + "updatedAt": "2025-08-06T05:52:57.781Z" + }, + { + "id": 9, + "title": "soco", + "description": "school", + "status": "false", + "priority": "somali", + "dueDate": null, + "assignedTo": null, + "subtasks": [], + "createdAt": "2025-08-06T18:39:53.625Z", + "updatedAt": "2025-08-06T18:39:53.625Z" } ] \ No newline at end of file diff --git a/routes/tasks.js b/routes/tasks.js index b2bcfcb..35241f0 100644 --- a/routes/tasks.js +++ b/routes/tasks.js @@ -1,6 +1,7 @@ +import { error } from "console"; import express from "express"; import fs from "fs/promises"; -import path from "path"; +import path, { parse } from "path"; import { fileURLToPath } from "url"; const router = express.Router(); @@ -137,7 +138,7 @@ router.get("/tasks/:id", async (req, res) => { res.json({ success: true, data: task, - }); + }); } catch (error) { res.status(500).json({ success: false, @@ -153,13 +154,45 @@ router.post("/tasks", async (req, res) => { try { // TODO: Implement task creation // 1. Extract data from req.body (title, description, status, priority, etc.) + const { title, description, status, priority } = req.body; + // 2. Validate the data using validateTaskData function + + if (!title || !description || !status || !priority) { + return res.status(400).json({ error: "Missing required fild" }); + } + // 3. Get all existing tasks using getAllTasks() + 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; + // 5. Create a new task object with all required fields + const newTask = { + id: newId, + title: title, + description: description, + status: status, + priority: priority, + dueDate: null, + assignedTo: null, + 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() + await writeTasks(tasks); + // 8. Send success response with status 201 + res.status(201).json(newTask); // Temporary response - remove this when you implement the above res.status(501).json({ @@ -182,13 +215,35 @@ 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} = req.body; + // 3. Validate the data if status or priority is being updated + if (!title || !description) { + return res.status(404).json({ error: "Mising require fild"}) + } + // 4. Get all tasks and find the task by ID + const tasks = await getAllTasks(); + + const taskIndex = tasks.findIndex(task => String(task.id === id)) + // 5. Check if task exists, return 404 if not found + if (taskIndex === -1) { + return res.status(404).json({ success: false, message: "Task not found" }); + } + // 6. Update the task with new data + if(title) tasks[taskIndex].title = title; + if(description) tasks[taskIndex].description = description; + // 7. Save to file using writeTasks() + await writeTasks(tasks); + // 8. Send success response with the updated task + res.status(200).json(taskIndex); // Temporary response - remove this when you implement the above res.status(501).json({ @@ -209,19 +264,42 @@ 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 = getAllTasks(); + // 3. Check if task exists, return 404 if not found + const taskIndex = tasks.findIndex((task) => task.id === parseInt(id)); + // 4. Store the task before deletion (for response) + if (taskIndex === -2) { + + return res.status(404).json({ + success: false, + message: "task not found" + }) + } + // 5. Remove the task from the array + const deleteTask = tasks.spalice(taskIndex, 1)[0]; + // 6. Save to file using writeTasks() + await writeTasks(tasks); + // 7. Send success response with the deleted task + res.status(200).json({ + success: true, + message: "task delete succeesfully", + delete: deleteTask, + }) // 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", - }); + // res.status(501).json({ + // success: false, + // error: + // "DELETE endpoint not implemented yet - implement task deletion above", + // }); } catch (error) { res.status(500).json({ success: false,