113 lines
2.2 KiB
TypeScript
113 lines
2.2 KiB
TypeScript
// make api route to get all sellables
|
|
import { NextResponse } from "next/server";
|
|
import { db } from "~/server/db";
|
|
|
|
export async function GET() {
|
|
const sellables = await db.sellable.findMany({
|
|
include: {
|
|
shop: { select: { label: true } },
|
|
item: { select: { stock: true } },
|
|
},
|
|
});
|
|
// and cache no store header
|
|
|
|
return NextResponse.json(sellables, {
|
|
headers: { "Cache-Control": "no-store" },
|
|
});
|
|
}
|
|
|
|
type Sellable = {
|
|
shopId: number;
|
|
itemId: string;
|
|
itemName: string;
|
|
price: number;
|
|
amount: number;
|
|
};
|
|
|
|
export async function POST(request: Request) {
|
|
const { shopId, itemId, price, amount } = (await request.json()) as Sellable;
|
|
|
|
const sellable = await db.sellable.create({
|
|
data: {
|
|
shopId,
|
|
item_name: itemId,
|
|
price,
|
|
amount,
|
|
},
|
|
});
|
|
|
|
return NextResponse.json(sellable, {
|
|
headers: { "Cache-Control": "no-store" },
|
|
});
|
|
}
|
|
|
|
export async function PATCH(request: Request) {
|
|
const { shopId, itemId, price, amount } = (await request.json()) as Sellable;
|
|
|
|
if (!shopId || !itemId) {
|
|
return NextResponse.json({ error: "Invalid payload" }, { status: 400 });
|
|
}
|
|
const item = await db.sellable.findFirst({
|
|
where: {
|
|
shopId,
|
|
item_name: itemId,
|
|
},
|
|
select: {
|
|
id: true,
|
|
},
|
|
});
|
|
|
|
if (!item) {
|
|
return NextResponse.json({ error: "Item not found" }, { status: 404 });
|
|
}
|
|
|
|
const updated = await db.sellable.update({
|
|
where: {
|
|
id: item.id,
|
|
},
|
|
data: {
|
|
price,
|
|
amount,
|
|
},
|
|
});
|
|
|
|
return NextResponse.json(updated, {
|
|
headers: { "Cache-Control": "no-store" },
|
|
});
|
|
}
|
|
|
|
export async function DELETE(request: Request) {
|
|
const { shopId, itemId } = (await request.json()) as Sellable;
|
|
|
|
if (!shopId || !itemId) {
|
|
return NextResponse.json({ error: "Invalid payload" }, { status: 400 });
|
|
}
|
|
|
|
const item = await db.sellable.findFirst({
|
|
where: {
|
|
shopId,
|
|
item_name: itemId,
|
|
},
|
|
select: {
|
|
id: true,
|
|
},
|
|
});
|
|
|
|
if (!item) {
|
|
return NextResponse.json({ error: "Item not found" }, { status: 404 });
|
|
}
|
|
|
|
await db.sellable.delete({
|
|
where: {
|
|
id: item.id,
|
|
},
|
|
});
|
|
|
|
return NextResponse.json(
|
|
{ success: true },
|
|
{
|
|
headers: { "Cache-Control": "no-store" },
|
|
},
|
|
);
|
|
}
|