Building a GET API Lab (CodeGrade)

Learning Goals


Key Vocab


Instructions

This is a test-driven lab.

Run pipenv install to install the dependencies and pipenv shell to enter your virtual environment before running your code.

$ pipenv install
$ pipenv shell

Change into the server directory and configure the FLASK_APP and FLASK_RUN_PORT environment variables:

$ cd server
$ export FLASK_APP=app.py
$ export FLASK_RUN_PORT=5555

In this application, we'll be working on a JSON API to get a list of bakeries and their baked goods. We have two models, bakeries and baked goods, that have a one-to-many relationship. Here's what the ERD for these tables looks like:

Bakeries ERD

The commands flask db init and flask db migrate have already been run. Run the following command to initialize the database from the existing migration script:

$ flask db upgrade head

Run the following command to seed the table with sample data:

$ python seed.py

You can run the app and explore your API in the browser by using Flask command:

$ flask run

Edit app.py to handle the following GET requests:

Once all of your tests are passing, commit and push your work using git to submit.

Examples

[
  {
    "baked_goods": [
      {
        "bakery_id": 1,
        "created_at": "2023-11-03 16:11:18",
        "id": 1,
        "name": "Chocolate dipped donut",
        "price": 2.75,
        "updated_at": null
      },
      {
        "bakery_id": 1,
        "created_at": "2023-11-03 16:11:18",
        "id": 2,
        "name": "Apple-spice filled donut",
        "price": 3.5,
        "updated_at": null
      }
    ],
    "created_at": "2023-11-03 16:11:18",
    "id": 1,
    "name": "Delightful donuts",
    "updated_at": null
  },
  {
    "baked_goods": [
      {
        "bakery_id": 2,
        "created_at": "2023-11-03 16:11:18",
        "id": 3,
        "name": "Glazed honey cruller",
        "price": 3.25,
        "updated_at": null
      },
      {
        "bakery_id": 2,
        "created_at": "2023-11-03 16:11:18",
        "id": 4,
        "name": "Chocolate cruller",
        "price": 100,
        "updated_at": "2023-11-03 16:11:18"
      }
    ],
    "created_at": "2023-11-03 16:11:18",
    "id": 2,
    "name": "Incredible crullers",
    "updated_at": null
  }
]
{
  "baked_goods": [
    {
      "bakery_id": 1,
      "created_at": "2023-11-03 16:11:18",
      "id": 1,
      "name": "Chocolate dipped donut",
      "price": 2.75,
      "updated_at": null
    },
    {
      "bakery_id": 1,
      "created_at": "2023-11-03 16:11:18",
      "id": 2,
      "name": "Apple-spice filled donut",
      "price": 3.5,
      "updated_at": null
    }
  ],
  "created_at": "2023-11-03 16:11:18",
  "id": 1,
  "name": "Delightful donuts",
  "updated_at": null
}
[
  {
    "bakery": {
      "created_at": "2023-11-03 16:31:32",
      "id": 1,
      "name": "Delightful donuts",
      "updated_at": null
    },
    "bakery_id": 1,
    "created_at": "2023-11-03 16:31:32",
    "id": 2,
    "name": "Apple-spice filled donut",
    "price": 3.5,
    "updated_at": null
  },
  {
    "bakery": {
      "created_at": "2023-11-03 16:31:32",
      "id": 2,
      "name": "Incredible crullers",
      "updated_at": null
    },
    "bakery_id": 2,
    "created_at": "2023-11-03 16:31:32",
    "id": 4,
    "name": "Chocolate cruller",
    "price": 3.4,
    "updated_at": null
  },
  {
    "bakery": {
      "created_at": "2023-11-03 16:31:32",
      "id": 2,
      "name": "Incredible crullers",
      "updated_at": null
    },
    "bakery_id": 2,
    "created_at": "2023-11-03 16:31:32",
    "id": 3,
    "name": "Glazed honey cruller",
    "price": 3.25,
    "updated_at": null
  },
  {
    "bakery": {
      "created_at": "2023-11-03 16:31:32",
      "id": 1,
      "name": "Delightful donuts",
      "updated_at": null
    },
    "bakery_id": 1,
    "created_at": "2023-11-03 16:31:32",
    "id": 1,
    "name": "Chocolate dipped donut",
    "price": 2.75,
    "updated_at": null
  }
]
{
  "bakery": {
    "created_at": "2023-11-03 16:16:21",
    "id": 1,
    "name": "Delightful donuts",
    "updated_at": null
  },
  "bakery_id": 1,
  "created_at": "2023-11-03 16:16:21",
  "id": 2,
  "name": "Apple-spice filled donut",
  "price": 3.5,
  "updated_at": null
}

Resources

Powered by Forestry.md