Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3,296 changes: 2,735 additions & 561 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"dependencies": {
"@hookform/resolvers": "^3.9.0",
"@lucia-auth/adapter-prisma": "^4.0.1",
"@node-rs/argon2-linux-arm64-musl": "^1.8.3",
"@node-rs/argon2": "^1.8.3",
"@prisma/client": "^5.18.0",
"@radix-ui/react-alert-dialog": "^1.1.1",
"@radix-ui/react-aspect-ratio": "^1.1.0",
Expand Down
15 changes: 15 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ model User {
passwordReset PasswordResetToken?
examSubmissions ExamSubmission[]
ExamProgress ExamProgress[]
Comments Comment[]

@@map("users")
}
Expand Down Expand Up @@ -100,6 +101,7 @@ model Exam {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
ExamProgress ExamProgress[]
Comments Comment[]

@@map("exams")
}
Expand Down Expand Up @@ -153,3 +155,16 @@ model ExamProgress {

@@unique([examId, userId])
}

model Comment{
id String @id @default(cuid())
userId String
content String
postedAt DateTime @default(now())
examId String
parentId String?
parent Comment? @relation("ParentComment", fields: [parentId], references: [id])
children Comment[] @relation("ParentComment")
exam Exam @relation(fields: [examId], references: [id],onDelete: Cascade)
user User @relation(fields: [userId], references: [id],onDelete: Cascade)
}
97 changes: 97 additions & 0 deletions src/actions/exams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export async function submitExam({
}

export async function getExamData(examId: string) {
const session = await validateRequest()

try {
const exam = await db.exam.findUnique({
where: { id: examId },
Expand Down Expand Up @@ -115,6 +117,11 @@ export async function getExamData(examId: string) {
text: q.text,
options: q.options,
})),
me: {
imageUrl: session.user?.imageUrl,
username: session.user?.username,
email: session.user?.email,
},
}
} catch (error) {
console.error('Error fetching exam data:', error)
Expand Down Expand Up @@ -149,6 +156,96 @@ export async function getExamResults(examId: string) {
}
}

export const getExamComments = cache(
async (examId: string, page: number = 1, pageSize: number = 10) => {
const session = await validateRequest()

if (!session || !session.user) {
throw new Error('Unauthorized')
}
const skip = (page - 1) * pageSize
try {
const [comments, totalCount] = await Promise.all([
db.comment.findMany({
where: { examId: examId, parentId: null },
include: {
user: true,
children: {
select: {
user: true,
content: true,
postedAt: true,
},
},
},
orderBy: { postedAt: 'desc' },
skip: skip,
take: pageSize,
}),
db.comment.count({ where: { examId: examId, parentId: null } }),
])
const totalPages = Math.ceil(totalCount / pageSize)

return {
comments,
totalPages,
currentPage: page,
}
} catch (error) {
console.error('Error fetching exam data:', error)
throw new Error('Failed to fetch exam data')
}
}
)

export const addExamComment = async (examId: string, content: string) => {
const session = await validateRequest()

if (!session || !session.user) {
throw new Error('Unauthorized')
}
try {
const ff = await db.comment.create({
data: { content, examId, userId: session.user.id },
})
return {
message: 'succesfully added:)',
imageUrl: session.user.imageUrl,
postedAt: ff.postedAt,
username: session.user.username,
email: session.user.email,
}
} catch (error) {
console.error('Error while adding comment:', error)
throw new Error('Failed to add exam comment')
}
}

export const addRepylToCommet = async (
examId: string,
commentId: string,
content: string
) => {
const session = await validateRequest()

if (!session || !session.user) {
throw new Error('Unauthorized')
}
try {
await db.comment.create({
data: {
content,
examId,
userId: session.user.id,
parentId: commentId,
},
})
} catch (error) {
console.error('Error while adding comment:', error)
throw new Error('Failed to add exam comment')
}
}

export const getUserResults = cache(
async (page: number = 1, pageSize: number = 10) => {
const session = await validateRequest()
Expand Down
14 changes: 14 additions & 0 deletions src/app/(main)/(non-exam-section)/exam-detail/[examId]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react'
import Examdetail from '../../../../../components/exams/exam-detail'
import { getExamData } from '@/actions/exams'

interface ExamDetailPageProps {
params: { examId: string }
}
const page = async ({ params }: ExamDetailPageProps) => {
const result = await getExamData(params.examId)
// console.log(result)
return <Examdetail exam={result} examId={params.examId} />
}

export default page
2 changes: 1 addition & 1 deletion src/app/(main)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default async function Layout({
return (
<SessionProvider value={session}>
<div className='flex min-h-screen flex-col'>
<div className='mx-auto mt-12 p-5'>{children}</div>
<div className='mx-auto mt-12 p-5 f'>{children}</div>
</div>
</SessionProvider>
)
Expand Down
2 changes: 1 addition & 1 deletion src/components/exams/avaiable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default function AvailableExams({ exams }: { exams: Exam[] }) {
</CardContent>
<CardFooter>
<Button asChild className='w-full'>
<Link href={`/take/${exam.id}`}>Take Test</Link>
<Link href={`/exam-detail/${exam.id}`}>Take Test</Link>
</Button>
</CardFooter>
</Card>
Expand Down
Loading