Skip to content

dereck22dev/TRIVIA-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

API Documentation

Trivia App

Udacity is invested in creating bonding experiences for its employees and students.

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:

createbd trivia

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

psql trivia < trivia.psql

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

To deploy the tests:

1-open the file models.py and replace DB_NAME by DB_TEST in the variable database_dir

2-run

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

Frontend - Trivia API

Getting Setup

tip: this frontend is designed to work with Flask-based Backend so it will not load successfully if the backend is not working or not connected. We recommend that you stand up the backend first, test using Postman or curl, update the endpoints in the frontend, and then the frontend should integrate smoothly.

Installing Dependencies
  1. Installing Node and NPM This project depends on Nodejs and Node Package Manager (NPM). Before continuing, you must download and install Node (the download includes NPM) from https://nodejs.com/en/download.

  2. Installing project 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

tip: npm iis shorthand for `npm install``

Running Your 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
Expected endpoints and behaviors

GET '/categories'

  • Fetches a dictionary of categories in which the keys are the ids and the value is the corresponding string of the category
  • Request Arguments: None
  • Returns: An object with a single key, categories, that contains an object of id: category_string key:value pairs.
{
  "categories": {
    "1": "Science",
    "2": "Art",
    "3": "Geography",
    "4": "History",
    "5": "Entertainment",
    "6": "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": 50,
  "categories": {
    "1": "Science",
    "2": "Art",
    "3": "Geography",
    "4": "History",
    "5": "Entertainment",
    "6": "Sports"
  },
  "currentCategory": "History"
}

GET '/categories/${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": "This is a question",
      "answer": "This is an answer",
      "difficulty": 5,
      "category": 4
    }
  ],
  "totalQuestions": 50,
  "currentCategory": "History"
}

DELETE '/questions/${id}'

  • Deletes a specified question using the id of the question
  • Request Arguments: id - integer
  • Returns: Does not need to return anything besides the appropriate HTTP status code. Optionally can return the id of the question. If you are able to modify the frontend, you can have it remove the question using the id instead of refetching the questions.

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": "This is a question",
    "answer": "This is an answer",
    "difficulty": 1,
    "category": 1
  }
}

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": 2
}
  • Returns: Does not return any new data

POST '/questions'

  • Sends a post request in order to search for a specific question by search term
  • Request Body:
{
  "searchTerm": "this is the term the user is looking for"
}
  • Returns: any array of questions, a number of totalQuestions that met the search term and the current category string
{
  "questions": [
    {
      "id": 5,
      "question": "This is a question",
      "answer": "This is an answer",
      "difficulty": 1,
      "category": 3
    }
  ],
  "totalQuestions": 50,
  "currentCategory": "Art"
}

About

Trivia Quizz Web Aplication

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published