Skip to content

riorizki/trivia_api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Full Stack API Final Project

Full Stack Trivia

Udacity is invested in creating bonding experiences for its employees and students. A bunch of team members got the idea to hold trivia on a regular basis and created a webpage to manage the trivia app and play the game, but their API experience is limited and still needs to be built out.

That's where you come in! Help them finish the trivia app so they can start holding trivia and seeing who's the most knowledgeable of the bunch. The application must:

  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.

Completing this trivia app will give you the ability to structure plan, implement, and test an API - skills essential for enabling your future applications to communicate with others.

Tasks

There are TODO comments throughout project. Start by reading the READMEs in:

  1. ./frontend/
  2. ./backend/

We recommend following the instructions in those files in order. This order will look familiar from our prior work in the course.

Starting and Submitting the Project

Fork the project repository and Clone your forked repository to your machine. Work on the project locally and make sure to push all your changes to the remote repository before submitting the link to your repository in the Classroom.

About the Stack

We started the full stack application for you. It is desiged with some key functional areas:

Backend

The ./backend directory contains a partially completed Flask and SQLAlchemy server. You will work primarily in app.py to define your endpoints and can reference models.py for DB and SQLAlchemy setup.

Frontend

The ./frontend directory contains a complete React frontend to consume the data from the Flask server. You will need to update the endpoints after you define them in the backend. Those areas are marked with TODO and can be searched for expediency.

Pay special attention to what data the frontend is expecting from each API response to help guide how you format your API.

View the README.md within ./frontend for more details.

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

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 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 https://localhost:3000 to view it in the browser. The page will reload if you make edits.

npm start

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

NOTE: Make sure you create a database named trivia_test in the PostgreSQL server before running the tests.

API Reference

Getting Started

  • Base URL: Currently this application is only hosted locally. The backend is hosted at https://127.0.0.1:5000/
  • Authentication: This version does not require authentication or API keys.

Error Handling

Errors are returned as JSON in the following format:

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

The API will return three types of errors:

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

Endpoints

GET /categories

  • General: Returns a list categories.

  • Sample: curl https://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.
    • Results are paginated in groups of 10.
    • Also returns list of categories and total number of questions.
  • Sample: curl https://127.0.0.1:5000/questions

      {
          "categories": {
              "1": "Science",
              "2": "Art",
              "3": "Geography",
              "4": "History",
              "5": "Entertainment",
              "6": "Sports"
          },
          "questions": [
              {
                  "answer": "Colorado, New Mexico, Arizona, Utah",
                  "category": 3,
                  "difficulty": 3,
                  "id": 164,
                  "question": "Which four states make up the 4 Corners region of the US?"
              },
              {
                  "answer": "Muhammad Ali",
                  "category": 4,
                  "difficulty": 1,
                  "id": 9,
                  "question": "What boxer's original name is Cassius Clay?"
              },
              {
                  "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": "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": "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?"
              }
          ],
          "success": true,
          "total_questions": 19
      }
    

DELETE /questions/<int:id>

  • General:

    • Deletes a question by id using url parameters.
    • Returns id of deleted question upon success.
  • Sample: curl https://127.0.0.1:5000/questions/6 -X DELETE

      {
          "deleted": 6,
          "success": true
      }
    

POST /questions

This endpoint either creates a new question or returns search results.

  1. If no search term is included in request:
  • General:

    • Creates a new question using JSON request parameters.
    • Returns JSON object with newly created question, as well as paginated questions.
  • Sample: curl https://127.0.0.1:5000/questions -X POST -H "Content-Type: application/json" -d '{ "question": "Which US state contains an area known as the Upper Penninsula?", "answer": "Michigan", "difficulty": 3, "category": "3" }'

      {
          "created": 173,
          "question_created": "Which US state contains an area known as the Upper Penninsula?",
          "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": "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": "Agra",
                  "category": 3,
                  "difficulty": 2,
                  "id": 15,
                  "question": "The Taj Mahal is located in which Indian city?"
              },
              {
                  "answer": "Escher",
                  "category": 2,
                  "difficulty": 1,
                  "id": 16,
                  "question": "Which Dutch graphic artist\u2013initials M C was a creator of optical illusions?"
              }
          ],
          "success": true,
          "total_questions": 20
      }
    
  1. If search term is included in request:
  • General:

    • Searches for questions using search term in JSON request parameters.
    • Returns JSON object with paginated matching questions.
  • Sample: curl https://127.0.0.1:5000/questions -X POST -H "Content-Type: application/json" -d '{"searchTerm": "which"}'

      {
          "questions": [
              {
                  "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": "The Palace of Versailles",
                  "category": 3,
                  "difficulty": 3,
                  "id": 14,
                  "question": "In which royal palace would you find the Hall of Mirrors?"
              },
              {
                  "answer": "Agra",
                  "category": 3,
                  "difficulty": 2,
                  "id": 15,
                  "question": "The Taj Mahal is located in which Indian city?"
              },
              {
                  "answer": "Escher",
                  "category": 2,
                  "difficulty": 1,
                  "id": 16,
                  "question": "Which Dutch graphic artist\u2013initials M C was a creator of optical illusions?"
              },
              {
                  "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": "Scarab",
                  "category": 4,
                  "difficulty": 4,
                  "id": 23,
                  "question": "Which dung beetle was worshipped by the ancient Egyptians?"
              },
              {
                  "answer": "Michigan",
                  "category": 3,
                  "difficulty": 3,
                  "id": 173,
                  "question": "Which US state contains an area known as the Upper Penninsula?"
              }
          ],
          "success": true,
          "total_questions": 18
      }
    

GET /categories/<int:id>/questions

  • General:

    • Gets questions by category id using url parameters.
    • Returns JSON object with paginated matching questions.
  • Sample: curl https://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": 18
      }
    

POST /quizzes

  • General:

    • Allows users to play the quiz game.
    • Uses JSON request parameters of category and previous questions.
    • Returns JSON object with random question not among previous questions.
  • Sample: curl https://127.0.0.1:5000/quizzes -X POST -H "Content-Type: application/json" -d '{"previous_questions": [20, 21], "quiz_category": {"type": "Science", "id": "1"}}'

      {
          "question": {
              "answer": "Blood",
              "category": 1,
              "difficulty": 4,
              "id": 22,
              "question": "Hematology is a branch of medicine involving the study of what?"
          },
          "success": true
      }
    

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published