diff --git a/db.py b/db.py index c6a3808..b6aece5 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": []} @@ -55,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) 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