From b6106b2cfd60e070f7a5ad47a39ce69db1d1b90a Mon Sep 17 00:00:00 2001 From: AbdiqadirAbdikrin Date: Sun, 30 Nov 2025 11:51:38 +0300 Subject: [PATCH] complete --- data/tasks.json | 44 ++----------- node | 0 routes/tasks.js | 123 +++++++++++++++++++++++++++++++---- task-management-system@1.0.0 | 0 4 files changed, 119 insertions(+), 48 deletions(-) create mode 100644 node create mode 100644 task-management-system@1.0.0 diff --git a/data/tasks.json b/data/tasks.json index 3747a16..e9e5404 100644 --- a/data/tasks.json +++ b/data/tasks.json @@ -1,41 +1,4 @@ [ - { - "id": "1", - "title": "Learn Node.js Fundamentals", - "description": "Complete the Node.js basics course and build a simple server", - "status": "in-progress", - "priority": "high", - "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", - "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 - } - ], - "createdAt": "2024-01-01T10:30:00.000Z", - "updatedAt": "2025-07-23T01:29:37.667Z" - }, { "id": "2", "title": "Build REST API", @@ -152,5 +115,12 @@ "subtasks": [], "createdAt": "2025-07-23T04:22:47.898Z", "updatedAt": "2025-07-23T04:22:47.899Z" + }, + { + "id": 6, + "title": "Learn new thing", + "description": "learn untilll u become bro", + "status": "in-progress", + "priority": "low" } ] \ No newline at end of file diff --git a/node b/node new file mode 100644 index 0000000..e69de29 diff --git a/routes/tasks.js b/routes/tasks.js index b2bcfcb..87f2f6e 100644 --- a/routes/tasks.js +++ b/routes/tasks.js @@ -1,5 +1,8 @@ +import { error } from "console"; import express from "express"; +import { stat } from "fs"; import fs from "fs/promises"; +import { get } from "http"; import path from "path"; import { fileURLToPath } from "url"; @@ -151,21 +154,62 @@ 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.) + const {title, description, status, priority} = req.body // 2. Validate the data using validateTaskData function + + if (!title || !description || !status) +{ + return res.status(400).json({message: "Missing required fieldsrequired"}) + } + // 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((s) => parseInt(s.id))) +1 : 1; + + // 5. Create a new task object with all required fields + const newTask = { + id: newID, + title, + description, + status, + priority: priority || null, + + + + } // 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(newID)({ + res.status(201).json({ success: true, + + // success: true, + // error: "POST endpoint not implemented yet - implement task creation above", + // 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({ @@ -182,18 +226,59 @@ 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} = req.body + + + // 3. Validate the data if status or priority is being updated + if (!title || !description || !status) { + return res.status(400).json({ message: "Missing fields for task update" }); + } + + + // 4. Get all tasks and find the task by ID + const Tasks = await getAllTasks() + + + const currenttask = Tasks.find((task) => task.id == Number(id)); + + + // 5. Check if task exists, return 404 if not found + if(!currenttask){ + return req.status(400).json({error: "is not updated"}) + } + // 6. Update the task with new data + const updatetask = { + id: Number(id), + title, + description, + status, + priority: priority || null, + + } + + + Tasks[currenttask] = updatetask; + + // 7. Save to file using writeTasks() + await writeTasks(Tasks) + // 8. Send success response with the updated task + res.status(200).json({ success: true, + // 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({ @@ -209,18 +294,34 @@ 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() + // 3. Check if task exists, return 404 if not found + const currentid = Tasks.find((task) => task.id == Number(id)) // 4. Store the task before deletion (for response) + if(!currentid){ + return res.status(404).json({error:"not found"}) + } + + // 5. Remove the task from the array + const deleteTasks = Tasks.filter((t) => t.id != Number(id)); + + // 6. Save to file using writeTasks() + await writeTasks(deleteTasksTasks) // 7. Send success response with the deleted task + res.status(200).json({ success: true, + + // 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({ diff --git a/task-management-system@1.0.0 b/task-management-system@1.0.0 new file mode 100644 index 0000000..e69de29