diff --git a/app/presentation/templates/pages/index.html b/app/presentation/templates/pages/index.html index c63b86b..6191e41 100644 --- a/app/presentation/templates/pages/index.html +++ b/app/presentation/templates/pages/index.html @@ -52,6 +52,9 @@ {{ post.created_at.strftime('%B %d, %Y') }} + + 👍 {{ post.like_count }} +
diff --git a/app/presentation/templates/pages/post_detail.html b/app/presentation/templates/pages/post_detail.html index bac133a..042d9c2 100644 --- a/app/presentation/templates/pages/post_detail.html +++ b/app/presentation/templates/pages/post_detail.html @@ -33,6 +33,13 @@ {% else %} {{ _('post.status_draft', current_locale) }} {% endif %} + + +
@@ -83,3 +90,42 @@ {% endblock %} + +{% block extra_js %} + +{% endblock %} diff --git a/app/presentation/web/routes.py b/app/presentation/web/routes.py index 83d457e..5dfd8e0 100644 --- a/app/presentation/web/routes.py +++ b/app/presentation/web/routes.py @@ -24,6 +24,7 @@ from app.application.use_cases import ( GetPostUseCase, ListPostsUseCase, PublishPostUseCase, + TogglePostLikeUseCase, UpdatePostUseCase, ) from app.domain.exceptions import ( @@ -523,6 +524,39 @@ async def delete_post( return RedirectResponse(url="/web/", status_code=303) +@router.post("/posts/{post_slug}/like") +async def toggle_like_web( + post_slug: str, + user: OptionalUserDep, + get_use_case: FromDishka[GetPostUseCase], + toggle_use_case: FromDishka[TogglePostLikeUseCase], +) -> dict[str, object]: + """Toggle like on a post via web UI. + + Args: + post_slug: The URL-friendly slug of the post. + user: Current user from cookie or None. + get_use_case: Use case for retrieving posts. + toggle_use_case: Use case for toggling likes. + + Returns: + JSON dict with updated like_count. + + Raises: + HTTPException: If post not found or user not authenticated. + """ + if not user: + raise HTTPException(status_code=401, detail="Authentication required") + + try: + post = await get_use_case.by_slug(post_slug) + except NotFoundException: + raise HTTPException(status_code=404, detail="Post not found") from None + + result = await toggle_use_case.execute(post.id, user.user_id) + return {"like_count": result.like_count} + + @router.get("/profile", response_class=HTMLResponse) async def profile( request: Request,