Skip to content

SummitQuest is a full-stack web application specializes in finding national parks to enjoy and mountains to climb.

License

Notifications You must be signed in to change notification settings

icortes/enjoy-the-outdoors

Repository files navigation

Enjoy the Outdoors - Capstone 2

SummitQuest is a full-stack web application specializes in finding national parks to enjoy and mountains to climb.

Pages

Home

Home Page

National Parks

National Parks Page

Mountains

Mountains Page

Code Highlight

import prisma from '../../../../../../prisma/prisma';

export async function GET(request: Request,{ params }: { params: { parktype: string } }) {
  try {
    if (params.parktype == 'All') {
      const data = await prisma.national_park_data.findMany({});
      return Response.json(data);
    } else {
      let data = await prisma.national_park_data.findMany({
        where: {
          LocationName: {
            contains: params.parktype,
            mode: 'insensitive',
          },
        },
      });

      return Response.json(data);
    }
  } catch (error) {
    return Response.json(error);
  }
}

Step by Step:

  1. Import Statement

    import prisma from '../../../../../../prisma/prisma';

    Imports the Prisma client instance. Prisma is an Object-Relational Mapping (ORM) tool used to interact with databases.

  2. Function Declaration

    export async function GET(request: Request,{ params }: { params: { parktype: string } }) {

    Declares an dynamic asynchronous function named GET that takes an object with a params property. The { params: { parktype: string } } is specifying the data type of the parameters.

  3. Try-Catch Block

    try {

    The code inside the try block attempts to execute, and if any errors occur during the execution, they will be caught in the associated catch block.

  4. Conditional Check

    if (params.parktype == 'All') {

    Checks if the value of params.parktype is equal to the string 'All'.

  5. Database Query (1st Block)

    const data = await prisma.national_park_data.findMany({});
    return Response.json(data);

    If params.parktype is 'All', it queries all data from the national_park_data collection using Prisma and returns the data as a JSON response.

  6. Database Query (2nd Block)

    else {
      let data = await prisma.national_park_data.findMany({
        where: {
          LocationName: {
            contains: params.parktype,
            mode: 'insensitive',
          },
        },
      });
    
      return Response.json(data);
    }

    If params.parktype is not 'All', it queries data from the national_park_data collection where the LocationName contains the specified parktype. The mode: 'insensitive' ensures a case-insensitive search. The result is then returned as a JSON response.

  7. Catch Block

    catch (error) {
      return Response.json(error);
    }

    Catches any errors that occurred during the execution of the try block and returns the error as a JSON response.

Built With

Deployed Link

Author

License

This project is licensed under the MIT License

Acknowledgments

  • Prisma docs
  • Nextjs docs
  • Zustand docs
  • Stack Overflow

About

SummitQuest is a full-stack web application specializes in finding national parks to enjoy and mountains to climb.

Topics

Resources

License

Stars

Watchers

Forks