89 lines
2.5 KiB
TypeScript
89 lines
2.5 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { prisma } from "@/lib/db";
|
|
import { auth } from "@/lib/auth";
|
|
import { getApiLang, pickText } from "@/lib/api-locale";
|
|
import { getApiWowVersion } from "@/lib/wow-versions";
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const { id } = await params;
|
|
const lang = getApiLang(request);
|
|
const wowVersion = getApiWowVersion(request);
|
|
|
|
const software = await prisma.software.findFirst({
|
|
where: { OR: [{ id }, { slug: id }] },
|
|
include: {
|
|
versions: {
|
|
where: { wowVersion },
|
|
orderBy: { versionCode: "desc" },
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!software) {
|
|
return NextResponse.json({ error: "Not found" }, { status: 404 });
|
|
}
|
|
|
|
return NextResponse.json({
|
|
...software,
|
|
name: pickText(software.name, software.nameEn, lang),
|
|
description: pickText(software.description, software.descriptionEn, lang),
|
|
nameZh: software.name,
|
|
descriptionZh: software.description,
|
|
nameEn: software.nameEn,
|
|
descriptionEn: software.descriptionEn,
|
|
versions: software.versions.map((v) => ({
|
|
...v,
|
|
changelog: pickText(v.changelog, v.changelogEn, lang),
|
|
changelogZh: v.changelog,
|
|
changelogEn: v.changelogEn,
|
|
})),
|
|
lang,
|
|
wowVersion,
|
|
});
|
|
}
|
|
|
|
export async function PUT(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const session = await auth();
|
|
if (!session?.user) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
const { id } = await params;
|
|
const body = await request.json();
|
|
|
|
const software = await prisma.software.update({
|
|
where: { id },
|
|
data: {
|
|
...(body.name !== undefined && { name: body.name }),
|
|
...(body.nameEn !== undefined && { nameEn: body.nameEn }),
|
|
...(body.slug !== undefined && { slug: body.slug }),
|
|
...(body.description !== undefined && { description: body.description }),
|
|
...(body.descriptionEn !== undefined && {
|
|
descriptionEn: body.descriptionEn,
|
|
}),
|
|
},
|
|
});
|
|
|
|
return NextResponse.json(software);
|
|
}
|
|
|
|
export async function DELETE(
|
|
_request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const session = await auth();
|
|
if (!session?.user) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
const { id } = await params;
|
|
await prisma.software.delete({ where: { id } });
|
|
return NextResponse.json({ success: true });
|
|
}
|