From d1c531a4d94809cfe92ac5dc4f374c636bd05cc1 Mon Sep 17 00:00:00 2001 From: Jose Salvatierra Date: Wed, 1 Jun 2022 14:51:09 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20(Many-to-many)=20Add=20missing=20sc?= =?UTF-8?q?hema=20and=20improve=20legibility?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Grayed out endpoints that had been implemented in previous lectures. --- .../03_many_to_many_relationships/README.md | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/docs/docs/07_sqlalchemy_many_to_many/03_many_to_many_relationships/README.md b/docs/docs/07_sqlalchemy_many_to_many/03_many_to_many_relationships/README.md index dadf2651..7abd7cf9 100644 --- a/docs/docs/07_sqlalchemy_many_to_many/03_many_to_many_relationships/README.md +++ b/docs/docs/07_sqlalchemy_many_to_many/03_many_to_many_relationships/README.md @@ -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 @@ -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 | +| ---------------------------------------------- | --------------------------------------------------------- | -------------------------------------------------------------------------------------- | +| ✅ `GET` | `/stores/{id}/tags` | Get a list of tags in a store. | +| ✅ `POST` | `/stores/{id}/tags` | Create a new tag. | +| ✅ `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. | +| ✅ `GET` | `/tags/{id}` | Get information about a tag given its unique id. | +| ✅ `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 @@ -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 -``` \ No newline at end of file +``` + +And with that, we're done! + +Now we're ready to look at securing API endpoints with user authentication. \ No newline at end of file