From c71664e38228fcf0b4ccaedecf4be6e69415bfe1 Mon Sep 17 00:00:00 2001 From: iamnithishraja <153272518+iamnithishraja@users.noreply.github.com> Date: Mon, 13 May 2024 02:38:02 +0530 Subject: [PATCH 1/3] added questions linking feature --- .../migration.sql | 5 +++++ prisma/schema.prisma | 2 ++ src/actions/question/index.ts | 3 ++- src/actions/question/schema.ts | 1 + src/actions/question/types.ts | 2 +- src/actions/types.ts | 1 + src/app/questions/page.tsx | 19 ++++++++++++++++++- src/components/NewPostDialog.tsx | 3 ++- .../admin/ContentRendererClient.tsx | 6 ++++++ 9 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 prisma/migrations/20240512200653_content_attach_questions/migration.sql diff --git a/prisma/migrations/20240512200653_content_attach_questions/migration.sql b/prisma/migrations/20240512200653_content_attach_questions/migration.sql new file mode 100644 index 000000000..5819fd60a --- /dev/null +++ b/prisma/migrations/20240512200653_content_attach_questions/migration.sql @@ -0,0 +1,5 @@ +-- AlterTable +ALTER TABLE "Question" ADD COLUMN "contentId" INTEGER; + +-- AddForeignKey +ALTER TABLE "Question" ADD CONSTRAINT "Question_contentId_fkey" FOREIGN KEY ("contentId") REFERENCES "Content"("id") ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index f2d9ac334..e28a36d28 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -221,6 +221,8 @@ model Question { totalanswers Int @default(0) answers Answer[] votes Vote[] + contentId Int? + video Content? @relation(fields: [contentId], references: [id]) tags String[] updatedAt DateTime @updatedAt diff --git a/src/actions/question/index.ts b/src/actions/question/index.ts index cdbd34605..971494d99 100644 --- a/src/actions/question/index.ts +++ b/src/actions/question/index.ts @@ -33,7 +33,7 @@ const createQuestionHandler = async ( }; } - const { title, content, tags } = data; + const { title, content, tags, contentId } = data; // Create initial slug let slug = generateHandle(title); @@ -62,6 +62,7 @@ const createQuestionHandler = async ( title, content, tags, + contentId: contentId || undefined, authorId: session.user.id, slug, // Include the slug }, diff --git a/src/actions/question/schema.ts b/src/actions/question/schema.ts index 8e42bbd50..888581905 100644 --- a/src/actions/question/schema.ts +++ b/src/actions/question/schema.ts @@ -3,6 +3,7 @@ import { z } from 'zod'; export const QuestionInsertSchema = z.object({ title: z.string().min(5, 'Question title too short'), content: z.string().min(0, 'Question content too short'), + contentId: z.nullable(z.number()), tags: z.array(z.string()).optional(), }); diff --git a/src/actions/question/types.ts b/src/actions/question/types.ts index abaf24c8e..34d714fba 100644 --- a/src/actions/question/types.ts +++ b/src/actions/question/types.ts @@ -57,7 +57,7 @@ export interface QuestionQuery { }; where?: { authorId?: string; - + contentId?: number; title?: { contains: string; }; diff --git a/src/actions/types.ts b/src/actions/types.ts index ac252c469..d88fcff3c 100644 --- a/src/actions/types.ts +++ b/src/actions/types.ts @@ -2,6 +2,7 @@ import { CommentType } from '@prisma/client'; export interface QueryParams { limit?: number; + contentId?: number; page?: number; tabtype?: TabType; search?: string; diff --git a/src/app/questions/page.tsx b/src/app/questions/page.tsx index 152cbe30b..48bddeb13 100644 --- a/src/app/questions/page.tsx +++ b/src/app/questions/page.tsx @@ -86,6 +86,14 @@ const getQuestionsWithQuery = async ( }; } + const contentFilter = searchParams.contentId; + if (contentFilter) { + additionalQuery.where = { + ...additionalQuery.where, + contentId: Number(searchParams.contentId), + }; + } + try { const data: any = await db.question.findMany({ ...baseQuery, @@ -157,7 +165,11 @@ export default async function Home({ New Question - +
@@ -217,6 +229,11 @@ export default async function Home({
+ {response?.data?.length === 0 && ( +
+ No questions found +
+ )} {response?.data?.map((post) => ( { +export const NewPostDialog = ({ contentId }: { contentId: number | null }) => { const { theme } = useTheme(); const formRef = useRef>(null); const searchParam = useSearchParams(); @@ -86,6 +86,7 @@ export const NewPostDialog = () => { execute({ title: title?.toString() || '', content: value, + contentId, tags: (tags?.toString() || '').split(','), }); }; diff --git a/src/components/admin/ContentRendererClient.tsx b/src/components/admin/ContentRendererClient.tsx index 46344a3f7..8378c005c 100644 --- a/src/components/admin/ContentRendererClient.tsx +++ b/src/components/admin/ContentRendererClient.tsx @@ -148,6 +148,12 @@ export const ContentRendererClient = ({ Slides + + Q & A +
) : null} {!showChapters && metadata.segments?.length > 0 && ( From d87556677e14725643f380dc7425510672b933fc Mon Sep 17 00:00:00 2001 From: iamnithishraja <153272518+iamnithishraja@users.noreply.github.com> Date: Mon, 13 May 2024 02:53:54 +0530 Subject: [PATCH 2/3] fixed linting issue --- prisma/schema.prisma | 105 ++++++++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 51 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index e28a36d28..d6195424e 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -21,7 +21,7 @@ model Course { purchasedBy UserPurchases[] bookmarks Bookmark[] certificate Certificate[] - certIssued Boolean @default(false) + certIssued Boolean @default(false) } model UserPurchases { @@ -53,6 +53,7 @@ model Content { comments Comment[] commentsCount Int @default(0) bookmark Bookmark? + Question Question[] } model CourseContent { @@ -65,11 +66,11 @@ model CourseContent { } model Certificate { - id String @id @default(cuid()) - user User @relation(fields: [userId], references: [id]) - userId String - course Course @relation(fields: [courseId], references: [id]) - courseId Int + id String @id @default(cuid()) + user User @relation(fields: [userId], references: [id]) + userId String + course Course @relation(fields: [courseId], references: [id]) + courseId Int @@unique([userId, courseId]) } @@ -208,76 +209,78 @@ model Comment { votes Vote[] isPinned Boolean @default(false) } + model Question { - id Int @id @default(autoincrement()) - title String - content String - slug String @unique - createdAt DateTime @default(now()) - author User @relation(fields: [authorId], references: [id]) - authorId String - upvotes Int @default(0) - downvotes Int @default(0) + id Int @id @default(autoincrement()) + title String + content String + slug String @unique + createdAt DateTime @default(now()) + author User @relation(fields: [authorId], references: [id]) + authorId String + upvotes Int @default(0) + downvotes Int @default(0) totalanswers Int @default(0) - answers Answer[] - votes Vote[] - contentId Int? - video Content? @relation(fields: [contentId], references: [id]) - tags String[] - updatedAt DateTime @updatedAt + answers Answer[] + votes Vote[] + contentId Int? + video Content? @relation(fields: [contentId], references: [id]) + tags String[] + updatedAt DateTime @updatedAt @@index([authorId]) } model Answer { - id Int @id @default(autoincrement()) - content String - createdAt DateTime @default(now()) - question Question @relation(fields: [questionId], references: [id]) - questionId Int - author User @relation(fields: [authorId], references: [id]) - authorId String - votes Vote[] - upvotes Int @default(0) - downvotes Int @default(0) - totalanswers Int @default(0) - parentId Int? - responses Answer[] @relation("AnswerToAnswer") - parent Answer? @relation("AnswerToAnswer", fields: [parentId], references: [id]) - updatedAt DateTime @updatedAt + id Int @id @default(autoincrement()) + content String + createdAt DateTime @default(now()) + question Question @relation(fields: [questionId], references: [id]) + questionId Int + author User @relation(fields: [authorId], references: [id]) + authorId String + votes Vote[] + upvotes Int @default(0) + downvotes Int @default(0) + totalanswers Int @default(0) + parentId Int? + responses Answer[] @relation("AnswerToAnswer") + parent Answer? @relation("AnswerToAnswer", fields: [parentId], references: [id]) + updatedAt DateTime @updatedAt @@index([questionId]) @@index([authorId]) @@index([parentId]) } - model Vote { - id Int @id @default(autoincrement()) - questionId Int? - question Question? @relation(fields: [questionId], references: [id]) - answerId Int? - answer Answer? @relation(fields: [answerId], references: [id]) - commentId Int? - comment Comment? @relation(fields: [commentId], references: [id]) - userId String - user User @relation(fields: [userId], references: [id]) - voteType VoteType - createdAt DateTime @default(now()) + id Int @id @default(autoincrement()) + questionId Int? + question Question? @relation(fields: [questionId], references: [id]) + answerId Int? + answer Answer? @relation(fields: [answerId], references: [id]) + commentId Int? + comment Comment? @relation(fields: [commentId], references: [id]) + userId String + user User @relation(fields: [userId], references: [id]) + voteType VoteType + createdAt DateTime @default(now()) - @@unique([questionId, userId]) - @@unique([answerId, userId]) - @@unique([commentId, userId]) + @@unique([questionId, userId]) + @@unique([answerId, userId]) + @@unique([commentId, userId]) } enum VoteType { UPVOTE DOWNVOTE } + enum PostType { QUESTION ANSWER } + enum CommentType { INTRO DEFAULT From 78f2c79f582112462b3993f8bb8de2f0a7e24062 Mon Sep 17 00:00:00 2001 From: iamnithishraja <153272518+iamnithishraja@users.noreply.github.com> Date: Mon, 13 May 2024 03:35:24 +0530 Subject: [PATCH 3/3] Update schema.prisma --- prisma/schema.prisma | 98 ++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 53 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 846854f3b..125df7c42 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -20,7 +20,7 @@ model Course { content CourseContent[] purchasedBy UserPurchases[] certificate Certificate[] - certIssued Boolean @default(false) + certIssued Boolean @default(false) } model UserPurchases { @@ -65,17 +65,13 @@ model CourseContent { } model Certificate { - id String @id @default(cuid()) - user User @relation(fields: [userId], references: [id]) - userId String - course Course @relation(fields: [courseId], references: [id]) - courseId Int id String @id @default(cuid()) slug String @default("certId") user User @relation(fields: [userId], references: [id]) userId String course Course @relation(fields: [courseId], references: [id]) courseId Int + @@unique([userId, courseId]) } @@ -189,8 +185,6 @@ model Bookmark { user User @relation(fields: [userId], references: [id], onDelete: Cascade) content Content @relation(fields: [contentId], references: [id], onDelete: Cascade) createdAt DateTime @default(now()) - courseId Int - course Course @relation(fields: [courseId], references: [id], onDelete: Cascade) } model Comment { @@ -213,78 +207,76 @@ model Comment { votes Vote[] isPinned Boolean @default(false) } - model Question { - id Int @id @default(autoincrement()) - title String - content String - slug String @unique - createdAt DateTime @default(now()) - author User @relation(fields: [authorId], references: [id]) - authorId String - upvotes Int @default(0) - downvotes Int @default(0) + id Int @id @default(autoincrement()) + title String + content String + slug String @unique + createdAt DateTime @default(now()) + author User @relation(fields: [authorId], references: [id]) + authorId String + upvotes Int @default(0) + downvotes Int @default(0) totalanswers Int @default(0) - answers Answer[] - votes Vote[] + answers Answer[] + votes Vote[] + tags String[] contentId Int? video Content? @relation(fields: [contentId], references: [id]) - tags String[] - updatedAt DateTime @updatedAt + updatedAt DateTime @updatedAt @@index([authorId]) } model Answer { - id Int @id @default(autoincrement()) - content String - createdAt DateTime @default(now()) - question Question @relation(fields: [questionId], references: [id]) - questionId Int - author User @relation(fields: [authorId], references: [id]) - authorId String - votes Vote[] - upvotes Int @default(0) - downvotes Int @default(0) - totalanswers Int @default(0) - parentId Int? - responses Answer[] @relation("AnswerToAnswer") - parent Answer? @relation("AnswerToAnswer", fields: [parentId], references: [id]) - updatedAt DateTime @updatedAt + id Int @id @default(autoincrement()) + content String + createdAt DateTime @default(now()) + question Question @relation(fields: [questionId], references: [id]) + questionId Int + author User @relation(fields: [authorId], references: [id]) + authorId String + votes Vote[] + upvotes Int @default(0) + downvotes Int @default(0) + totalanswers Int @default(0) + parentId Int? + responses Answer[] @relation("AnswerToAnswer") + parent Answer? @relation("AnswerToAnswer", fields: [parentId], references: [id]) + updatedAt DateTime @updatedAt @@index([questionId]) @@index([authorId]) @@index([parentId]) } + model Vote { - id Int @id @default(autoincrement()) - questionId Int? - question Question? @relation(fields: [questionId], references: [id]) - answerId Int? - answer Answer? @relation(fields: [answerId], references: [id]) - commentId Int? - comment Comment? @relation(fields: [commentId], references: [id]) - userId String - user User @relation(fields: [userId], references: [id]) - voteType VoteType - createdAt DateTime @default(now()) + id Int @id @default(autoincrement()) + questionId Int? + question Question? @relation(fields: [questionId], references: [id]) + answerId Int? + answer Answer? @relation(fields: [answerId], references: [id]) + commentId Int? + comment Comment? @relation(fields: [commentId], references: [id]) + userId String + user User @relation(fields: [userId], references: [id]) + voteType VoteType + createdAt DateTime @default(now()) - @@unique([questionId, userId]) - @@unique([answerId, userId]) - @@unique([commentId, userId]) + @@unique([questionId, userId]) + @@unique([answerId, userId]) + @@unique([commentId, userId]) } enum VoteType { UPVOTE DOWNVOTE } - enum PostType { QUESTION ANSWER } - enum CommentType { INTRO DEFAULT