Skip to content

Commit

Permalink
Endpoints for get store by name and get items in store by name
Browse files Browse the repository at this point in the history
  • Loading branch information
HarrisonWelch committed Sep 25, 2023
1 parent e04c523 commit ce1c5fc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/60_test_get_store_by_name.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# How to get a specific store and its items

Picking up from last lecture

Make endpoints for getting store by name and getting items in the store by name.

```py
@app.get("/store/<string:name>")
def get_store(name):
for store in stores:
if store["name"] == name:
return store
return {"message": "Store not found"}, 404


@app.get("/store/<string:name>/item")
def get_item_in_store(name):
for store in stores:
if store["name"] == name:
return {"items": store["items"]} # Dict for extensibility later, more keys doesn't hurt anything unlike changing data type
return {"message": "Store not found"}, 404
```

Test get store by name:

![60_test_get_store_by_name image](https://github.com/HarrisonWelch/REST-APIs-with-Flask-and-Python-in-2023-Notes/blob/master/Screenshots/60_test_get_store_by_name.png)

Test get store items by name:

![60_test_get_items_in_store_by_name image](https://github.com/HarrisonWelch/REST-APIs-with-Flask-and-Python-in-2023-Notes/blob/master/Screenshots/60_test_get_items_in_store_by_name.png)

Automated tests on everything we run. GitHub has some auto-tests for view and using.
16 changes: 16 additions & 0 deletions Section_3_Your_first_REST_API/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,19 @@ def create_item(name):
store["items"].append(new_item)
return new_item, 201
return {"message": "Store not found"}, 404


@app.get("/store/<string:name>")
def get_store(name):
for store in stores:
if store["name"] == name:
return store
return {"message": "Store not found"}, 404


@app.get("/store/<string:name>/item")
def get_item_in_store(name):
for store in stores:
if store["name"] == name:
return {"items": store["items"]} # Dict for extensibility later, more keys doesn't hurt anything unlike changing data type
return {"message": "Store not found"}, 404

0 comments on commit ce1c5fc

Please sign in to comment.