Skip to content

Commit

Permalink
Merge pull request #129 from TebogoYungMercykay/development
Browse files Browse the repository at this point in the history
Returning the Replies for a Single Post Query
  • Loading branch information
TebogoYungMercykay authored Jan 27, 2024
2 parents 46deef8 + efa3e53 commit 4f3e888
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
9 changes: 6 additions & 3 deletions app/routers/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ def create_post(post: schemas.PostCreate, db: Session = Depends(get_db), current
@router.post("/{id}", response_model=schemas.JSONPostOut)
def get_post(id: int, db: Session = Depends(get_db), current_user: int = Depends(oauth2.get_current_user)):

post = db.query(models.Post, func.count(models.Vote.post_id).label("votes")).join(
post, votes = db.query(models.Post, func.count(models.Vote.post_id).label("votes")).join(
models.Vote, models.Vote.post_id == models.Post.id, isouter=True).group_by(models.Post.id).filter(models.Post.id == id).first()


replies = db.query(models.Reply).filter(models.Reply.post_id == id).all()

if not post:
error_response = {
"status": "error",
Expand All @@ -87,7 +89,8 @@ def get_post(id: int, db: Session = Depends(get_db), current_user: int = Depends
}
return JSONResponse(content=error_response, status_code=404)

return schemas.JSONPostOut(status="success", id=current_user.id, data=post)
return schemas.JSONPostOut(status="success", id=current_user.id,
data=schemas.PostOut(Post=post, votes=votes, replies=replies))


@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT)
Expand Down
1 change: 1 addition & 0 deletions app/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class Config:
class PostOut(BaseModel):
Post: Post
votes: int
replies: List[RepliesOut]

class Config:
from_attributes = True
Expand Down

0 comments on commit 4f3e888

Please sign in to comment.