PioneerSquareLabs / otto-playground

A playground where Otto can run free while hopefully not accidentally reformatting your hard drive
https://otto-playground.vercel.app
13 stars 0 forks source link

Create API endpoint => src/pages/api/tasks.ts #717

Closed otto-jacob closed 1 year ago

otto-jacob commented 1 year ago

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

  1. Create a new file tasks.ts inside the src/pages/api folder.

  2. Import the necessary dependencies:

import { NextApiRequest, NextApiResponse } from "next";
import { prisma } from "~/server/db";
import { getServerAuthSession } from "~/server/auth";
import { ProjectTask, projectTaskSchema } from "~/types";
  1. Define the main handler function for the API route:
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  // Your implementation here
}
  1. 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;
  1. 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`);
}
  1. For GET requests, retrieve all tasks for a specific project:
  1. For POST requests, create a new task for a specific project:
  1. For PUT requests, update an existing task for a specific project:
  1. For DELETE requests, delete an existing task for a specific project:
  1. Handle error cases:

Expected Output

The API route should return the following TypeScript types for each action:

Error Cases