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/sitemap.ts #715

Closed otto-jacob closed 1 year ago

otto-jacob commented 1 year ago

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

Overview

The purpose of this issue is to create a Next.js API endpoint sitemap.ts that will handle the CRUD operations for the project sitemap. This endpoint will be responsible for creating, reading, updating, and deleting sitemap items in the database. The endpoint should support the following actions: GET, POST, PUT, and DELETE.

Detailed Instructions

  1. Create a new file named sitemap.ts inside the src/pages/api directory.

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

    import { NextApiRequest, NextApiResponse } from "next";
    import { z } from "zod";
    import { prisma } from "~/server/db";
    import { getServerAuthSession } from "~/server/auth";
    import { ProjectSitemap, projectSitemapSchema } from "~/types";
  3. Define the main handler function for the API endpoint with the following signature:

    export default async function handler(req: NextApiRequest, res: NextApiResponse) {
     // Implementation goes here
    }
  4. Inside the handler function, call the getServerAuthSession function to get the user's session. If the user is not authenticated, return a 401 Unauthorized error:

    const session = await getServerAuthSession({ req, res });
    if (!session) {
     res.status(401).json({ message: "Unauthorized" });
     return;
    }
  5. Create a switch statement to handle different HTTP methods (GET, POST, PUT, DELETE):

    switch (req.method) {
     case "GET":
       // Handle GET request
       break;
     case "POST":
       // Handle POST request
       break;
     case "PUT":
       // Handle PUT request
       break;
     case "DELETE":
       // Handle DELETE request
       break;
     default:
       res.setHeader("Allow", ["GET", "POST", "PUT", "DELETE"]);
       res.status(405).end(`Method ${req.method} Not Allowed`);
    }
  6. For the GET case, query the database using Prisma to fetch all sitemap items for the current user's projects. Validate the fetched data using Zod and return it as a JSON response:

  7. For the POST case, validate the request body using Zod, create a new sitemap item in the database using Prisma, and return the created item as a JSON response:

  8. For the PUT case, validate the request body using Zod, update the specified sitemap item in the database using Prisma, and return the updated item as a JSON response:

  9. For the DELETE case, validate the request body using Zod, delete the specified sitemap item from the database using Prisma, and return a success message as a JSON response:

Error Handling

For each case, make sure to handle errors appropriately. Wrap the database operations in try-catch blocks and return a 500 Internal Server Error with a descriptive error message in case of any exceptions.

Testing

After implementing the API endpoint, test it using a tool like Postman or by creating a simple UI to interact with the endpoint. Make sure to test all supported actions (GET, POST, PUT, DELETE) and verify that the endpoint behaves as expected.