From e6498b356451a3215c3f3f909c4d8ac752b41d91 Mon Sep 17 00:00:00 2001 From: Jarviss77 Date: Fri, 12 Jan 2024 19:30:39 +0530 Subject: [PATCH] Final Commit --- prisma/schema.prisma | 155 ++++++++++++----------- sockets test/quiz.html | 23 +++- src/controllers/questions.controllers.js | 20 +-- src/controllers/room.controllers.js | 144 +++++++++++++++------ src/routes/v2/room.routes.js | 3 +- 5 files changed, 212 insertions(+), 133 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index a876ba0..ede0c82 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -1,106 +1,109 @@ generator client { - provider = "prisma-client-js" + provider = "prisma-client-js" } datasource db { - provider = "mysql" - url = env("DATABASE_URL") - relationMode = "prisma" + provider = "mysql" + url = env("DATABASE_URL") + relationMode = "prisma" } -// do not touch this model, this was for creating a non-empty db. model Init { - id Int @id @default(autoincrement()) + id Int @id @default(autoincrement()) } model User { - id Int @id @default(autoincrement()) - name String - email String @unique - password String - currPoints Int @default(0) - userRoom Room? @relation("RoomToUsers", fields: [userRoomId], references: [roomId]) - userRoomId Int? - pendingRooms Room[] @relation("RoomToPendingUsers") - isCreator Boolean @default(false) - messages Message[] @relation("UserToMessages") - results Result[] @relation("UserToResults") - - @@index([userRoomId], name: "userRoomId") + id Int @id @default(autoincrement()) + name String + email String @unique + password String + currPoints Int @default(0) + userRoomId Int? + isCreator Boolean @default(false) + userRoom Room? @relation("RoomToUsers", fields: [userRoomId], references: [roomId]) + pendingRooms Room[] @relation("RoomToPendingUsers") + messages Message[] @relation("UserToMessages") + results Result[] @relation("UserToResults") + + @@index([userRoomId], map: "userRoomId") } model Question { - questionId Int @id @default(autoincrement()) - question String - options Option[] @relation("QuestionToOptions") - quiz Quiz? @relation("QuizToQuestions", fields: [quizId], references: [quizId]) - quizId Int? + questionId Int @id @default(autoincrement()) + question String + quizId Int? + options Option[] @relation("QuestionToOptions") + quiz Quiz? @relation("QuizToQuestions", fields: [quizId], references: [quizId]) - @@index([quizId], name: "quizId") + @@index([quizId], map: "quizId") } model Option { - optionId Int @id @default(autoincrement()) - option String - question Question @relation("QuestionToOptions", fields: [questionId], references: [questionId], onDelete: Cascade) - questionId Int - isCorrect Boolean @default(false) - //the below two fields are not really required, so they'll be null in most cases - //they are initialised just to remove the prisma errors. - result Result? @relation("ResultToOptions", fields: [resultId], references: [resultId]) - resultId Int? - - @@index([questionId], name: "questionId") - @@index([resultId], name: "resultId") + optionId Int @id @default(autoincrement()) + option String + questionId Int + isCorrect Boolean @default(false) + resultId Int? + question Question @relation("QuestionToOptions", fields: [questionId], references: [questionId], onDelete: Cascade) + result Result? @relation("ResultToOptions", fields: [resultId], references: [resultId]) + + @@index([questionId], map: "questionId") + @@index([resultId], map: "resultId") } model Room { - roomId Int @id @default(autoincrement()) - roomName String - roomDescription String? - code String @unique - isClosed Boolean @default(false) - isInviteOnly Boolean @default(false) - pending User[] @relation("RoomToPendingUsers") - users User[] @relation("RoomToUsers") - messages Message[] @relation("RoomToMessages") - quizzes Quiz[] @relation("RoomToQuizzes") + roomId Int @id @default(autoincrement()) + roomName String + roomDescription String? + code String @unique + isInviteOnly Boolean @default(false) + isClosed Boolean @default(false) + pending User[] @relation("RoomToPendingUsers") + users User[] @relation("RoomToUsers") + messages Message[] @relation("RoomToMessages") + quizzes Quiz[] @relation("RoomToQuizzes") } model Message { - messageId Int @id @default(autoincrement()) - message String - user User @relation("UserToMessages", fields: [userId], references: [id]) - userId Int - room Room @relation("RoomToMessages", fields: [roomId], references: [roomId]) - roomId Int - - @@index([userId], name: "id") - @@index([roomId], name: "roomId") + messageId Int @id @default(autoincrement()) + message String + userId Int + roomId Int + user User @relation("UserToMessages", fields: [userId], references: [id]) + room Room @relation("RoomToMessages", fields: [roomId], references: [roomId]) + + @@index([userId], map: "id") + @@index([roomId], map: "roomId") } model Quiz { - quizId Int @id @default(autoincrement()) - questions Question[] @relation("QuizToQuestions") - room Room @relation("RoomToQuizzes", fields: [roomId], references: [roomId]) - roomId Int - //results of all the users who have attempted this quiz - //should be initialised as empty when the quiz is created. - //add all the user's results after the quiz is over. - results Result[] @relation("ResultToQuiz") - - @@index([roomId], name: "roomId") + quizId Int @id @default(autoincrement()) + roomId Int + questions Question[] @relation("QuizToQuestions") + room Room @relation("RoomToQuizzes", fields: [roomId], references: [roomId]) + results Result[] @relation("ResultToQuiz") + + @@index([roomId], map: "roomId") } model Result { - resultId Int @id @default(autoincrement()) - quiz Quiz @relation("ResultToQuiz", fields: [quizId], references: [quizId]) - quizId Int @unique - optionsMarked Option[] @relation("ResultToOptions") - score Int - user User @relation("UserToResults", fields: [userId], references: [id]) - userId Int - - @@index([quizId], name: "quizId") - @@index([userId], name: "userId") + resultId Int @id @default(autoincrement()) + quizId Int + score Int + userId Int + quiz Quiz @relation("ResultToQuiz", fields: [quizId], references: [quizId]) + optionsMarked Option[] @relation("ResultToOptions") + user User @relation("UserToResults", fields: [userId], references: [id]) + + @@index([quizId], map: "quizId") + @@index([userId], map: "userId") +} + +model RoomToPendingUsers { + A Int + B Int + + @@unique([A, B], map: "_RoomToPendingUsers_AB_unique") + @@index([B], map: "_RoomToPendingUsers_B_index") + @@map("_RoomToPendingUsers") } diff --git a/sockets test/quiz.html b/sockets test/quiz.html index d5eed46..748bb23 100644 --- a/sockets test/quiz.html +++ b/sockets test/quiz.html @@ -87,8 +87,26 @@ }); }; + function addQuiz() { + fetch(`http://localhost:4000/api/v2/room/addQuiz?roomCode=${roomCode}`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'authorization': 'Bearer ' + document.getElementById('token').value, + } + }) + .then(response => response.json()) + .then(data => { + console.log('Success:', data); + }) + .catch((error) => { + console.error('Error:', error); + }); + } + function startQuiz() { - fetch(`http://localhost:4000/api/v1/room/startQuiz?roomCode=${roomCode}`, { + var quizId = document.getElementById('quizId').value; + fetch(`http://localhost:4000/api/v1/room/startQuiz?roomCode=${roomCode}&quizId=${quizId}`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -119,10 +137,13 @@

Quiz Testing

Enter the room code

+

Enter the QuizId

+

Enter the token

+