feat: full site build — Project/Melody schema (Option A), admin CRUD, public sections, uploads, email+SMTP, internal analytics, legal pages, docs
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import Link from "next/link";
|
||||
import { notFound } from "next/navigation";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import AdminPageHeader from "@/components/admin/admin-page-header";
|
||||
import MelodyForm from "@/components/admin/melody-form";
|
||||
import { prisma } from "@/lib/db";
|
||||
import type { MelodyInput } from "@/lib/validations/melody";
|
||||
|
||||
export default async function EditMelodyPage({ params }: { params: { id: string } }) {
|
||||
const [melody, categories] = await Promise.all([
|
||||
prisma.melody.findUnique({ where: { id: params.id } }),
|
||||
prisma.category.findMany({ where: { kind: "MELODY" }, orderBy: [{ order: "asc" }, { nameEn: "asc" }], select: { id: true, nameAr: true, nameEn: true } }),
|
||||
]);
|
||||
if (!melody) notFound();
|
||||
const defaultValues: MelodyInput = {
|
||||
slug: melody.slug, titleAr: melody.titleAr, titleEn: melody.titleEn, descAr: melody.descAr ?? "", descEn: melody.descEn ?? "",
|
||||
audioFile: melody.audioFile, coverImage: melody.coverImage ?? "", durationSec: melody.durationSec, isDownloadable: melody.isDownloadable,
|
||||
status: melody.status, isFeatured: melody.isFeatured, sortOrder: melody.sortOrder, categoryId: melody.categoryId,
|
||||
};
|
||||
return <div className="mx-auto w-full max-w-7xl"><AdminPageHeader title={`Edit ${melody.titleEn}`} description="Update the audio, artwork, category, and public visibility settings." actions={<Button asChild variant="outline"><Link href="/admin/melodies">Back to melodies</Link></Button>} /><MelodyForm mode="edit" melodyId={melody.id} categories={categories} defaultValues={defaultValues} /></div>;
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
"use server";
|
||||
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { redirect } from "next/navigation";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { melodyIdSchema, melodySchema } from "@/lib/validations/melody";
|
||||
|
||||
export type MelodyActionResult = {
|
||||
success?: true;
|
||||
error?: string;
|
||||
};
|
||||
|
||||
async function isAdminAuthenticated() {
|
||||
const session = await auth();
|
||||
return Boolean(session?.user);
|
||||
}
|
||||
|
||||
function getActionError(error: unknown) {
|
||||
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2002") {
|
||||
return "A melody with this slug already exists.";
|
||||
}
|
||||
|
||||
return "Unable to save the melody right now.";
|
||||
}
|
||||
|
||||
async function hasMelodyCategory(categoryId: string) {
|
||||
const category = await prisma.category.findFirst({
|
||||
where: { id: categoryId, kind: "MELODY" },
|
||||
select: { id: true },
|
||||
});
|
||||
return Boolean(category);
|
||||
}
|
||||
|
||||
function normalizeMelodyInput(input: unknown) {
|
||||
const parsed = melodySchema.safeParse(input);
|
||||
if (!parsed.success) {
|
||||
return { error: parsed.error.issues[0]?.message ?? "Invalid melody data." } as const;
|
||||
}
|
||||
|
||||
return {
|
||||
data: {
|
||||
...parsed.data,
|
||||
descAr: parsed.data.descAr || null,
|
||||
descEn: parsed.data.descEn || null,
|
||||
coverImage: parsed.data.coverImage || null,
|
||||
},
|
||||
} as const;
|
||||
}
|
||||
|
||||
export async function createMelody(input: unknown): Promise<MelodyActionResult> {
|
||||
if (!(await isAdminAuthenticated())) {
|
||||
return { error: "Your session has expired. Please sign in again." };
|
||||
}
|
||||
|
||||
const normalized = normalizeMelodyInput(input);
|
||||
if ("error" in normalized) return normalized;
|
||||
if (!(await hasMelodyCategory(normalized.data.categoryId))) {
|
||||
return { error: "Choose a valid melody category." };
|
||||
}
|
||||
|
||||
try {
|
||||
await prisma.melody.create({ data: normalized.data });
|
||||
revalidatePath("/admin/melodies");
|
||||
revalidatePath("/en/melodies");
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
return { error: getActionError(error) };
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateMelody(melodyId: string, input: unknown): Promise<MelodyActionResult> {
|
||||
if (!(await isAdminAuthenticated())) {
|
||||
return { error: "Your session has expired. Please sign in again." };
|
||||
}
|
||||
|
||||
const validId = melodyIdSchema.safeParse(melodyId);
|
||||
if (!validId.success) return { error: "Invalid melody id." };
|
||||
|
||||
const normalized = normalizeMelodyInput(input);
|
||||
if ("error" in normalized) return normalized;
|
||||
if (!(await hasMelodyCategory(normalized.data.categoryId))) {
|
||||
return { error: "Choose a valid melody category." };
|
||||
}
|
||||
|
||||
try {
|
||||
await prisma.melody.update({ where: { id: validId.data }, data: normalized.data });
|
||||
revalidatePath("/admin/melodies");
|
||||
revalidatePath(`/admin/melodies/${validId.data}/edit`);
|
||||
revalidatePath("/en/melodies");
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
return { error: getActionError(error) };
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteMelody(formData: FormData) {
|
||||
if (!(await isAdminAuthenticated())) redirect("/admin/login");
|
||||
|
||||
const validId = melodyIdSchema.safeParse(formData.get("melodyId"));
|
||||
if (!validId.success) redirect("/admin/melodies?error=invalid-id");
|
||||
|
||||
try {
|
||||
await prisma.melody.delete({ where: { id: validId.data } });
|
||||
} catch (error) {
|
||||
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2025") {
|
||||
redirect("/admin/melodies?error=not-found");
|
||||
}
|
||||
redirect("/admin/melodies?error=delete-failed");
|
||||
}
|
||||
|
||||
revalidatePath("/admin/melodies");
|
||||
revalidatePath("/en/melodies");
|
||||
redirect("/admin/melodies");
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import Link from "next/link";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import AdminPageHeader from "@/components/admin/admin-page-header";
|
||||
import MelodyForm from "@/components/admin/melody-form";
|
||||
import { prisma } from "@/lib/db";
|
||||
import type { MelodyInput } from "@/lib/validations/melody";
|
||||
|
||||
export default async function NewMelodyPage() {
|
||||
const categories = await prisma.category.findMany({ where: { kind: "MELODY" }, orderBy: [{ order: "asc" }, { nameEn: "asc" }], select: { id: true, nameAr: true, nameEn: true } });
|
||||
const defaultValues: MelodyInput = {
|
||||
slug: "", titleAr: "", titleEn: "", descAr: "", descEn: "", audioFile: "", coverImage: "", durationSec: null,
|
||||
isDownloadable: false, status: "DRAFT", isFeatured: false, sortOrder: 0, categoryId: "",
|
||||
};
|
||||
|
||||
return <div className="mx-auto w-full max-w-7xl"><AdminPageHeader title="New melody" description="Create an audio track with its cover, category, and publishing controls." actions={<Button asChild variant="outline"><Link href="/admin/melodies">Back to melodies</Link></Button>} /><MelodyForm mode="create" categories={categories} defaultValues={defaultValues} /></div>;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import Link from "next/link";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import AdminPageHeader from "@/components/admin/admin-page-header";
|
||||
import { deleteMelody } from "@/app/admin/(protected)/melodies/actions";
|
||||
import { prisma } from "@/lib/db";
|
||||
|
||||
type MelodiesPageProps = { searchParams: { error?: string } };
|
||||
|
||||
function getErrorMessage(error?: string) {
|
||||
if (error === "invalid-id") return "The selected melody id is invalid.";
|
||||
if (error === "not-found") return "The selected melody no longer exists.";
|
||||
if (error === "delete-failed") return "The melody could not be deleted.";
|
||||
return null;
|
||||
}
|
||||
|
||||
export default async function MelodiesPage({ searchParams }: MelodiesPageProps) {
|
||||
const melodies = await prisma.melody.findMany({
|
||||
orderBy: [{ isFeatured: "desc" }, { sortOrder: "asc" }, { updatedAt: "desc" }],
|
||||
include: { category: { select: { nameEn: true, nameAr: true } } },
|
||||
});
|
||||
const errorMessage = getErrorMessage(searchParams.error);
|
||||
|
||||
return (
|
||||
<div className="mx-auto w-full max-w-7xl">
|
||||
<AdminPageHeader title="Melodies" description="Manage audio tracks, cover art, categories, publishing, and download permissions." actions={<Button asChild><Link href="/admin/melodies/new">New melody</Link></Button>} />
|
||||
{errorMessage ? <p className="mb-6 rounded-md border border-destructive/40 bg-destructive/10 px-4 py-3 text-sm text-destructive">{errorMessage}</p> : null}
|
||||
{melodies.length === 0 ? (
|
||||
<Card><CardContent className="p-6"><p className="text-sm text-muted-foreground">No melodies yet.</p></CardContent></Card>
|
||||
) : (
|
||||
<div className="grid gap-4 md:grid-cols-2 xl:grid-cols-3">
|
||||
{melodies.map((melody) => (
|
||||
<Card key={melody.id}>
|
||||
<CardHeader className="flex-row items-start justify-between gap-4 space-y-0">
|
||||
<div><CardTitle className="text-lg">{melody.titleEn}</CardTitle><p className="mt-1 text-sm text-muted-foreground" dir="rtl">{melody.titleAr}</p></div>
|
||||
<span className="rounded-pill border border-border px-2 py-1 text-xs text-muted-foreground">{melody.status}</span>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<p className="text-xs text-muted-foreground">/{melody.slug}</p>
|
||||
<p className="mt-2 text-sm text-muted-foreground">{melody.category.nameEn} · {melody.category.nameAr}</p>
|
||||
<p className="mt-2 text-xs text-muted-foreground">{melody.durationSec ? `${melody.durationSec}s` : "No duration"} · {melody.isDownloadable ? "Download enabled" : "Streaming only"}</p>
|
||||
<div className="mt-5 flex items-center gap-2">
|
||||
<Button asChild size="sm" variant="outline"><Link href={`/admin/melodies/${melody.id}/edit`}>Edit</Link></Button>
|
||||
<form action={deleteMelody}><input type="hidden" name="melodyId" value={melody.id} /><Button type="submit" size="sm" variant="ghost">Delete</Button></form>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user