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 src/pages/api/data.ts #745

Closed otto-jacob closed 1 year ago

otto-jacob commented 1 year ago

Summary:

Create the API endpoint src/pages/api/data.ts. Here is the description: Next.js API route for project data. Be sure to add the proper extension when creating the file. Here are the instructions:

Background

We need to create a new Next.js API route for handling project data. This API endpoint will be responsible for fetching, creating, updating, and deleting project data schema and data columns associated with a specific project. The endpoint will be a Next.js API endpoint written in TypeScript using ES6 syntax like arrow functions.

Task Description

Create a new Next.js API route data.ts in the src/pages/api directory. This API route will handle the following actions:

  1. GET: Fetch project data schema and associated data columns for a specific project.
  2. POST: Create a new project data schema and associated data columns.
  3. PUT: Update an existing project data schema and associated data columns.
  4. DELETE: Delete a project data schema and associated data columns.

Step-by-step Instructions

  1. Create a new file data.ts in the src/pages/api directory.

  2. Import the required dependencies at the top of the file:

    import { NextApiRequest, NextApiResponse } from "next";
    import { prisma } from "~/server/db";
    import { getServerAuthSession } from "~/server/auth";
    import {
     ProjectDataSchema,
     ProjectDataColumn,
     projectDataSchemaSchema,
     projectDataColumnSchema,
    } from "~/types";
    import { z } from "zod";
  3. Define a type for the API response data:

    type ApiResponseData = {
     projectDataSchema: ProjectDataSchema | null;
     projectDataColumns: ProjectDataColumn[] | null;
    };
  4. Create an async function handler with the following signature:

    const handler = async (
     req: NextApiRequest,
     res: NextApiResponse<ApiResponseData | { message: string }>
    ) => { /* ... */ };
  5. Inside the handler function, call getServerAuthSession({ req, res }) to get the user's session. If there is no session, return a 401 Unauthorized response:

    const session = await getServerAuthSession({ req, res });
    if (!session) {
     res.status(401).json({ message: "Unauthorized" });
     return;
    }
  6. Handle each action (GET, POST, PUT, DELETE) based on the req.method:

    switch (req.method) {
     case "GET":
       // Fetch project data schema and associated data columns
       break;
     case "POST":
       // Create a new project data schema and associated data columns
       break;
     case "PUT":
       // Update an existing project data schema and associated data columns
       break;
     case "DELETE":
       // Delete a project data schema and associated data columns
       break;
     default:
       res.setHeader("Allow", ["GET", "POST", "PUT", "DELETE"]);
       res.status(405).json({ message: `Method ${req.method} Not Allowed` });
    }
  7. Implement the "GET" action:

    • Fetch the project data schema and associated data columns from the database using Prisma.
    • Validate the fetched data using Zod.
    • Return the fetched data in the API response.
  8. Implement the "POST" action:

    • Parse and validate the request body using Zod.
    • Create a new project data schema and associated data columns in the database using Prisma.
    • Validate the created data using Zod.
    • Return the created data in the API response.
  9. Implement the "PUT" action:

    • Parse and validate the request body using Zod.
    • Update the existing project data schema and associated data columns in the database using Prisma.
    • Validate the updated data using Zod.
    • Return the updated data in the API response.
  10. Implement the "DELETE" action:

    • Parse and validate the request body using Zod.
    • Delete the project data schema and associated data columns from the database using Prisma.
    • Return a success message in the API response.
  11. Export the handler function as the default export:

    export default handler;

Error Handling

Imports to Avoid

Acceptance Criteria

otto-jacob commented 1 year ago

Hello human! 👋

This PR was created by Otto to address the issue Create src/pages/api/data.ts

Next Steps

  1. Please review the PR carefully. Auto-generated code can and will contain subtle bugs and mistakes.

  2. If you identify code that needs to be changed, please reject the PR with a specific reason. Be as detailed as possible in your comments. Otto will take these comments, make changes to the code and push up changes. Please note that this process will take a few minutes.

  3. Once the code looks good, approve the PR and merge the code.

vercel[bot] commented 1 year ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
otto-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 6, 2023 8:52pm