Skip to content

Commit

Permalink
✨ (Many-to-many) Add missing schema and improve legibility
Browse files Browse the repository at this point in the history
Grayed out endpoints that had been implemented in previous lectures.
  • Loading branch information
jslvtr committed Jun 1, 2022
1 parent aa299a2 commit d1c531a
Showing 1 changed file with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ Next up, let's add the nested fields to the marshmallow schemas.
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.

```python title="schemas.py"
class ItemSchema(PlainItemSchema):
store_id = fields.Int(required=True, load_only=True)
store = fields.Nested(PlainStoreSchema(), dump_only=True)
# highlight-start
tags = fields.List(fields.Nested(PlainTagSchema()), dump_only=True)
# highlight-end

class TagSchema(PlainTagSchema):
store_id = fields.Int(load_only=True)
# highlight-start
Expand All @@ -136,6 +143,19 @@ class TagAndItemSchema(Schema):

## The API endpoints

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/))!

| Method | Endpoint | Description |
| ---------------------------------------------- | --------------------------------------------------------- | -------------------------------------------------------------------------------------- |
| <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> |
| <span style={{opacity: "50%"}}>✅ `POST`</span> | <span style={{opacity: "50%"}}>`/stores/{id}/tags`</span> | <span style={{opacity: "50%"}}>Create a new tag.</span> |
|`POST` | `/items/{id}/tags/{id}` | Link an item in a store with a tag from the same store. |
|`DELETE` | `/items/{id}/tags/{id}` | Unlink a tag from an item. |
| <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> |
|`DELETE` | `/tags/{id}` | Delete a tag, which must have no associated items. |

Here's the code (new lines highlighted):

```python title="resources/tag.py"
from flask.views import MethodView
from flask_smorest import Blueprint, abort
Expand Down Expand Up @@ -242,4 +262,8 @@ class Tag(MethodView):
message="Could not delete tag. Make sure tag is not associated with any items, then try again.",
)
# highlight-end
```
```

And with that, we're done!

Now we're ready to look at securing API endpoints with user authentication.

0 comments on commit d1c531a

Please sign in to comment.