Stage 1: Course/Module/Lesson CRUD admin UI with TipTap editor
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
"use server";
|
||||
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { headers } from "next/headers";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { revalidatePath } from "next/cache";
|
||||
|
||||
async function requireAdmin() {
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
if (!session || session.user.role !== "admin") throw new Error("Forbidden");
|
||||
}
|
||||
|
||||
export async function createLesson(moduleId: string, courseId: string, formData: FormData) {
|
||||
await requireAdmin();
|
||||
const title = formData.get("title") as string;
|
||||
const count = await prisma.lesson.count({ where: { moduleId } });
|
||||
const lesson = await prisma.lesson.create({ data: { moduleId, title, order: count } });
|
||||
revalidatePath(`/admin/courses/${courseId}/modules/${moduleId}`);
|
||||
return lesson.id;
|
||||
}
|
||||
|
||||
export async function updateLesson(lessonId: string, courseId: string, moduleId: string, formData: FormData) {
|
||||
await requireAdmin();
|
||||
const title = formData.get("title") as string;
|
||||
await prisma.lesson.update({ where: { id: lessonId }, data: { title } });
|
||||
revalidatePath(`/admin/courses/${courseId}/modules/${moduleId}`);
|
||||
}
|
||||
|
||||
export async function deleteLesson(lessonId: string, courseId: string, moduleId: string) {
|
||||
await requireAdmin();
|
||||
await prisma.lesson.delete({ where: { id: lessonId } });
|
||||
revalidatePath(`/admin/courses/${courseId}/modules/${moduleId}`);
|
||||
}
|
||||
|
||||
export async function reorderLessons(moduleId: string, courseId: string, orderedIds: string[]) {
|
||||
await requireAdmin();
|
||||
await Promise.all(
|
||||
orderedIds.map((id, index) =>
|
||||
prisma.lesson.update({ where: { id }, data: { order: index } })
|
||||
)
|
||||
);
|
||||
revalidatePath(`/admin/courses/${courseId}/modules/${moduleId}`);
|
||||
}
|
||||
Reference in New Issue
Block a user