Skip to content

Instantly share code, notes, and snippets.

@AdventureBear
Last active February 3, 2024 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdventureBear/faefac266c76291746b675da521b66c8 to your computer and use it in GitHub Desktop.
Save AdventureBear/faefac266c76291746b675da521b66c8 to your computer and use it in GitHub Desktop.
//This gist shows all the API endpoints for a single model.
//The model in this example is called "round".
//There are two files here, one is for the routes for "all" rounds
//(GET and POST) and the other file is for individual rounds that have a defined id. (GET PUT PATCH DELETE)
//NEW FILE
//api/round/route.ts
import {NextRequest, NextResponse} from "next/server";
import prisma from "@/prisma/client"
import {roundSchema} from "@/app/validations";
export async function GET(request: NextRequest) {
//fetch rounds from database
const rounds = await prisma.round.findMany()
return NextResponse.json(rounds)
}
export async function POST(request: NextRequest) {
const body = await request.json()
const validation = roundSchema.safeParse(body)
// validate data, if invalid return 400
if (!validation.success)
return NextResponse.json(validation.error.format(), {status: 400})
const {setId, distance, quantity, repeat, order, comment} = body
const newRound = await prisma.round.create({
data: {
setId,
quantity,
distance,
repeat,
order,
comment
}
})
//otherwise return body with 201 status
return NextResponse.json(newRound, {status: 201})
}
// NEW FILE
// api/round/[id]/route.ts
import {NextRequest, NextResponse} from "next/server";
import {patchRoundSchema, roundSchema} from "@/app/validations";
import prisma from "@/prisma/client";
export async function GET(
request: NextRequest,
{params}: { params: { id: string } }) {
//Fetch from DB
const round = await prisma.round.findUnique({
where: {id: parseInt(params.id)}
})
//If not found return 404
//Else return actual data
if (!round)
return NextResponse.json({error: "round not found"}, {status: 404})
return NextResponse.json(round)
}
export async function PATCH(
request: NextRequest,
{params}: { params: { id: string } }) {
//get body
const body = await request.json()
console.log(body)
const validation = patchRoundSchema.safeParse(body)
if (!validation.success)
return NextResponse.json(validation.error.format(), {status: 400})
const round = await prisma.round.findUnique({
where: {id: parseInt(params.id)}
})
console.log("API retrieved round: ", round)
if (!round)
return NextResponse.json({error: "Invalid round"}, {status: 404})
const {quantity, distance, repeat, order, comment} = body
const updatedRound = await prisma.round.update({
where: {id: round.id},
data: {
quantity,
distance,
repeat,
order,
comment
}
})
console.log(updatedRound)
return NextResponse.json(updatedRound)
}
export async function PUT(
request: NextRequest,
{params}: { params: { id: string } }) {
// Validate request body
const body = await request.json()
const validation = roundSchema.safeParse(body)
// if invalid return 400
if (!validation.success)
return NextResponse.json(validation.error.errors, {status: 400})
// otherwise fetch the item with given id
const round = await prisma.round.findUnique({
where: {
id: parseInt(params.id)
}
})
// if it doesn't exist return 404
if (!round)
return NextResponse.json({error: "Round not found"}, {status: 404})
//get set id from existing round to prevent moving rounds
const setId = round.setId
const { quantity, distance, repeat, order, comment} = body
const updatedRound = await prisma.round.update({
where: {id: round.id},
data: {
setId,
quantity,
distance,
repeat,
order,
comment
}
})
// return updated user
return NextResponse.json(updatedRound)
}
export async function DELETE(
request: NextRequest,
{params}: { params: { id: string } }
) {
//Fetch round from db
const round = await prisma.round.findUnique({
where: {id: parseInt(params.id)}
})
//If not found, return 404 not found error
if (!round)
return NextResponse.json(
{error: "Round not found"},
{status: 404})
//Delete the user
await prisma.round.delete({
where: {id: round.id}
})
//return 200
return NextResponse.json({})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment