Monteeq CI commited on
Commit
b4d24b5
·
1 Parent(s): 2893cc2

🚀 Deploy from GitHub @ 976ca1a5dff0b5ad549295e7bba9c856bab094ea

Browse files

Triggered by: Smasduq
Message: Merge pull request #8 from Monteeq/feat/nextjs-app-router-migration

Feat/nextjs app router migration

Files changed (1) hide show
  1. app/api/v1/endpoints/posts.py +35 -0
app/api/v1/endpoints/posts.py CHANGED
@@ -73,6 +73,41 @@ def get_posts(
73
  db.commit()
74
  return posts
75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  @router.post("/create", response_model=schemas.Post)
77
  async def create_post(
78
  content: str = Form(...),
 
73
  db.commit()
74
  return posts
75
 
76
+ @router.get("/{post_id}", response_model=schemas.Post)
77
+ def get_post(
78
+ post_id: int,
79
+ db: Session = Depends(get_db),
80
+ current_user: Optional[dict] = Depends(get_current_user_optional)
81
+ ):
82
+ """Single post for SEO / deep links (used by Next.js /post/[id])."""
83
+ post = db.query(Post).filter(Post.id == post_id).first()
84
+ if not post:
85
+ raise HTTPException(status_code=404, detail="Post not found")
86
+
87
+ target = post
88
+ if post.original_post_id and post.original_post:
89
+ target = post.original_post
90
+
91
+ target.likes_count = db.query(func.count(Like.id)).filter(Like.post_id == target.id).scalar() or 0
92
+ target.comments_count = db.query(func.count(Comment.id)).filter(Comment.post_id == target.id).scalar() or 0
93
+ if current_user:
94
+ target.liked_by_user = (
95
+ db.query(Like)
96
+ .filter(Like.post_id == target.id, Like.user_id == current_user.id)
97
+ .first()
98
+ is not None
99
+ )
100
+ else:
101
+ target.liked_by_user = False
102
+
103
+ # Attribute counts onto the returned row when it is a repost wrapper
104
+ if post.original_post_id and post.original_post:
105
+ post.likes_count = target.likes_count
106
+ post.comments_count = target.comments_count
107
+ post.liked_by_user = target.liked_by_user
108
+
109
+ return post
110
+
111
  @router.post("/create", response_model=schemas.Post)
112
  async def create_post(
113
  content: str = Form(...),