diff --git a/package.json b/package.json index 19faea1..ee7a4ed 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,11 @@ "scripts": { "start": "node server.js", "dev": "nodemon server.js", - "test": "echo \"Error: no test specified\" && exit 1" + "db:generate": "prisma generate", + "db:push": "prisma db push", + "db: migrate": "prisma migrate dev", + "db:studio": "prisma studio" + }, "keywords": [ "nodejs", @@ -19,13 +23,13 @@ "author": "Duraan", "license": "MIT", "dependencies": { - "express": "^4.18.2", - "cors": "^2.8.5", + "@prisma/client": "^5.7.1", "body-parser": "^1.20.2", - "@prisma/client": "^5.7.1" + "cors": "^2.8.5", + "express": "^4.18.2" }, "devDependencies": { "nodemon": "^3.0.1", - "prisma": "^5.7.1" + "prisma": "^5.22.0" } } diff --git a/prisma/schema.prisma b/prisma/schema.prisma index fb27ac1..61b979f 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -29,14 +29,31 @@ model Task { model Subtask { + id Int @id @default(autoincrement()) + title String + description String + completed Boolean @default(false) + task Task @relation(fields: [taskId], references: [id],onDelete:Cascade) + taskId String // Foreign key to Task + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt } + + // TODO: Create an enum for the status of the task with the following values (pending, in_progress, completed, cancelled) enum TaskStatus { - + pending + in_progress + completed + cancelled } // Create an enum for the priority of the task with the following values (low, medium, high, urgent) enum Priority { + low + medium + high + urgent } \ No newline at end of file diff --git a/services/taskServices.js b/services/taskServices.js index 73012bb..f51bbb9 100644 --- a/services/taskServices.js +++ b/services/taskServices.js @@ -8,16 +8,22 @@ export async function getAllTasks() { }); } + + // Get task by ID export async function getTaskById(id) { try { - - // TODO: Check if task exists - - // TODO: If not, throw an error - - // TODO: If it does, return the task - + // TODO: Check if task exists + const student = await prisma.student.findUnique({ + where:{id} , + }) + // TODO: If not, throw an error + + if (!student){ + throw new Error('student not found') + } + // TODO: If it does, return the task + return student; } catch (error) { throw new Error(`Error retrieving task: ${error.message}`); @@ -32,8 +38,27 @@ export async function createTask(taskData) { taskData.status === "in-progress" ? "in_progress" : taskData.status; - // TODO: Create the new task where all the task data is in "taskData", also create the subtasks with the data in "taskData.subtasks". Return the created task and it's subtasks using the include option. - + // TODO: Create the new task where all the task data is in "taskData", also create the subtasks with the data + // in "taskData.subtasks". Return the created task and it's subtasks using the include option. + const createdTask = await prisma.task.create({ + data: { + ...taskData, + status: status, + + subtasks: { + createMany: { + data: taskData.subtasks.map(subtask => ({ + ...subtask, + + completed: subtask.completed ?? false + })) + } + } + }, + include: { + subtasks: true + } + }) } catch (error) { throw new Error(`Error creating task: ${error.message}`);