Skip to content

Trivia API is a web application that creates bonding experiences for its users

License

Notifications You must be signed in to change notification settings

donkodimov/trivia-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Trivia API

Trivia API is a web application that creates bonding experiences for its users. It can be hold on a regular basis via a webpage to manage the trivia app and play the game.

The user can:

  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

Python 3.7

Follow instructions to install the latest version of python for your platform in the python docs

Virtual Enviornment

Working within a virtual environment is recommended.

PIP Dependencies

navigate to the /backend directory and run:

pip install -r requirements.txt

This will install all of the required packages in the requirements.txt file.

Key Dependencies
  • Flask is a lightweight backend microservices framework. Flask is required to handle requests and responses.

  • SQLAlchemy is the Python SQL toolkit and ORM we'll use handle the lightweight sqlite database. You'll primarily work in app.py and can reference models.py.

  • Flask-CORS is the extension we'll use to handle cross origin requests from our frontend server.

Database Setup

With Postgres running, restore a database using the trivia.psql file provided. From the backend folder in terminal run:

psql trivia < trivia.psql

Running the server

From within the backend directory

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

Frontend Dependencies

This project uses NPM to manage software dependencies. from the frontend directory run:

npm install

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

API Reference

Getting Started

  • Backend Base URL: http://127.0.0.1:5000/
  • Frontend Base URL: http://127.0.0.1:3000/
  • Authentication is not implemented yet. The API can be used for free.

Error Handling

Errors are returned as JSON objects in the following format:

{
    "success": False, 
    "error": 404,
    "message": "Resource Not Found"
}

The API will return three error types when requests fail:

  • 400: Bad Request
  • 404: Resource Not Found
  • 405: Method Not Allowed
  • 422: Not Processable

Endpoints

Get all categories

Request

GET /categories

curl http://127.0.0.1:5000/categories

Response

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

Get all questions

Request

GET /questions

curl http://127.0.0.1:5000/questions

Response

{
  "categories": {
    "1": "Science", 
    "2": "Art", 
    "3": "Geography", 
    "4": "History", 
    "5": "Entertainment", 
    "6": "Sports"
  }, 
  "currentCategory": "Science", 
  "questions": [
    {
      "answer": "Apollo 13", 
      "category": 5, 
      "difficulty": 4, 
      "id": 2, 
      "question": "What movie earned Tom Hanks his third straight Oscar nomination, in 1996?"
    }, 
    {
      "answer": "Tom Cruise", 
      "category": 5, 
      "difficulty": 4, 
      "id": 4, 
      "question": "What actor did author Anne Rice first denounce, then praise in the role of her beloved Lestat?"
    }, 
    {
      "answer": "Maya Angelou", 
      "category": 4, 
      "difficulty": 2, 
      "id": 5, 
      "question": "Whose autobiography is entitled 'I Know Why the Caged Bird Sings'?"
    }, 
    {
      "answer": "Edward Scissorhands", 
      "category": 5, 
      "difficulty": 3, 
      "id": 6, 
      "question": "What was the title of the 1990 fantasy directed by Tim Burton about a young man with multi-bladed appendages?"
    }, 
    {
      "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?"
    }
  ], 
  "total_questions": 19
}

Delete a question by id

Request

GET /questions/<int:id>

curl http://127.0.0.1:5000/questions/9 -X DELETE

Response

    {
        "success": true,
        "id": 9,
        "message": "Question deleted"
    }

Get all questions for a category

Request

GET /categories/<int:cat_id>/questions

curl http://127.0.0.1:5000/categories/1/questions

Response

{
  "currentCategory": "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?"
    }
  ],
  "totalQuestions": 3
}

Add new question

Request

POST /questions

curl http://127.0.0.1:5000/questions -X POST -H "Content-Type: application/json" -d '{
        "question": "When did Perseverance rover landed on Mars",
        "answer": "18 Feb 2021",
        "difficulty": 3,
        "category": "1"
    }'

Response

    {
        "success": true,
        "id": 28
    }

Search for a question

Request

POST /questions/search

curl http://127.0.0.1:5000/questions/search -X POST -H "Content-Type: application/json" -d '{
        "searchTerm": "Perseverance"
    }'

Response

    {
        "questions": [
            {
            "answer": "18 Feb 2021",
            "category": 1,
            "difficulty": 3,
            "id": 28,
            "question": "When did Perseverance rover landed on Mars"
            }
        ],
        "success": true,
        "totalQuestions": 1
    }

Start quiz

Request

POST /quizzes

curl http://127.0.0.1:5000/quizzes -X POST -H "Content-Type: application/json" -d '{
    "previous_questions": [1, 7],
    "quiz_category": {"type": "Science", "id": "1"}
    }'

Response

{
    "question": {
        "answer": "Alexander Fleming",
        "category": 1,
        "difficulty": 3,
        "id": 21,
        "question": "Who discovered penicillin?"
    },
    "success": true
}

Authors

  • Backend integration and testing: Donko Dimov
  • Frontend and all starter files provided by Udacity Team

About

Trivia API is a web application that creates bonding experiences for its users

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published