From f26a552e2586824a616b65e4b8ded0f89c916012 Mon Sep 17 00:00:00 2001 From: Benjamin Mollier Date: Tue, 28 Jun 2022 13:09:47 +0200 Subject: [PATCH 1/3] feat: add tags --- db.py | 1 + 1 file changed, 1 insertion(+) diff --git a/db.py b/db.py index c6a3808..90432bf 100644 --- a/db.py +++ b/db.py @@ -26,6 +26,7 @@ class Post(BaseModel): updated_at: datetime = Field(default_factory=datetime.now) body: str author: str + tags: list[str] = Field([]) in_memory_db: dict[str, list[Post]] = {"posts": []} From 8e1f34560eb1adbae6cf678910c8de238f938b4d Mon Sep 17 00:00:00 2001 From: Benjamin Mollier Date: Tue, 28 Jun 2022 13:10:25 +0200 Subject: [PATCH 2/3] add automatic tags when creating post and edit tags endpoint --- posts.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/posts.py b/posts.py index df0428a..e428865 100644 --- a/posts.py +++ b/posts.py @@ -30,4 +30,23 @@ def get_by_id(post_id: int): @router.post("/posts") def create_new_post(post_create: PostCreateSchema): post = Post(**post_create.dict()) + + if "python" in post.body or "Python" in post.body: + post.tags.append("python") + if "vue" in post.body or "Vue" in post.body: + post.tags.append("vue") + if "nodejs" in post.body or "NodeJS" in post.body: + post.tags.append("nodejs") + if "php" in post.body or "PHP" in post.body: + post.tags.append("php") + return save_post(post) + + +@router.post("/posts/{postId}/edit-tags") +def set_tags_on_post(postId: str, newTags: list[str]): + post = get_post_by_id(postId) + + post.tags = newTags + post = save_post(post) + return post From c784af01397144b06efc4d529d01af3ae5a2e7e6 Mon Sep 17 00:00:00 2001 From: Benjamin Mollier Date: Tue, 28 Jun 2022 13:13:48 +0200 Subject: [PATCH 3/3] fix runtime error due to string and int comparison --- db.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db.py b/db.py index 90432bf..b6aece5 100644 --- a/db.py +++ b/db.py @@ -56,4 +56,4 @@ def get_post_by_id(post_id: int): :return: the post with given id model or None if no post with given id was found """ - return next((post for post in in_memory_db["posts"] if post.id == post_id), None) + return next((post for post in in_memory_db["posts"] if str(post.id) == post_id), None)