diff --git a/data/tasks.json b/data/tasks.json index 3747a16..2539ede 100644 --- a/data/tasks.json +++ b/data/tasks.json @@ -1,115 +1,22 @@ [ { "id": "1", - "title": "Learn Node.js Fundamentals", - "description": "Complete the Node.js basics course and build a simple server", - "status": "in-progress", - "priority": "high", + "title": "JavaScript", + "description": "Build JavaScript", + "status": "pending", + "priority": "medium", "dueDate": "2024-01-15", "assignedTo": "John Doe", "subtasks": [ { - "id": "1.1", - "title": "Install Node.js", - "completed": true, - "description": "Download and install Node.js on your machine" - }, - { - "id": "1.2", - "title": "Create First Server", - "completed": false, - "description": "Build a simple HTTP server using Express.js" - }, - { - "id": "1.3", - "title": "Handle Routes", + "title": "Install JavaScript", "completed": false, - "description": "Learn how to create and handle different routes" - }, - { - "id": "1.4", - "title": "Test Subtask", - "description": "This is a test subtask", - "completed": false + "description": "Download and install JavaScript on your machine" } ], "createdAt": "2024-01-01T10:30:00.000Z", "updatedAt": "2025-07-23T01:29:37.667Z" }, - { - "id": "2", - "title": "Build REST API", - "description": "Create a complete REST API with CRUD operations", - "status": "pending", - "priority": "medium", - "dueDate": "2024-01-20", - "assignedTo": "Jane Smith", - "subtasks": [ - { - "id": "2.1", - "title": "Design API Structure", - "completed": false, - "description": "Plan the API endpoints and data structure" - }, - { - "id": "2.2", - "title": "Implement GET Routes", - "completed": false, - "description": "Create routes to retrieve data" - }, - { - "id": "2.3", - "title": "Implement POST Routes", - "completed": false, - "description": "Create routes to add new data" - }, - { - "id": "2.4", - "title": "Add Validation", - "completed": false, - "description": "Implement input validation and error handling" - } - ], - "createdAt": "2024-01-02T11:00:00.000Z", - "updatedAt": "2024-01-02T11:00:00.000Z" - }, - { - "id": "3", - "title": "Database Integration", - "description": "Connect the API to a database system", - "status": "completed", - "priority": "low", - "dueDate": "2024-01-10", - "assignedTo": "Mike Johnson", - "subtasks": [ - { - "id": "3.1", - "title": "Choose Database", - "completed": true, - "description": "Select appropriate database (MongoDB/PostgreSQL)" - }, - { - "id": "3.2", - "title": "Setup Connection", - "completed": true, - "description": "Configure database connection" - }, - { - "id": "3.3", - "title": "Create Models", - "completed": true, - "description": "Define data models and schemas" - }, - { - "id": "3.4", - "title": "Update API", - "completed": true, - "description": "Modify API to use database instead of JSON files" - } - ], - "createdAt": "2024-01-03T09:15:00.000Z", - "updatedAt": "2024-01-08T16:45:00.000Z" - }, { "id": "4", "title": "Frontend Development", @@ -143,14 +50,60 @@ }, { "id": "5", - "title": "Test", - "description": "Test", - "status": "pending", - "priority": "medium", + "title": "Full Stuck", + "description": "Frontend and Backend", + "status": "in-progress", + "priority": "high", "dueDate": null, "assignedTo": null, - "subtasks": [], + "subtasks": [ + { + "title": "Install React and Node.js", + "completed": false, + "description": "Download and install JReact and Node.js on your machine" + } + ], "createdAt": "2025-07-23T04:22:47.898Z", "updatedAt": "2025-07-23T04:22:47.899Z" + }, + { + "id": 6, + "title": "javaScript and c++", + "description": "Full Complated Course", + "status": null, + "priority": null, + "dueDate": "2025-11-21T05:14:06.849Z", + "subtasks": [] + }, + { + "id": 7, + "title": "JavaScript", + "description": "Build JavaScript", + "status": "pending", + "priority": "medium", + "dueDate": "2025-11-21T09:45:12.683Z", + "subtasks": [ + { + "title": "Install JavaScript", + "completed": false, + "description": "Download and install JavaScript on your machine" + } + ] + }, + { + "id": 8, + "title": "React & NODE JS", + "description": "Learning New Skills", + "status": "in-progress", + "priority": "high", + "dueDate": "2025-11-22T05:13:01.056Z", + "assignedTo": null, + "subtasks": [ + { + "title": "Install React", + "completed": false, + "description": "Download and install React on your machine" + } + ] } ] \ No newline at end of file diff --git a/routes/tasks.js b/routes/tasks.js index b2bcfcb..1c21062 100644 --- a/routes/tasks.js +++ b/routes/tasks.js @@ -1,3 +1,4 @@ +import { create } from "domain"; import express from "express"; import fs from "fs/promises"; import path from "path"; @@ -153,20 +154,55 @@ 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, assignedTo, subtasks } = + req.body; + // 2. Validate the data using validateTaskData function + const validate = validateTaskData({ + title, + description, + status, + priority, + assignedTo, + subtasks, + }); + + if (!validate) { + return res.status(400).json({ error: "Missing required fields" }); + } // 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((t) => parseInt(t.id))) + 1 : 1; + // 5. Create a new task object with all required fields + const newTask = { + id: newID, + title, + description, + status: status || null, + priority: priority || null, + dueDate: new Date().toISOString(), + assignedTo: assignedTo || null, + subtasks: subtasks || [], + }; // 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({ - success: false, - error: - "POST endpoint not implemented yet - implement task creation above", - }); + // res.status(501).json({ + // success: false, + // error: + // "POST endpoint not implemented yet - implement task creation above", + // }); } catch (error) { res.status(500).json({ success: false, @@ -182,19 +218,44 @@ router.put("/tasks/:id", async (req, res) => { try { // TODO: Implement task update // 1. Extract the task ID from req.params + const taskId = req.params.id; + // 2. Get the update data from req.body + const updateData = req.body; + // console.log(req.body) + // 3. Validate the data if status or priority is being updated + if ("status" in updateData && !updateData.status) { + return res.status(400).json({ error: "Missing required fields" }); + } + + if ("priority" in updateData && !updateData.priority) { + return res.status(400).json({ error: "Missing required fields" }); + } + // 4. Get all tasks and find the task by ID + const tasks = await getAllTasks(); + const task = tasks.find((t) => t.id == taskId); + // 5. Check if task exists, return 404 if not found + if (!task) { + return res.status(404).json({ error: "Task not found" }); + } + // 6. Update the task with new data + Object.assign(task, updateData); + // 7. Save to file using writeTasks() + await writeTasks(tasks); + // 8. Send success response with the updated task + res.status(200).json(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", - }); + // res.status(501).json({ + // success: false, + // error: "PUT endpoint not implemented yet - implement task update above", + // }); } catch (error) { res.status(500).json({ success: false, @@ -209,19 +270,35 @@ router.delete("/tasks/:id", async (req, res) => { try { // TODO: Implement task deletion // 1. Extract the task ID from req.params + const deletedID = req.params.id; + // 2. Get all tasks and find the task by ID + const tasks = await getAllTasks(); + const index = tasks.findIndex((t) => t.id === deletedID); + // 3. Check if task exists, return 404 if not found + if (index === -1) { + return res.status(404).json({ error: "Task not found" }); + } + // 4. Store the task before deletion (for response) + const deletedTask = tasks[index]; + // 5. Remove the task from the array + tasks.splice(index, 1); + // 6. Save to file using writeTasks() + await writeTasks(tasks); + // 7. Send success response with the deleted task + res.status(202).json(deletedTask); // 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,