Skip to content

Commit

Permalink
edited blogs/views.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Anupamaraie committed Nov 4, 2023
1 parent e5302da commit 37a8490
Showing 1 changed file with 0 additions and 27 deletions.
27 changes: 0 additions & 27 deletions blogs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,48 +209,23 @@ def get(self, request: Request, id) -> Response:
serializer = self.serializer_class(comment)
return cr.success(data=serializer.data, message="Comment fetched successfully!")


# class LikeCreateView(APIView):

# permission_classes = [IsAuthenticated]

# def post(self, request: Request, post_id) -> Response:

# user = request.user
# post = Likes.objects.filter(post_id=post_id, user=user).filter()
# current_likes = post.user.count()
# if not post:
# post.user.add()


# existing_like = Likes.objects.filter(post_id=post_id, user=request.user).first()
# post = existing_like.post
# user = existing_like.user
# if not existing_like:
# serializer.save(user = user, post = post)
# return cr.success(data=serializer.data, message="Succefully liked a post.")


class LikeCreateView(APIView):

permission_classes = [IsAuthenticated]

def post(self, request, post_id):

post = Posts.objects.filter(post_id=post_id).first()
user = request.user

if not post:
return cr.error(message="Post not found.")

like_exists = Likes.objects.filter(user=user, post=post).exists()

if like_exists:
return cr.success(message='You have already liked this post.')

like = Likes(user=user, post=post)
like.save()

return cr.success(message='Post liked successfully.')

def delete(self, request, post_id):
Expand All @@ -261,10 +236,8 @@ def delete(self, request, post_id):
return cr.error(message="Post not found.")

like = Likes.objects.filter(user=user, post=post).first()

if not like:
return cr.success(message='You have not liked this post.')

like.delete()

return cr.success(message='Like removed successfully.')

0 comments on commit 37a8490

Please sign in to comment.