Skip to content

This application is an interactive game where players can test their knowledge by answering trivia questions.

Notifications You must be signed in to change notification settings

OmarMuhammedAli/Trivia

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Full Stack Trivia API

This application is an interactive game where players can test their knowledge answering trivia questions. The main tasks were to create a Flask RESTful API, essential database models, and a test suite for implementing the following functionality:

  1. Display questions - both all questions and by category. Questions should show the question, category and difficulty rating by default and can show/hide the answer.
  2. Delete questions.
  3. Add questions and require that they include question and answer text.
  4. Search for questions based on a text query string.
  5. Play the quiz game, randomizing either all questions or within a specific category.

Getting Started

Installing Dependencies

Developers using this project should already have Python3, pip, node, and npm installed.

Frontend Dependencies

This project uses NPM to manage software dependencies. NPM Relies on the package.json file located in the frontend directory of this repository. After cloning, open your terminal and run:

npm install

Backend Dependencies

Once you have your virtual environment setup and running, install dependencies by naviging to the /backend directory and running:

pip install -r requirements.txt

Running the Frontend in Dev Mode

The frontend app was built using create-react-app. In order to run the app in development mode use npm start. You can change the script in the package.json file.

Open http://localhost:3000 to view it in the browser. The page will reload if you make edits.

npm start

Setting up the Environment Variables

Create a .env file in the backend directory and fill in the values:

DRIVER_NAME=......
USERNAME=......
PASSWORD=......
HOST=......
DB_PORT=......
DB_NAME=......
TEST_DB_NAME=trivia_test
TEST_DB_PATH=......

Running the Server

From within the backend directory first ensure you are working using your created virtual environment.

To run the server, execute:

export FLASK_APP=flaskr
export FLASK_ENV=development
flask run

Testing

To run the tests, run

dropdb trivia_test
createdb trivia_test
psql trivia_test < trivia.psql
python test_flaskr.py

Omit the dropdb command the first time you run tests.

API Reference

Getting Started

  • Base URL:This application is hosted locally at http://127.0.0.1:5000/. A public URL will be added here once the application is deployed.
  • Authentication: No authentication or API keys are required for this version of the application.

Error Handling

Errors are returned as JSON and are formatted in the following manner:

{
    "success": False,
    "error": 404,
    "message": "resource not found"
}

Example errors the user may incounter using Trivia API:

  • 400 – bad request
  • 404 – resource not found
  • 422 – unprocessable

Endpoints

GET /categories

  • General: Returns a list categories.

  • Sample: curl http://127.0.0.1:5000/categories

      {
          "categories": {
              "1": "Science",
              "2": "Art",
              "3": "Geography",
              "4": "History",
              "5": "Entertainment",
              "6": "Sports"
          },
          "success": true
      }
    

GET /questions

  • General:
    • Returns a list questions paginated in groups of 10.
  • Sample: curl http://127.0.0.1:5000/questions
   {
        "categories": {
            "1": "Science",
            "2": "Art",
            "3": "Geography",
            "4": "History",
            "5": "Entertainment",
            "6": "Sports"
        },
        "current_category": null,
        "questions": [
            {
                "answer": "Maya Angelou",
                "category": "4",
                "difficulty": 2,
                "id": 5,
                "question": "Whose autobiography is entitled 'I Know Why the Caged Bird Sings'?"
            },
            {
                "answer": "Muhammad Ali",
                "category": "4",
                "difficulty": 1,
                "id": 9,
                "question": "What boxer's original name is Cassius Clay?"
            },
            {
                "answer": "Brazil",
                "category": "6",
                "difficulty": 3,
                "id": 10,
                "question": "Which is the only team to play in every soccer World Cup tournament?"
            },
            {
                "answer": "Uruguay",
                "category": "6",
                "difficulty": 4,
                "id": 11,
                "question": "Which country won the first ever soccer World Cup in 1930?"
            },
            {
                "answer": "George Washington Carver",
                "category": "4",
                "difficulty": 2,
                "id": 12,
                "question": "Who invented Peanut Butter?"
            },
            {
                "answer": "Lake Victoria",
                "category": "3",
                "difficulty": 2,
                "id": 13,
                "question": "What is the largest lake in Africa?"
            },
            {
                "answer": "The Palace of Versailles",
                "category": "3",
                "difficulty": 3,
                "id": 14,
                "question": "In which royal palace would you find the Hall of Mirrors?"
            },
            {
                "answer": "Mona Lisa",
                "category": "2",
                "difficulty": 3,
                "id": 17,
                "question": "La Giaconda is better known as what?"
            },
            {
                "answer": "One",
                "category": "2",
                "difficulty": 4,
                "id": 18,
                "question": "How many paintings did Van Gogh sell in his lifetime?"
            },
            {
                "answer": "Jackson Pollock",
                "category": "2",
                "difficulty": 2,
                "id": 19,
                "question": "Which American artist was a pioneer of Abstract Expressionism, and a leading exponent of action painting?"
            }
        ],
        "success": true,
        "total_questions": 21
    }

DELETE /questions/<int:question_id>

  • General:

    • Deletes a question by id using url parameters.
  • Sample: curl http://127.0.0.1:5000/questions/6 -X DELETE

      {
          "success": true,
          "total_question": 20
      }
    

POST /questions

This endpoint creates a new question.

  • General:
    • Creates a new question using JSON request parameters.
    • Returns JSON object with newly created question, as well as paginated questions.
  • Sample: curl http://127.0.0.1:5000/questions -X POST -H "Content-Type: application/json" -d '{"question": "In which year did the egyptian revolution occur?", "answer": "2011", "difficulty": 2, "category": "4"}'
   {
        "categories": {
            "1": "Science",
            "2": "Art",
            "3": "Geography",
            "4": "History",
            "5": "Entertainment",
            "6": "Sports"
        },
        "current_category": null,
        "questions": [
            {
                "answer": "Maya Angelou",
                "category": "4",
                "difficulty": 2,
                "id": 5,
                "question": "Whose autobiography is entitled 'I Know Why the Caged Bird Sings'?"
            },
            {
                "answer": "Muhammad Ali",
                "category": "4",
                "difficulty": 1,
                "id": 9,
                "question": "What boxer's original name is Cassius Clay?"
            },
            {
                "answer": "Brazil",
                "category": "6",
                "difficulty": 3,
                "id": 10,
                "question": "Which is the only team to play in every soccer World Cup tournament?"
            },
            {
                "answer": "Uruguay",
                "category": "6",
                "difficulty": 4,
                "id": 11,
                "question": "Which country won the first ever soccer World Cup in 1930?"
            },
            {
                "answer": "George Washington Carver",
                "category": "4",
                "difficulty": 2,
                "id": 12,
                "question": "Who invented Peanut Butter?"
            },
            {
                "answer": "Lake Victoria",
                "category": "3",
                "difficulty": 2,
                "id": 13,
                "question": "What is the largest lake in Africa?"
            },
            {
                "answer": "The Palace of Versailles",
                "category": "3",
                "difficulty": 3,
                "id": 14,
                "question": "In which royal palace would you find the Hall of Mirrors?"
            },
            {
                "answer": "Mona Lisa",
                "category": "2",
                "difficulty": 3,
                "id": 17,
                "question": "La Giaconda is better known as what?"
            },
            {
                "answer": "One",
                "category": "2",
                "difficulty": 4,
                "id": 18,
                "question": "How many paintings did Van Gogh sell in his lifetime?"
            },
            {
                "answer": "Jackson Pollock",
                "category": "2",
                "difficulty": 2,
                "id": 19,
                "question": "Which American artist was a pioneer of Abstract Expressionism, and a leading exponent of action painting?"
            },
            {
                "answer": "2011",
                "category": "4",
                "difficulty": 2,
                "id": 20,
                "question": "In which year did the egyptian revolution occur?"
            }
        ],
        "success": true,
        "total_questions": 22
    }

POST/search

This endpoit search for a list of questions based on a search term.

  • General:
    • Searches for questions using search term in JSON request parameters.
    • Returns JSON object with paginated matching questions.
  • Sample: curl http://127.0.0.1:5000/questions -X POST -H "Content-Type: application/json" -d '{"searchTerm": "what"}'
   {
        "categories": {
            "1": "Science",
            "2": "Art",
            "3": "Geography",
            "4": "History",
            "5": "Entertainment",
            "6": "Sports"
        },
        "current_category": null,
        "questions": [
            {
                "answer": "Muhammad Ali",
                "category": "4",
                "difficulty": 1,
                "id": 9,
                "question": "What boxer's original name is Cassius Clay?"
            },
            {
                "answer": "Lake Victoria",
                "category": "3",
                "difficulty": 2,
                "id": 13,
                "question": "What is the largest lake in Africa?"
            },
            {
                "answer": "Mona Lisa",
                "category": "2",
                "difficulty": 3,
                "id": 17,
                "question": "La Giaconda is better known as what?"
            },
        ],
        "success": true,
        "total_questions": 3
    }

GET /categories/<int:id>/questions

  • General:

    • Gets questions by category id using url parameters.
    • Returns JSON object with paginated matching results.
  • Sample: curl http://127.0.0.1:5000/categories/1/questions

      {
          "current_category": "Science",
          "questions": [
              {
                  "answer": "The Liver",
                  "category": 1,
                  "difficulty": 4,
                  "id": 20,
                  "question": "What is the heaviest organ in the human body?"
              },
              {
                  "answer": "Alexander Fleming",
                  "category": 1,
                  "difficulty": 3,
                  "id": 21,
                  "question": "Who discovered penicillin?"
              },
              {
                  "answer": "Blood",
                  "category": 1,
                  "difficulty": 4,
                  "id": 22,
                  "question": "Hematology is a branch of medicine involving the study of what?"
              }
          ],
          "success": true,
          "total_questions": 3
      }
    

POST /quizzes

  • General:

    • Lets the user play a game of trivia.
    • Uses JSON request parameters of category and previous questions.
    • Returns JSON object with random question that hasn't been provided before.
  • Sample: curl http://127.0.0.1:5000/quizzes -X POST -H "Content-Type: application/json" -d '{"previous_questions": [9, 5], "quiz_category": {"type": "History", "id": "4"}}'

      {
          "question": {
              "answer": "One",
              "category": "2",
              "difficulty": 4,
              "id": 18,
              "question": "How many paintings did Van Gogh sell in his lifetime?"
          },
          "success": true
      }
    

Authors

The API (backend/flaskr/__init__.py), test suite (backend/test_flaskr.py), Database models (backend/flaskr/models.py), Utilities (backend/flaskr/utils.py), and this README were authored by Omar Muhammed Ali.
The front-end was provided by Udacity as part of the graduation projects of the Full-Stack Nanodegree.

About

This application is an interactive game where players can test their knowledge by answering trivia questions.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •