functional account and item management
This commit is contained in:
@@ -1,19 +1,13 @@
|
||||
// make api route to get all sellables
|
||||
import { NextResponse } from "next/server";
|
||||
import { auth } from "~/server/auth";
|
||||
import { db } from "~/server/db";
|
||||
|
||||
export async function GET() {
|
||||
const session = await auth();
|
||||
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const sellables = await db.sellable.findMany({
|
||||
include: {
|
||||
shop: { select: { label: true } },
|
||||
item: { select: { stock: true } },
|
||||
const sellables = await db.item.findMany({
|
||||
select: {
|
||||
shop: { select: { label: true, id: true } },
|
||||
item_name: true,
|
||||
stock: true,
|
||||
},
|
||||
});
|
||||
// and cache no store header
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
// 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" },
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user