41 lines
1.0 KiB
TypeScript
41 lines
1.0 KiB
TypeScript
"use server";
|
|
|
|
import { prisma } from "@/lib/prisma";
|
|
import { revalidatePath } from "next/cache";
|
|
import { ContentStatus } from "@prisma/client";
|
|
|
|
export async function createNews(data: {
|
|
title: string;
|
|
excerpt: string;
|
|
content: string;
|
|
author: string;
|
|
imageUrl: string;
|
|
tags?: string[];
|
|
}) {
|
|
try {
|
|
const slug = data.title
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9]+/g, '-')
|
|
.replace(/(^-|-$)/g, '');
|
|
|
|
const post = await prisma.blogPost.create({
|
|
data: {
|
|
title: data.title,
|
|
excerpt: data.excerpt,
|
|
content: data.content,
|
|
author: data.author,
|
|
imageUrl: data.imageUrl,
|
|
tags: data.tags || [],
|
|
status: ContentStatus.PENDING, // Always pending for user submission
|
|
slug: `${slug}-${Math.random().toString(36).substring(2, 7)}`
|
|
}
|
|
});
|
|
|
|
revalidatePath("/blog");
|
|
return { success: true, post };
|
|
} catch (error) {
|
|
console.error("Failed to create news:", error);
|
|
return { success: false, error: "Erreur lors de la création de l'actualité" };
|
|
}
|
|
}
|