SummitQuest is a full-stack web application specializes in finding national parks to enjoy and mountains to climb.
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);
}
}
-
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.
-
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. -
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.
-
Conditional Check
if (params.parktype == 'All') {
Checks if the value of
params.parktype
is equal to the string 'All'. -
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 thenational_park_data
collection using Prisma and returns the data as a JSON response. -
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 thenational_park_data
collection where theLocationName
contains the specifiedparktype
. Themode: 'insensitive'
ensures a case-insensitive search. The result is then returned as a JSON response. -
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.
- Isaac Cortes Hernandez
This project is licensed under the MIT License
- Prisma docs
- Nextjs docs
- Zustand docs
- Stack Overflow