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

Final Commit #56

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
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
155 changes: 79 additions & 76 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -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")
}
23 changes: 22 additions & 1 deletion sockets test/quiz.html
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -119,10 +137,13 @@
<h2>Quiz Testing</h2>
<h3>Enter the room code</h3>
<input type="text" id="roomCode">
<h3>Enter the QuizId</h3>
<input type="text" id="quizId">
<h3>Enter the token</h3>
<input type="text" id="token">
<button onclick="joinRoom()">Join Room</button>
<button onclick="leaveRoom()">Leave Room</button>
<button onclick="addQuiz()">Add Quiz</button>
<button onclick="startQuiz()">Start Quiz</button>
<div class="roomDetails" hidden>
<h2>Room Details</h2>
Expand Down
20 changes: 3 additions & 17 deletions src/controllers/questions.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ async function addMultipleChoiceQuestion(req, res) {
try {
const question = req.body.question;
const options = req.body.options;
const roomCode = req.query.roomCode;
const quizId = req.query.quizId;
const quizId = parseInt(req.query.quizId);

const newQuestion = await prisma.question.create({
data: {
question,
room: {
quiz: {
connect: {
code: roomCode
quizId: quizId
}
},
options: {
Expand All @@ -30,19 +29,6 @@ async function addMultipleChoiceQuestion(req, res) {
}
});

await prisma.quiz.update({
where: {
quizId: quizId
},
data: {
questions: {
connect: {
questionId: newQuestion.questionId
}
}
}
});

response_200(res, 'Question added successfully', newQuestion);
} catch (error) {
console.error(`Error adding question: ${error}`);
Expand Down
Loading