Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added questions linking feature to content #583

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
99 changes: 54 additions & 45 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ model Course {
content CourseContent[]
purchasedBy UserPurchases[]
certificate Certificate[]
certIssued Boolean @default(false)
certIssued Boolean @default(false)
}

model UserPurchases {
Expand Down Expand Up @@ -52,6 +52,7 @@ model Content {
comments Comment[]
commentsCount Int @default(0)
bookmark Bookmark?
Question Question[]
}

model CourseContent {
Expand All @@ -64,13 +65,17 @@ model CourseContent {
}

model Certificate {
id String @id @default(cuid())
iamnithishraja marked this conversation as resolved.
Show resolved Hide resolved
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])
}

Expand Down Expand Up @@ -208,74 +213,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[]
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
Expand Down
3 changes: 2 additions & 1 deletion src/actions/question/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -62,6 +62,7 @@ const createQuestionHandler = async (
title,
content,
tags,
contentId: contentId || undefined,
authorId: session.user.id,
slug, // Include the slug
},
Expand Down
1 change: 1 addition & 0 deletions src/actions/question/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});

Expand Down
2 changes: 1 addition & 1 deletion src/actions/question/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export interface QuestionQuery {
};
where?: {
authorId?: string;

contentId?: number;
title?: {
contains: string;
};
Expand Down
1 change: 1 addition & 0 deletions src/actions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CommentType } from '@prisma/client';

export interface QueryParams {
limit?: number;
contentId?: number;
page?: number;
tabtype?: TabType;
search?: string;
Expand Down
19 changes: 18 additions & 1 deletion src/app/questions/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -157,7 +165,11 @@ export default async function Home({
New Question
</Link>
</div>
<NewPostDialog />
<NewPostDialog
contentId={
searchParams.contentId ? Number(searchParams.contentId) : null
}
/>
<div className="md:mx-[15%] mx-auto md:p-10 ">
<div className="flex flex-col items-center p-4 dark:text-white">
<div className="flex ">
Expand Down Expand Up @@ -217,6 +229,11 @@ export default async function Home({
</div>
<div className="w-full m-auto">
<div className="space-y-4 w-full">
{response?.data?.length === 0 && (
<div className="text-center text-3xl m-10">
No questions found
</div>
)}
{response?.data?.map((post) => (
<PostCard
post={post}
Expand Down
3 changes: 2 additions & 1 deletion src/components/NewPostDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { getUpdatedUrl, searchParamsToObject } from '@/lib/utils';
import { FormPostInput } from './posts/form/form-input';
import { FormPostErrors } from './posts/form/form-errors';

export const NewPostDialog = () => {
export const NewPostDialog = ({ contentId }: { contentId: number | null }) => {
const { theme } = useTheme();
const formRef = useRef<ElementRef<'form'>>(null);
const searchParam = useSearchParams();
Expand Down Expand Up @@ -86,6 +86,7 @@ export const NewPostDialog = () => {
execute({
title: title?.toString() || '',
content: value,
contentId,
tags: (tags?.toString() || '').split(','),
});
};
Expand Down
6 changes: 6 additions & 0 deletions src/components/admin/ContentRendererClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ export const ContentRendererClient = ({
Slides
</button>
</a>
<a
className="bg-blue-500 hover:bg-blue-700 text-white font-bold rounded p-2"
href={`/questions?contentId=${content.id}`}
>
Q & A
</a>
</div>
) : null}
{!showChapters && metadata.segments?.length > 0 && (
Expand Down
Loading