Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
jslvtr merged 8 commits intotecladocode:developfromLUS24:develop
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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.

</TabItem>
</Tabs>
</div>
Expand Down
83 changes: 40 additions & 43 deletionsproject/01-first-rest-api/app.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsproject/02-first-rest-api-docker/app.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp