Skip to content

Commit d1c531a

Browse files
committed
✨ (Many-to-many) Add missing schema and improve legibility
Grayed out endpoints that had been implemented in previous lectures.
1 parent aa299a2 commit d1c531a

File tree

1 file changed

+25
-1
lines changed
  • docs/docs/07_sqlalchemy_many_to_many/03_many_to_many_relationships

1 file changed

+25
-1
lines changed

docs/docs/07_sqlalchemy_many_to_many/03_many_to_many_relationships/README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ Next up, let's add the nested fields to the marshmallow schemas.
119119
The `TagAndItemSchema` will be used to return information about both the Item and Tag that have been modified in an endpoint, together with an informative message.
120120

121121
```python title="schemas.py"
122+
class ItemSchema(PlainItemSchema):
123+
store_id = fields.Int(required=True, load_only=True)
124+
store = fields.Nested(PlainStoreSchema(), dump_only=True)
125+
# highlight-start
126+
tags = fields.List(fields.Nested(PlainTagSchema()), dump_only=True)
127+
# highlight-end
128+
122129
class TagSchema(PlainTagSchema):
123130
store_id = fields.Int(load_only=True)
124131
# highlight-start
@@ -136,6 +143,19 @@ class TagAndItemSchema(Schema):
136143

137144
## The API endpoints
138145

146+
Now let's add the rest of our API endpoints (grayed out are the ones we implemented in [one-to-many relationships review](../one_to_many_review/))!
147+
148+
| Method | Endpoint | Description |
149+
| ---------------------------------------------- | --------------------------------------------------------- | -------------------------------------------------------------------------------------- |
150+
| <span style={{opacity: "50%"}}>✅ `GET`</span> | <span style={{opacity: "50%"}}>`/stores/{id}/tags`</span> | <span style={{opacity: "50%"}}>Get a list of tags in a store.</span> |
151+
| <span style={{opacity: "50%"}}>✅ `POST`</span> | <span style={{opacity: "50%"}}>`/stores/{id}/tags`</span> | <span style={{opacity: "50%"}}>Create a new tag.</span> |
152+
|`POST` | `/items/{id}/tags/{id}` | Link an item in a store with a tag from the same store. |
153+
|`DELETE` | `/items/{id}/tags/{id}` | Unlink a tag from an item. |
154+
| <span style={{opacity: "50%"}}>✅ `GET`</span> | <span style={{opacity: "50%"}}>`/tags/{id}`</span> | <span style={{opacity: "50%"}}>Get information about a tag given its unique id.</span> |
155+
|`DELETE` | `/tags/{id}` | Delete a tag, which must have no associated items. |
156+
157+
Here's the code (new lines highlighted):
158+
139159
```python title="resources/tag.py"
140160
from flask.views import MethodView
141161
from flask_smorest import Blueprint, abort
@@ -242,4 +262,8 @@ class Tag(MethodView):
242262
message="Could not delete tag. Make sure tag is not associated with any items, then try again.",
243263
)
244264
# highlight-end
245-
```
265+
```
266+
267+
And with that, we're done!
268+
269+
Now we're ready to look at securing API endpoints with user authentication.

0 commit comments

Comments
 (0)