Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Mt-847 Add Tags #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": []}
Expand Down Expand Up @@ -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)
19 changes: 19 additions & 0 deletions posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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