Skip to content

Commit

Permalink
Merge pull request #25 from emmanueposu/jbranch5
Browse files Browse the repository at this point in the history
games,categories, questions, and choices now get added to the databas…
  • Loading branch information
emmanueposu authored May 16, 2024
2 parents 887bd35 + c8f2707 commit a2b48ea
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 45 deletions.
2 changes: 1 addition & 1 deletion trivia-forge/frontend/src/Components/Choices.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function Choices({ choices }) {
{choices.map((choice, index) => {
return (
<div className="card-body" key={index}>
<textarea className="form-control" defaultValue={choice.choice}></textarea>
<textarea className="form-control" defaultValue={choice.text}></textarea>
</div>
);
})}
Expand Down
5 changes: 2 additions & 3 deletions trivia-forge/frontend/src/Models/Category.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
9 changes: 4 additions & 5 deletions trivia-forge/frontend/src/Models/Choice.jsx
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
}
}
}
7 changes: 4 additions & 3 deletions trivia-forge/frontend/src/Models/Game.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
}
}
13 changes: 9 additions & 4 deletions trivia-forge/frontend/src/Models/Question.jsx
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
}
}
}
11 changes: 7 additions & 4 deletions trivia-forge/frontend/src/Pages/TriviaGenPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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);


Expand Down
31 changes: 25 additions & 6 deletions trivia-forge/frontend/src/Pages/TriviaReviewPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div>
<title>Trivia Review</title>
Expand All @@ -24,10 +37,16 @@ function TriviaReviewPage() {
</div>
))}
</div>
<div block className="trivia-button-container">
<Button variant="primary" className="trivia-review-button">
Save Changes
</Button>
<div className="trivia-button-container">
{page === 'edit' ? (
<Button variant="primary" onClick={HandleSaveGame} className="trivia-review-button">
Save Changes
</Button>
) : (
<Button variant="primary" onClick={HandleCreateGame} className="trivia-review-button">
Save New Game
</Button>
)}
</div>
</div>
);
Expand Down
78 changes: 68 additions & 10 deletions trivia-forge/frontend/src/Services/Services.jsx
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;
}
}
27 changes: 18 additions & 9 deletions trivia-forge/frontend/src/Services/TF-db_services.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
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 ************************************ */

Expand Down Expand Up @@ -75,8 +77,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');
Expand Down Expand Up @@ -126,7 +129,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) {
Expand All @@ -137,8 +140,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');
Expand Down Expand Up @@ -196,8 +202,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');
Expand Down Expand Up @@ -253,8 +260,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');
Expand Down

0 comments on commit a2b48ea

Please sign in to comment.