Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cou-148-rest-fix-uuid-imports-in-projects-and-ebook #88

Merged
merged 8 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ def create_store():
<TabItem value="new" label="create_store (new)">

```py title="app.py"
import uuid

@app.post("/store")
def create_store():
store_data = request.get_json()
Expand All @@ -255,6 +257,8 @@ def create_store():
return store
```

Here we add a new import, [the `uuid` module](https://docs.python.org/3/library/uuid.html). We will be using it to create unique IDs for our stores and items instead of relying on the uniqueness of their `names`.
jslvtr marked this conversation as resolved.
Show resolved Hide resolved

</TabItem>
</Tabs>
</div>
Expand Down
83 changes: 40 additions & 43 deletions project/01-first-rest-api/app.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,54 @@
import uuid
from flask import Flask, request

app = Flask(__name__)

stores = {}
items = {}


@app.get("/item/<string:id>")
def get_item(id):
try:
return items[id]
except KeyError:
return {"message": "Item not found"}, 404


@app.post("/item")
def create_item():
request_data = request.get_json()
new_item_id = uuid.uuid4().hex
new_item = {
"name": request_data["name"],
"price": request_data["price"],
"store_id": request_data["store_id"],
stores = [
{
"name": "My Store",
"items": [
{
"name": "Chair",
"price": 15.99
}
]
}
items[new_item_id] = new_item
return new_item


@app.get("/item")
def get_all_items():
return {"items": list(items.values())}
]


@app.get("/store/<string:id>")
def get_store(id):
try:
# Here you might also want to add the items in this store
# We'll do that later on in the course
return stores[id]
except KeyError:
return {"message": "Store not found"}, 404
@app.get("/store")
def get_stores():
return {"stores": stores}


@app.post("/store")
def create_store():
request_data = request.get_json()
new_store_id = uuid.uuid4().hex
new_store = {"id": new_store_id, "name": request_data["name"]}
stores[new_store_id] = new_store
new_store = {"name": request_data["name"], "items": []}
stores.append(new_store)
return new_store, 201


@app.get("/store")
def get_stores():
return {"stores": list(stores.values())}
@app.post("/store/<string:name>/item")
def create_item(name):
request_data = request.get_json()
for store in stores:
if store["name"] == name:
new_item = {"name": request_data["name"], "price": request_data["price"]}
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"]}
return {"message": "Store not found"}, 404
83 changes: 40 additions & 43 deletions project/02-first-rest-api-docker/app.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,54 @@
import uuid
from flask import Flask, request

app = Flask(__name__)

stores = {}
items = {}


@app.get("/item/<string:id>")
def get_item(id):
try:
return items[id]
except KeyError:
return {"message": "Item not found"}, 404


@app.post("/item")
def create_item():
request_data = request.get_json()
new_item_id = uuid.uuid4().hex
new_item = {
"name": request_data["name"],
"price": request_data["price"],
"store_id": request_data["store_id"],
stores = [
{
"name": "My Store",
"items": [
{
"name": "Chair",
"price": 15.99
}
]
}
items[new_item_id] = new_item
return new_item


@app.get("/item")
def get_all_items():
return {"items": list(items.values())}
]


@app.get("/store/<string:id>")
def get_store(id):
try:
# Here you might also want to add the items in this store
# We'll do that later on in the course
return stores[id]
except KeyError:
return {"message": "Store not found"}, 404
@app.get("/store")
def get_stores():
return {"stores": stores}


@app.post("/store")
def create_store():
request_data = request.get_json()
new_store_id = uuid.uuid4().hex
new_store = {"id": new_store_id, "name": request_data["name"]}
stores[new_store_id] = new_store
new_store = {"name": request_data["name"], "items": []}
stores.append(new_store)
return new_store, 201


@app.get("/store")
def get_stores():
return {"stores": list(stores.values())}
@app.post("/store/<string:name>/item")
def create_item(name):
request_data = request.get_json()
for store in stores:
if store["name"] == name:
new_item = {"name": request_data["name"], "price": request_data["price"]}
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"]}
return {"message": "Store not found"}, 404