62 lines
2.2 KiB
TypeScript
62 lines
2.2 KiB
TypeScript
"use server";
|
|
import { revalidatePath } from "next/cache";
|
|
import { rawDb, uniqueSlug } from "@/lib/db/client";
|
|
import { redirect } from "next/navigation";
|
|
|
|
export async function updateActressMeta(
|
|
actressId: number,
|
|
data: {
|
|
name?: string;
|
|
altNames?: string | null;
|
|
notes?: string | null;
|
|
bornOn?: string | null;
|
|
heightCm?: number | null;
|
|
weightKg?: number | null;
|
|
cupSize?: string | null;
|
|
},
|
|
): Promise<{ slug: string } | null> {
|
|
const row = rawDb.prepare(`SELECT name, slug FROM actresses WHERE id = ?`).get(actressId) as
|
|
| { name: string; slug: string }
|
|
| undefined;
|
|
if (!row) return null;
|
|
|
|
let newSlug = row.slug;
|
|
let newName = row.name;
|
|
if (data.name != null) {
|
|
const trimmed = data.name.trim();
|
|
if (trimmed && trimmed !== row.name) {
|
|
newName = trimmed;
|
|
newSlug = uniqueSlug(rawDb, "actresses", trimmed, actressId);
|
|
}
|
|
}
|
|
const altNames = data.altNames == null ? null : data.altNames.trim() || null;
|
|
const notes = data.notes == null ? null : data.notes.trim() || null;
|
|
const bornOn = data.bornOn == null ? null : (data.bornOn.trim() || null);
|
|
const heightCm = data.heightCm == null || !Number.isFinite(data.heightCm) ? null : Math.round(data.heightCm);
|
|
const weightKg = data.weightKg == null || !Number.isFinite(data.weightKg) ? null : Math.round(data.weightKg);
|
|
const cupSize = data.cupSize == null ? null : (data.cupSize.trim() || null);
|
|
|
|
rawDb.prepare(`
|
|
UPDATE actresses
|
|
SET name = ?, slug = ?, alt_names = ?, notes = ?,
|
|
born_on = ?, height_cm = ?, weight_kg = ?, cup_size = ?
|
|
WHERE id = ?
|
|
`).run(newName, newSlug, altNames, notes, bornOn, heightCm, weightKg, cupSize, actressId);
|
|
|
|
revalidatePath("/actress");
|
|
revalidatePath(`/actress/${row.slug}`);
|
|
if (newSlug !== row.slug) revalidatePath(`/actress/${newSlug}`);
|
|
return { slug: newSlug };
|
|
}
|
|
|
|
export async function updateActressMetaAction(formData: FormData) {
|
|
const id = Number(formData.get("id"));
|
|
if (!Number.isFinite(id)) return;
|
|
const result = await updateActressMeta(id, {
|
|
name: String(formData.get("name") ?? ""),
|
|
altNames: String(formData.get("altNames") ?? ""),
|
|
notes: String(formData.get("notes") ?? ""),
|
|
});
|
|
if (result) redirect(`/actress/${result.slug}`);
|
|
}
|