Next Js Prisma API References



 import { NextResponse, NextRequest } from "next/server";

import prisma from "@/lib/client";
//POST API
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const {
name,
product_value,
total_supply,
chain,
status,
marketCap,
category,
image_url,
description,
launch_date,
} = body;

const errors = [];

if (!name) errors.push("Name field is required");
if (!product_value) errors.push("product Value field is required");
if (!total_supply) errors.push("Total Supply is required");
if (!chain) errors.push("Chain field is required");
if (!status) errors.push("Status field is required");
if (!marketCap) errors.push("Market Cap field is required");
if (!category) errors.push("Category field is required");
if (!image_url) errors.push("Image is required");
if (!description) errors.push("Description field is required");
if (!launch_date) errors.push("Launch Date field is required");

if (errors.length > 0) {
return NextResponse.json({ errors }, { status: 400 });
}

const product = await prisma.product.create({ data: body });
return NextResponse.json({
success: true,
message: "product created successfully",
data: product,
});
} catch (error) {
console.log("[product_ERROR]", error);
return NextResponse.json(
{ error: "Internal Server Error" },
{ status: 500 }
);
}
}


import { NextRequest, NextResponse } from "next/server";
import prisma from "@/lib/client";

// Delete API
export async function DELETE(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const airdrop = await prisma.airdrop.findUnique({
where: { id: params.id },
});
if (!airdrop) {
return NextResponse.json({ error: "Airdrop not found" }, { status: 404 });
}
await prisma.airdrop.delete({
where: { id: params.id },
});
return NextResponse.json(
{ message: "Airdrop deleted successfully" },
{ status: 200 }
);
} catch (error) {
console.log("[DELETE_ERROR]", error);
return NextResponse.json(
{ error: "Internal Server Error" },
{ status: 500 }
);
}
}
// PATCH/UPDATE API
export async function PATCH(
req: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const body = await req.json();
const {
name,
airdrop_value,
total_supply,
chain,
status,
marketCap,
category,
image_url,
description,
launch_date,
} = body;

const airdrop = await prisma.airdrop.findUnique({
where: { id: params.id },
});

if (!airdrop) {
return NextResponse.json({ error: "Airdrop not found" }, { status: 404 });
}

await prisma.airdrop.updateMany({
where: {
id: params.id,
},
data: {
name,
airdrop_value,
total_supply,
chain,
status,
marketCap,
category,
image_url,
description,
launch_date,
},
});

return NextResponse.json({ message: "update success" }, { status: 200 });
} catch (error) {
console.log("[UPDATE_ERROR", error);
return new NextResponse("Internal Server Error", { status: 500 });
}
}