Skip to content

Commit

Permalink
search functionality and ui done
Browse files Browse the repository at this point in the history
  • Loading branch information
subinsk committed Jan 14, 2023
1 parent 4c4f1f5 commit d54c8d7
Show file tree
Hide file tree
Showing 9 changed files with 371 additions and 132 deletions.
46 changes: 30 additions & 16 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ datasource db {
}

model user {
user_id String @id @default(uuid())
email String @unique
name String
notes note[]
courses course[]
subjects subject[]
pyqs pyq[]
instructors instructor[]
note_upvotes note_upvote[]
course_reviews course_review[]
pyq_upvotes pyq_upvote[]
user_id String @id @default(uuid())
email String @unique
name String
notes note[]
courses course[]
subjects subject[]
pyqs pyq[]
instructors instructor[]
note_upvotes note_upvote[]
course_reviews course_review[]
pyq_upvotes pyq_upvote[]
course_review_upvotes course_review_upvote[]
}

model subject {
Expand Down Expand Up @@ -78,28 +79,41 @@ model course {
title String @db.VarChar(255)
instructor instructor @relation(fields: [instructor_id], references: [id], onDelete: Restrict, onUpdate: Cascade)
instructor_id Int
code String @unique @db.VarChar(100)
code String @db.VarChar(100)
anonymous Boolean
created_by user @relation(fields: [created_by_id], references: [user_id], onDelete: Restrict)
created_by_id String
created_at DateTime @default(now())
updated_at DateTime @updatedAt
reviews course_review[]
@@unique([instructor_id, code])
}

model course_review {
id Int @id @unique @default(autoincrement())
user user @relation(fields: [user_id], references: [user_id], onDelete: Cascade, onUpdate: Cascade)
id Int @id @unique @default(autoincrement())
user user @relation(fields: [user_id], references: [user_id], onDelete: Cascade, onUpdate: Cascade)
user_id String
course course @relation(fields: [course_id], references: [id], onDelete: Cascade, onUpdate: Cascade)
course course @relation(fields: [course_id], references: [id], onDelete: Cascade, onUpdate: Cascade)
course_id Int
rating Float @default(0.0)
rating Float @default(0.0)
comment String
anonymous Boolean
upvotes course_review_upvote[]
@@unique([user_id, course_id])
}

model course_review_upvote {
id Int @id @unique @default(autoincrement())
user user @relation(fields: [user_id], references: [user_id], onDelete: Cascade, onUpdate: Cascade)
user_id String
course_review course_review @relation(fields: [course_review_id], references: [id], onDelete: Cascade, onUpdate: Cascade)
course_review_id Int
@@unique([user_id, course_review_id])
}

model pyq {
id Int @id @unique @default(autoincrement())
title String @db.VarChar(255)
Expand Down
2 changes: 1 addition & 1 deletion src/components/Courses/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const Modal: FC<{
<div className="flex flex-col p-10 space-y-2">
{/* Title */}
<Input
inputTitle="Title"
inputTitle="Course Name"
value={title}
setValue={setTitle}
placeholder={
Expand Down
15 changes: 14 additions & 1 deletion src/pages/api/db/courses/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,20 @@ export default async function coursesHandler(
name: true,
},
},
upvotes: {
select: {
user_id: true,
},
},
anonymous: true,
user_id: true,
rating: true,
id: true,
_count: {
select: {
upvotes: true,
},
},
},
},
created_by: {
Expand Down Expand Up @@ -100,7 +110,10 @@ export default async function coursesHandler(
const { title, code, instructorId, isAnonymous } = body
const course = await prisma.course.findUnique({
where: {
code: code,
instructor_id_code: {
code: code,
instructor_id: instructorId,
},
},
})

Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/db/courses/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { NextApiRequest, NextApiResponse } from 'next'
import { adminAuth } from '../../../../utils/firebaseAdminInit'
import { prisma } from '../../../../utils/prismaClientInit'

export default async function courseUpvoteHandler(
export default async function courseReviewHandler(
req: NextApiRequest,
res: NextApiResponse
) {
Expand Down
87 changes: 87 additions & 0 deletions src/pages/api/db/courses/review_upvote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import type { NextApiRequest, NextApiResponse } from 'next'
import { adminAuth } from '../../../../utils/firebaseAdminInit'
import { prisma } from '../../../../utils/prismaClientInit'

export default async function courseReviewUpvoteHandler(
req: NextApiRequest,
res: NextApiResponse
) {
const { method, headers, body, query } = req

switch (method) {
case 'POST':
if (headers && headers.authorization) {
const accessToken = headers.authorization.split(' ')[1]
const user = await adminAuth.verifyIdToken(accessToken!)

if (user) {
try {
const { user_id } = user
const { course_review_id } = body

await prisma.course_review_upvote.create({
data: {
course_review_id,
user_id,
},
})

res.status(200).json({
message: 'Upvoted',
})
} catch (err: any) {
console.log(err)
res.status(405).json({
err,
})
}
} else {
res.status(401).json({
message: 'Unauthorized Access',
})
}
} else {
res.status(401).json({
message: 'Unauthorized Access',
})
}
break
case 'DELETE':
if (headers && headers.authorization) {
const accessToken = headers.authorization.split(' ')[1]
const user = await adminAuth.verifyIdToken(accessToken!)

if (user) {
try {
const { user_id } = user
const { id } = query

await prisma.course_review_upvote.deleteMany({
where: {
id: parseInt(id as string),
user_id,
},
})

res.status(200).json({
message: 'Removed Upvote',
})
} catch (err: any) {
console.log(err)
res.status(405).json({
err,
})
}
} else {
res.status(401).json({
message: 'Unauthorized Access',
})
}
} else {
res.status(401).json({
message: 'Unauthorized Access',
})
}
break
}
}
Loading

0 comments on commit d54c8d7

Please sign in to comment.