Create the API endpoint src/pages/api/tasks.ts. Here is the description: Next.js API route for project tasks. Be sure to add the proper extension when creating the file. Here are the instructions:
Summary
We need to create a new Next.js API route tasks.ts that will handle the project tasks. This API route should support the following actions:
GET: Retrieve all tasks for a specific project
POST: Create a new task for a specific project
PUT: Update an existing task for a specific project
DELETE: Delete an existing task for a specific project
Detailed Instructions
Create a new file tasks.ts inside the src/pages/api folder.
Import the necessary dependencies:
import { NextApiRequest, NextApiResponse } from "next";
import { prisma } from "~/server/db";
import { getServerAuthSession } from "~/server/auth";
import { ProjectTask, projectTaskSchema } from "~/types";
Define the main handler function for the API route:
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
// Your implementation here
}
Inside the handler function, use the getServerAuthSession function to authenticate the user:
const session = await getServerAuthSession({ req, res });
if (!session) {
return res.status(401).json({ message: "Unauthorized" });
}
const user = session.user;
Implement a switch statement to handle different HTTP methods (GET, POST, PUT, DELETE):
switch (req.method) {
case "GET":
// Handle GET requests
break;
case "POST":
// Handle POST requests
break;
case "PUT":
// Handle PUT requests
break;
case "DELETE":
// Handle DELETE requests
break;
default:
res.setHeader("Allow", ["GET", "POST", "PUT", "DELETE"]);
res.status(405).end(`Method ${req.method} Not Allowed`);
}
For GET requests, retrieve all tasks for a specific project:
Extract the projectId from the request query parameters.
Use Prisma to fetch all tasks for the given project and user.
Validate the fetched tasks using the projectTaskSchema from Zod.
Return the tasks as a JSON response.
For POST requests, create a new task for a specific project:
Extract the task data from the request body.
Validate the task data using the projectTaskSchema from Zod.
Use Prisma to create a new task for the given project and user.
Return the created task as a JSON response.
For PUT requests, update an existing task for a specific project:
Extract the task data and taskId from the request body.
Validate the task data using the projectTaskSchema from Zod.
Use Prisma to update the task with the given taskId for the given project and user.
Return the updated task as a JSON response.
For DELETE requests, delete an existing task for a specific project:
Extract the taskId from the request body.
Use Prisma to delete the task with the given taskId for the given project and user.
Return a JSON response with a message indicating the task was deleted successfully.
Handle error cases:
Catch any errors that occur during the execution of the API route.
Return a JSON response with an appropriate status code and error message.
Expected Output
The API route should return the following TypeScript types for each action:
GET: ProjectTask[]
POST: ProjectTask
PUT: ProjectTask
DELETE: { message: string }
Error Cases
If the user is not authenticated, return a 401 status code with the message "Unauthorized".
If the request method is not supported, return a 405 status code with the message "Method Not Allowed".
If any errors occur during the execution of the API route, return an appropriate status code and error message.
Create the API endpoint src/pages/api/tasks.ts. Here is the description: Next.js API route for project tasks. Be sure to add the proper extension when creating the file. Here are the instructions:
Summary
We need to create a new Next.js API route
tasks.ts
that will handle the project tasks. This API route should support the following actions:Detailed Instructions
Create a new file
tasks.ts
inside thesrc/pages/api
folder.Import the necessary dependencies:
getServerAuthSession
function to authenticate the user:projectId
from the request query parameters.projectTaskSchema
from Zod.projectTaskSchema
from Zod.taskId
from the request body.projectTaskSchema
from Zod.taskId
for the given project and user.taskId
from the request body.taskId
for the given project and user.Expected Output
The API route should return the following TypeScript types for each action:
ProjectTask[]
ProjectTask
ProjectTask
{ message: string }
Error Cases