Skip to content

Commit

Permalink
Refactor post retrieval to use post_id index only
Browse files Browse the repository at this point in the history
  • Loading branch information
izikdepth committed Oct 29, 2024
1 parent fa1c03d commit df0cef2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class User(Model):

class Post(Model):
__keyspace__ = 'store'
id = columns.UUID(primary_key=True, default=uuid4)
id = columns.UUID(primary_key=True, default=uuid4, index=True)
user_id = columns.UUID(primary_key=True, partition_key=True)
created_at = columns.DateTime(primary_key=True, clustering_order="DESC")
title = columns.Text(required=False)
Expand Down
10 changes: 6 additions & 4 deletions app/post/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,14 @@ def create_post(post: PostCreate):


@post_router.get("/{post_id}", response_model=PostResponse)
def read_post(post_id: str, user_id: str = Query(...), created_at: datetime = Query(...)):
db_post = Post.objects(id=post_id, user_id=user_id, created_at=created_at).first()
def read_post(post_id: str):
# Fetch the post directly by its UUID using the index
db_post = Post.objects.filter(id=post_id).first()
if db_post is None:
raise HTTPException(status_code=404, detail="Post not found")

# Calculate total votes for the post
total_votes = sum(vote.vote_value for vote in Vote.objects(post_id=post_id))
total_votes = sum(vote.vote_value for vote in Vote.objects.filter(post_id=post_id))

return PostResponse(
id=db_post.id,
Expand All @@ -140,6 +141,7 @@ def read_post(post_id: str, user_id: str = Query(...), created_at: datetime = Qu
votes=total_votes
)


@post_router.put("/{post_id}", response_model=PostResponse)
def update_post(post_id: str, post: PostUpdate):
db_post = Post.objects(id=post_id).first()
Expand Down

0 comments on commit df0cef2

Please sign in to comment.