diff --git a/trivia-forge/frontend/src/Components/Choices.jsx b/trivia-forge/frontend/src/Components/Choices.jsx index 15ad2bc7..f0c8c45c 100644 --- a/trivia-forge/frontend/src/Components/Choices.jsx +++ b/trivia-forge/frontend/src/Components/Choices.jsx @@ -7,7 +7,7 @@ function Choices({ choices }) { {choices.map((choice, index) => { return (
- +
); })} diff --git a/trivia-forge/frontend/src/Models/Category.jsx b/trivia-forge/frontend/src/Models/Category.jsx index 0e314fa8..8cba1065 100644 --- a/trivia-forge/frontend/src/Models/Category.jsx +++ b/trivia-forge/frontend/src/Models/Category.jsx @@ -12,9 +12,8 @@ export class Category { toJsonObject() { return { - id: this.id, - name: this.name, - gameID: this.gameID + title: this.name, + game_id: this.gameID } } } \ No newline at end of file diff --git a/trivia-forge/frontend/src/Models/Choice.jsx b/trivia-forge/frontend/src/Models/Choice.jsx index e4b46a17..397abd11 100644 --- a/trivia-forge/frontend/src/Models/Choice.jsx +++ b/trivia-forge/frontend/src/Models/Choice.jsx @@ -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 } } } \ No newline at end of file diff --git a/trivia-forge/frontend/src/Models/Game.jsx b/trivia-forge/frontend/src/Models/Game.jsx index 9cacab70..9eb40439 100644 --- a/trivia-forge/frontend/src/Models/Game.jsx +++ b/trivia-forge/frontend/src/Models/Game.jsx @@ -13,9 +13,10 @@ export class Game { toJsonObject() { return { - name: this.name, - theme: this.theme, - userID: this.userID + title: this.name, + user_id: this.userID, + Theme: this.theme + } } } \ No newline at end of file diff --git a/trivia-forge/frontend/src/Models/Question.jsx b/trivia-forge/frontend/src/Models/Question.jsx index 5794ed91..1a47360f 100644 --- a/trivia-forge/frontend/src/Models/Question.jsx +++ b/trivia-forge/frontend/src/Models/Question.jsx @@ -1,9 +1,10 @@ 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 = []; } @@ -11,13 +12,17 @@ export class Question { 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 } } } \ No newline at end of file diff --git a/trivia-forge/frontend/src/Pages/TriviaGenPage.jsx b/trivia-forge/frontend/src/Pages/TriviaGenPage.jsx index 87eb1bfb..b1e35dd6 100644 --- a/trivia-forge/frontend/src/Pages/TriviaGenPage.jsx +++ b/trivia-forge/frontend/src/Pages/TriviaGenPage.jsx @@ -75,7 +75,8 @@ function TriviaGenPage() { } } //create a new game and category object and add category to game - let game = new Game(Title, Theme); + //need to change third parameter to current User ID once Users can sign in. + let game = new Game(Title, Theme, 1); for (let i = 0; i < categories.length; i++) { let newCategory = new Category(categories[i].name); @@ -100,7 +101,7 @@ function TriviaGenPage() { let hint = sections[i + 6]; //create question object and add it to category - let newQuestion = new Question(question, answer, hint); + let newQuestion = new Question(question, answer, hint, isMultipleChoice); newCategory.addQuestion(newQuestion); //add choices to question object @@ -109,10 +110,12 @@ function TriviaGenPage() { } } - + console.log("Category Questions:", newCategory.questions); } + console.log("Game Categories", game.categories); + // state property to pass data as object to new route - navigate('/review', { state: { game } }); + navigate('/review', { state: { game, page: 'review' } }); //console.log(completion.choices[0].message); diff --git a/trivia-forge/frontend/src/Pages/TriviaReviewPage.jsx b/trivia-forge/frontend/src/Pages/TriviaReviewPage.jsx index 0a8ce3a5..bec4d27b 100644 --- a/trivia-forge/frontend/src/Pages/TriviaReviewPage.jsx +++ b/trivia-forge/frontend/src/Pages/TriviaReviewPage.jsx @@ -2,15 +2,28 @@ import React from 'react'; import { useLocation } from 'react-router-dom'; // used to access passed state import Categories from '../Components/Categories'; import { Button } from 'react-bootstrap'; +import { AddAllForGame, UpdateAllForGame } from '../Services/Services'; +import { useNavigate } from "react-router-dom"; import '../App.css'; function TriviaReviewPage() { // Reference: https://reactrouter.com/en/main/hooks/use-location // pulls object from state property in TriviaGenPage const location = useLocation(); - const { game } = location.state; + const { game, page } = location.state; let categories = game.categories; - + const navigate = useNavigate(); + + const HandleSaveGame = () => { + UpdateAllForGame(game); + navigate('/myTrivia'); + }; + + const HandleCreateGame = () => { + AddAllForGame(game); + navigate('/myTrivia'); + }; + return (
@@ -23,10 +36,16 @@ function TriviaReviewPage() {
))}
-
- +
+ {page === 'edit' ? ( + + ) : ( + + )}
); diff --git a/trivia-forge/frontend/src/Services/Services.jsx b/trivia-forge/frontend/src/Services/Services.jsx index 1f02d816..df340f8d 100644 --- a/trivia-forge/frontend/src/Services/Services.jsx +++ b/trivia-forge/frontend/src/Services/Services.jsx @@ -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; -} \ No newline at end of file +} diff --git a/trivia-forge/frontend/src/Services/TF-db_services.jsx b/trivia-forge/frontend/src/Services/TF-db_services.jsx index e650a8d1..1c6f5a40 100644 --- a/trivia-forge/frontend/src/Services/TF-db_services.jsx +++ b/trivia-forge/frontend/src/Services/TF-db_services.jsx @@ -1,9 +1,11 @@ import axios from 'axios'; -import {User} from '../Models/User'; -import {Game} from '../Models/Game'; -import {Question} from '../Models/Question'; +import { User } from '../Models/User'; +import { Game } from '../Models/Game'; +import { Question } from '../Models/Question'; +import { Category } from '../Models/Category'; +import { Choice } from '../Models/Choice'; -const API_URL = 'http://localhost:5000'; +const API_URL = 'http://127.0.0.1:5000'; /* ************************************ User ************************************ */ @@ -74,8 +76,9 @@ export const getGame = async (game) => { } export const addGame = async (game) => { + let newGame = new Game(game.name, game.theme, game.userID); try { - const response = await axios.post(`${API_URL}/games`, game.toJsonObject()); + const response = await axios.post(`${API_URL}/games`, newGame.toJsonObject()); return response.data; } catch (error) { console.error('Failed to add game'); @@ -125,7 +128,7 @@ export const getQuestions = async (category_ids) => { export const getQuestion = async (question) => { try { - const response = await axios.get(`${API_URL}/questions${question.id}`); + const response = await axios.get(`${API_URL}/questions/${question.id}`); const { id, question, answer, categoryID } = response.data; return new Question(id, question, answer, categoryID); } catch (error) { @@ -136,8 +139,11 @@ export const getQuestion = async (question) => { }; export const addQuestion = async (question) => { + console.log("questionCategoryID:", question.categoryID) + let newQuestion = new Question(question.question, question.answer, question.hint, question.multipleChoice, question.categoryID); try { - const response = await axios.post(`${API_URL}/questions/${question.id}`, question.toJsonObject()); + const response = await axios.post(`${API_URL}/questions`, newQuestion.toJsonObject()); + console.log("question:", newQuestion.toJsonObject()) return response.data; } catch (error) { console.error('Failed to add question'); @@ -195,8 +201,9 @@ export const getCategory = async (category) => { }; export const addCategory = async (category) => { + let newCategory = new Category(category.name, category.gameID); try { - const response = await axios.post(`${API_URL}/categories`, category.toJsonObject()); + const response = await axios.post(`${API_URL}/categories`, newCategory.toJsonObject()); return response.data; } catch (error) { console.error('Failed to add category'); @@ -249,8 +256,10 @@ export const getChoice = async (choice) => { } export const addChoice = async (choice) => { + let newChoice = new Choice(choice.text, choice.questionID); + console.log("newChoice:", newChoice.toJsonObject()) try { - const response = await axios.post(`${API_URL}/choices`, choice.toJsonObject()); + const response = await axios.post(`${API_URL}/choices`, newChoice.toJsonObject()); return response.data; } catch (error) { console.error('Failed to add choice');