- Articles: list / new / edit / delete (websiteArticles), unique-slug generation via slugify() (pure, unit-tested); staff-gated server actions. - Bans: list active bans (ban_expire > now), create ban (writes the bans row + RCON disconnect) with type/duration/reason, lift ban; staff-gated actions. - Admin nav extended (Articles, Bans). Verified: tsc exit 0, vitest 48/48, next build exit 0 (7 /admin routes).
65 lines
2.0 KiB
TypeScript
65 lines
2.0 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { redirect } from "next/navigation";
|
|
import { slugify } from "@/lib/format";
|
|
import { requireStaff } from "@/lib/admin/guard";
|
|
import { prisma } from "@/lib/prisma";
|
|
|
|
async function uniqueSlug(title: string): Promise<string> {
|
|
const base = slugify(title);
|
|
let slug = base;
|
|
let n = 2;
|
|
while (await prisma.websiteArticles.findUnique({ where: { slug } })) {
|
|
slug = `${base}-${n++}`;
|
|
}
|
|
return slug;
|
|
}
|
|
|
|
export async function createArticle(formData: FormData): Promise<void> {
|
|
const staff = await requireStaff();
|
|
const title = String(formData.get("title") ?? "").trim();
|
|
const shortStory = String(formData.get("shortStory") ?? "").trim();
|
|
const fullStory = String(formData.get("fullStory") ?? "").trim();
|
|
const image = String(formData.get("image") ?? "").trim();
|
|
if (!title) return;
|
|
|
|
const now = new Date();
|
|
await prisma.websiteArticles.create({
|
|
data: {
|
|
slug: await uniqueSlug(title),
|
|
title: title.slice(0, 255),
|
|
shortStory: shortStory.slice(0, 255),
|
|
fullStory,
|
|
image: image.slice(0, 255),
|
|
userId: String(staff.id),
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
},
|
|
});
|
|
redirect("/admin/articles");
|
|
}
|
|
|
|
export async function updateArticle(formData: FormData): Promise<void> {
|
|
await requireStaff();
|
|
const id = BigInt(String(formData.get("id")));
|
|
await prisma.websiteArticles.update({
|
|
where: { id },
|
|
data: {
|
|
title: String(formData.get("title") ?? "").trim().slice(0, 255),
|
|
shortStory: String(formData.get("shortStory") ?? "").trim().slice(0, 255),
|
|
fullStory: String(formData.get("fullStory") ?? "").trim(),
|
|
image: String(formData.get("image") ?? "").trim().slice(0, 255),
|
|
updatedAt: new Date(),
|
|
},
|
|
});
|
|
revalidatePath(`/admin/articles/${id}`);
|
|
redirect("/admin/articles");
|
|
}
|
|
|
|
export async function deleteArticle(formData: FormData): Promise<void> {
|
|
await requireStaff();
|
|
await prisma.websiteArticles.delete({ where: { id: BigInt(String(formData.get("id"))) } });
|
|
redirect("/admin/articles");
|
|
}
|