Skip to content

Latest commit

 

History

History
458 lines (186 loc) · 5.8 KB

README.md

File metadata and controls

458 lines (186 loc) · 5.8 KB

Backend - Trivia API

Setting up the Backend

Install Dependencies

  1. Python 3.7 - Follow instructions to install the latest version of python for your platform in the python docs

  2. Virtual Environment - We recommend working within a virtual environment whenever using Python for projects. This keeps your dependencies for each project separate and organized. Instructions for setting up a virual environment for your platform can be found in the python docs

  3. PIP Dependencies - Once your virtual environment is setup and running, install the required dependencies by navigating to the /backend directory and running:

pip install -r requirements.txt

Key Pip 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 to handle the lightweight SQL database. You'll primarily work in app.pyand can reference models.py.

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

Set up the Database

With Postgres running, create a trivia database:

createdb trivia

Populate the database using the trivia.psql file provided. From the backend folder in terminal run:

psql trivia < trivia.psql

Create env file

Create a .env file and copy it to ./backend. Example:

DB_USER='postgres'

DB_PASSWORD='password'

DB_NAME='trivia'

DB_HOST='localhost'

DB_PORT='5432'

Run the Server

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

To run the server, execute:

flask run --reload

The --reload flag will detect file changes and restart the server automatically.

Testing

Write at least one test for the success and at least one error behavior of each endpoint using the unittest library.

To deploy the tests, run

dropdb trivia_test

createdb trivia_test

psql trivia_test < trivia.psql

python test_flaskr.py


ENDPOINTS

GET '/categories'

  • Fetches a dictionary of categories

  • Request Arguments: None

  • Returns:

    • success: A boolean indicating the success of the request.

    • categories: An object of categories

  {

    'categories': {

      {'id': 1, 'type':'Science'},

      {'id': 2, 'type':'Art'},

      {'id': 3, 'type':'Geography'},

      {'id': 4, 'type':'History'},

      {'id': 5, 'type':'Entertainment'}

      {'id': 6, 'type':'Sports'},

    }

  }


GET '/questions?page=${integer}'

  • Fetches a paginated set of questions, a total number of questions, all categories and current category string.

  • Request Arguments: page - integer

  • Returns: An object with 10 paginated questions, total questions, object including all categories, and current category string

{

    'questions': [

        {

            'id': 1,

            'question': 'This is a question',

            'answer': 'This is an answer',

            'difficulty': 5,

            'category': 2

        },

    ],

    'totalQuestions': 100,

    'currentCategory': 'History',

    'categories': {

      {'id': 1, 'type':'Science'},

      {'id': 2, 'type':'Art'},

      {'id': 3, 'type':'Geography'},

      {'id': 4, 'type':'History'},

      {'id': 5, 'type':'Entertainment'},

      {'id': 6, 'type':'Sports'},

    },

}


GET '/categories/${category_id}/questions'

  • Fetches questions for a cateogry specified by id request argument

  • Request Arguments: id - integer

  • Returns: An object with questions for the specified category, total questions, and current category string

{

  "questions": [

    {

      "id": 1,

      "question": "Here is a question",

      "answer": "Here is an answer",

      "difficulty": 5,

      "category": 4

    }

  ],

  "totalQuestions": 99,

  "currentCategory": "History"

}

DELETE '/questions/${id}'

  • Deletes a specified question using the id of the question

  • Request Arguments: id - integer

  • Returns: Success message

{

  "success": true,

  "message": "Question deleted successfully"

}

POST '/quizzes'

  • Sends a post request in order to get the next question

  • Request Body:

{

  "previous_questions": [1, 4, 20, 15],

  "quiz_category": "current category"

}
  • Returns: a single new question object
{

  "question": {

    "id": 1,

    "question": "Here is a question",

    "answer": "Here is an answer",

    "difficulty": 5,

    "category": 4

  }

}

POST '/questions'

  • Sends a post request in order to add a new question

  • Request Body:

{

  "question": "Heres a new question string",

  "answer": "Heres a new answer string",

  "difficulty": 1,

  "category": 3

}
  • Returns: Return success status and new questions ID
{

    "success": True,

    "message": "Question created successfully",

    "question_id": 1,

}

POST '/questions/find'

  • Sends a post request in order to search for a specific question by search term

  • Request Body:

{

  "searchTerm": "searching"

}
  • Returns: any array of questions, a number of totalQuestions that met the search term and the current category string
{

  "questions": [

    {

      "id": 1,

      "question": "Here is a question",

      "answer": "Here is an answer",

      "difficulty": 3,

      "category": 3

    }

  ],

  "totalQuestions": 99,

  "currentCategory": "Entertainment"

}