-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from emmanueposu/jbranch5
games,categories, questions, and choices now get added to the databas…
- Loading branch information
Showing
9 changed files
with
138 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
export class Choice { | ||
constructor(choice, questionID = null) { | ||
constructor(text, questionID = null) { | ||
this.id = null; | ||
this.choice = choice; | ||
this.text = text; | ||
this.questionID = questionID; | ||
} | ||
|
||
toJsonObject() { | ||
return { | ||
id: this.id, | ||
choice: this.choice, | ||
questionID: this.questionID | ||
text: this.text, | ||
question_id: this.questionID | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,28 @@ | ||
export class Question { | ||
constructor(question, answer, hint, categoryID = null) { | ||
constructor(question, answer, hint, multipleChoice, categoryID = null) { | ||
this.id = null; | ||
this.question = question; | ||
this.answer = answer; | ||
this.hint = hint; | ||
this.multipleChoice = multipleChoice; | ||
this.categoryID = categoryID; | ||
this.choices = []; | ||
} | ||
|
||
addChoice(choice) { | ||
this.choices.push(choice); | ||
} | ||
setCategoryID(categoryID) { | ||
this.category = categoryID; | ||
} | ||
|
||
toJsonObject() { | ||
return { | ||
id: this.id, | ||
question: this.question, | ||
problem: this.question, | ||
answer: this.answer, | ||
categoryID: this.category | ||
hint: this.hint, | ||
multiple_choice: this.multipleChoice, | ||
category_id: this.categoryID | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,76 @@ | ||
import * as db from './TF-db_services'; | ||
import { Game } from '../Models/Game'; | ||
import { Category } from '../Models/Category'; | ||
import { Question } from '../Models/Question'; | ||
import { Choice } from '../Models/Choice'; | ||
|
||
export const addAllForGame = async (game) => { | ||
const newGame = await db.addGame(game); | ||
|
||
|
||
// export const AddAllForGame = async (game) => { | ||
// const newGame = await db.addGame(game); | ||
// game.categories.forEach(async (category) => { | ||
// category.gameID = newGame.id; | ||
// const newCategory = await db.addCategory(category); | ||
// category.questions.forEach(async (question) => { | ||
// console.log("Added Category ID", category.id); | ||
// question.categoryID = newCategory.id; | ||
// const newQuestion = await db.addQuestion(question); | ||
// question.choices.forEach(async (choice) => { | ||
// choice.questionID = newQuestion.id; | ||
// await db.addChoice(choice); | ||
// }); | ||
// }); | ||
// }); | ||
// return newGame; | ||
// } | ||
export const AddAllForGame = async (game) => { | ||
try { | ||
// Add the game to the database | ||
console.log("Adding game:", game); | ||
const newGame = await db.addGame(game); | ||
console.log("Added game with ID:", newGame.id); | ||
|
||
// Process each category in the game's categories | ||
for (const category of game.categories) { | ||
category.gameID = newGame.id; // Link category to the game | ||
console.log("Adding category:", category); | ||
const newCategory = await db.addCategory(category); | ||
console.log("Added category with ID:", newCategory.id); | ||
|
||
// Process each question in the category's questions | ||
for (const question of category.questions) { | ||
question.categoryID = newCategory.id; // Link question to the category | ||
console.log("Adding question:", question); | ||
const newQuestion = await db.addQuestion(question); | ||
console.log("Added question with ID:", newQuestion.id); | ||
|
||
// Process each choice in the question's choices | ||
for (const choice of question.choices) { | ||
choice.questionID = newQuestion.id; // Link choice to the question | ||
console.log("Adding choice:", choice); | ||
const newChoice = await db.addChoice(choice); | ||
console.log("Added choice with ID:", newChoice.id); | ||
} | ||
} | ||
} | ||
|
||
// Return the newly created game | ||
return newGame; | ||
} catch (error) { | ||
console.error("Error adding game, categories, questions, or choices:", error); | ||
throw error; | ||
} | ||
}; | ||
|
||
export const UpdateAllForGame = async (game) => { | ||
await db.updateGame(game); | ||
game.categories.forEach(async (category) => { | ||
category.gameID = newGame.id; | ||
const newCategory = await db.addCategory(category); | ||
await db.updateCategory(category); | ||
category.questions.forEach(async (question) => { | ||
question.categoryID = newCategory.id; | ||
const newQuestion = await db.addQuestion(question); | ||
await db.updateQuestion(question); | ||
question.choices.forEach(async (choice) => { | ||
choice.questionID = newQuestion.id; | ||
await db.addChoice(choice); | ||
await db.updateChoice(choice); | ||
}); | ||
}); | ||
}); | ||
return newGame; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters