Skip to content

Commit

Permalink
bookmark fix try2
Browse files Browse the repository at this point in the history
  • Loading branch information
oktayozel committed Nov 24, 2024
1 parent d2cc48a commit c81d7cc
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 2 additions & 1 deletion backend/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class Post(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
like_count = models.IntegerField(default=0)
bookmarked_by = models.ManyToManyField(User, related_name='bookmarked_posts', blank=True)

liked_by = models.ManyToManyField(User, related_name='liked_posts', blank=True)

def __str__(self):
Expand Down Expand Up @@ -198,7 +199,7 @@ def __str__(self):

class Bookmark(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='bookmarks')
post = models.ForeignKey(Post, on_delete=models.CASCADE) # No related_name needed here
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='bookmarked_by')
created_at = models.DateTimeField(auto_now_add=True)

class Meta:
Expand Down
4 changes: 3 additions & 1 deletion backend/app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ class Meta:
def get_posts(self, obj):
"""Get posts created by the user, similar to get_post_details."""
posts = Post.objects.filter(author=obj.user)
user = self.context['request'].user

post_data = []
for post in posts:
is_liked = post.liked_by.filter(id=self.context['request'].user.id).exists()
is_bookmarked = post.bookmarked_by.filter(id=self.context['request'].user.id).exists()
is_bookmarked = Bookmark.objects.filter(user=user, post=post).exists()

comments_data = [
{
Expand Down

0 comments on commit c81d7cc

Please sign in to comment.