From e6803dd019f359b9cadfb12b9f730d69d2bf8ee7 Mon Sep 17 00:00:00 2001 From: naqib-axmed Date: Sun, 7 Dec 2025 09:29:48 +0300 Subject: [PATCH] week 19 assignment --- .gitignore | 7 ++++++ package.json | 5 +++- prisma/schema.prisma | 37 +++++++++++++++++------------- routes/tasks.js | 1 + services/taskServices.js | 49 ++++++++++++++++++++++++++++++++++++++-- 5 files changed, 80 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index a9048e0..32b99ea 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ coverage/ .nyc_output # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) + .grunt # Bower dependency directory (https://bower.io/) @@ -28,6 +29,7 @@ bower_components # node-waf configuration .lock-wscript + # Compiled binary addons (https://nodejs.org/api/addons.html) build/Release @@ -69,6 +71,11 @@ typings/ .env.test.local .env.production.local +12 + + + + # parcel-bundler cache (https://parceljs.org/) .cache .parcel-cache diff --git a/package.json b/package.json index 19faea1..cfe0144 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,10 @@ "scripts": { "start": "node server.js", "dev": "nodemon server.js", - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "db:generate" : "prisma generate", + "db-push": "prisma db-push", + "db-studio": "prisma studio" }, "keywords": [ "nodejs", diff --git a/prisma/schema.prisma b/prisma/schema.prisma index fb27ac1..ab76519 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -1,6 +1,3 @@ -// This is your Prisma schema file, -// learn more about it in the docs: https://pris.ly/d/prisma-schema - generator client { provider = "prisma-client-js" } @@ -11,32 +8,40 @@ datasource db { } model Task { - id String @id @default(cuid()) + id String @id @default(cuid()) title String description String - status TaskStatus - priority Priority + status TaskStatus @default(pending) + priority Priority @default(low) dueDate DateTime? assignedTo String? - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt subtasks Subtask[] @@map("tasks") } -// TODO: Create a Subtask model that has a (title, description, completed, taskId, task, createdAt, updatedAt) where taskId is a foreign key to the Task model and task is a relation to the Task model - model Subtask { - + id String @id @default(cuid()) + taskId String + title String + completed Boolean @default(false) + description String + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + task Task @relation(fields: [taskId], references: [id]) } -// 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 { - -} \ No newline at end of file + low + medium + high +} diff --git a/routes/tasks.js b/routes/tasks.js index cedda1d..f49cd27 100644 --- a/routes/tasks.js +++ b/routes/tasks.js @@ -132,3 +132,4 @@ router.delete("/tasks/:id", async (req, res) => { }); export default router; + diff --git a/services/taskServices.js b/services/taskServices.js index 73012bb..493c355 100644 --- a/services/taskServices.js +++ b/services/taskServices.js @@ -1,3 +1,4 @@ +import { Priority } from "@prisma/client"; import prisma from "../lib/prisma.js"; // Simpler approach - just get all tasks @@ -10,13 +11,26 @@ export async function getAllTasks() { // Get task by ID export async function getTaskById(id) { - try { + + try { +const task = await prisma.task.findUnique({ + where: {id}, + include: {subtasks: true} +}) // TODO: Check if task exists + if (!task && !id) { + // TODO: If not, throw an error + throw new Error `task with ${id} is not found` + + } + // TODO: If it does, return the task + + return task } catch (error) { @@ -32,7 +46,38 @@ 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 CreateTask = await prisma.task.create({ + data: { + title: taskData.title, + description: taskData.description, + status, + priority: taskData.priority, + dueDate: taskData.dueDate, + assignedTo: taskData.assignedTo, + + subtasks: { + create: taskData.subtasks.map((subtask) => ({ + title: subtask.title, + description: subtask.description, + completed: subtask.completed || false, + })) || [], + } + }, + + include: { + subtasks: true, + }, + + + + }) + + return CreateTask; + } catch (error) {