MCPcopy Create free account
hub / github.com/code100x/cms / createQuestionHandler

Function createQuestionHandler

src/actions/question/index.ts:24–78  ·  view source on GitHub ↗
(
  data: InputTypeCreate,
)

Source from the content-addressed store, hash-verified

22import { ROLES } from '../types';
23
24const createQuestionHandler = async (
25 data: InputTypeCreate,
26): Promise<ReturnTypeCreate> => {
27 const session = await getServerSession(authOptions);
28
29 if (!session || !session.user.id) {
30 return {
31 error: 'Unauthorized',
32 };
33 }
34
35 const { title, content, tags } = data;
36
37 // Create initial slug
38 let slug = generateHandle(title);
39
40 try {
41 const userExists = await db.user.findUnique({
42 where: { id: session.user.id },
43 });
44
45 if (!userExists) {
46 return {
47 error: 'User not found.',
48 };
49 }
50 // Check if slug already exists
51 const existingQuestion = await db.question.findFirst({
52 where: { slug },
53 });
54
55 if (existingQuestion) {
56 slug += `-${Math.random().toString(36).substring(2, 5)}`;
57 }
58
59 const question = await db.question.create({
60 data: {
61 title,
62 content,
63 tags,
64 authorId: session.user.id,
65 slug, // Include the slug
66 },
67 });
68 revalidatePath(`/question/${question.id}`);
69 revalidatePath(`/question`);
70
71 return { data: question };
72 } catch (error) {
73 console.error(error);
74 return {
75 error: 'Failed to create question.',
76 };
77 }
78};
79
80const updateQuestionHandler = async (
81 data: InputTypeUpadate,

Callers

nothing calls this directly

Calls 1

generateHandleFunction · 0.90

Tested by

no test coverage detected