Skip to content
This repository was archived by the owner on Oct 24, 2024. It is now read-only.

Commit 171f90b

Browse files
committed
feat(postlist): Filter items by selected tag
1 parent 6bab98d commit 171f90b

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/routes/posts.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ async def post_list(
2121
order: OrderChoices = OrderChoices.ascending,
2222
page: int = Query(1, gt=0),
2323
page_items: int = Query(2, gt=0),
24+
tag: str | None = Query(None),
2425
hx_request: str | None = Header(None)
2526
):
2627
posts = []
@@ -46,6 +47,11 @@ def getPost(post_id: int):
4647
except ValueError:
4748
pass
4849

50+
if tag in taglist:
51+
posts = [post for post in posts if post.get("tags") and tag in post["tags"]]
52+
elif tag is not None:
53+
raise HTTPException(status_code=400, detail=f"The tag [{tag}] does not exist.")
54+
4955
posts = sortPosts(posts, sort, order)
5056

5157
# Simple pagination
@@ -59,14 +65,20 @@ def getPost(post_id: int):
5965
# next and previous pages
6066
pagination = {}
6167
if page > 1:
62-
pagination["prev"] = f"/posts?sort={sort.value}&order={order.value}&page={page-1}&page_items={page_items}"
68+
pagination["prev"] = (
69+
f"/posts?sort={sort.value}&order={order.value}"
70+
f"&page={page-1}&page_items={page_items}&tag={tag}"
71+
)
6372
else:
6473
pagination["prev"] = None
6574

6675
if end_index >= len(posts):
6776
pagination["next"] = None
6877
else:
69-
pagination["next"] = f"/posts?sort={sort.value}&order={order.value}&page={page+1}&page_items={page_items}"
78+
pagination["next"] = (
79+
f"/posts?sort={sort.value}&order={order.value}"
80+
f"&page={page+1}&page_items={page_items}&tag={tag}"
81+
)
7082

7183
# Total pages
7284
if len(posts) <= page_items:

0 commit comments

Comments
 (0)