Added posts likes and dislikes

This commit is contained in:
ITQ
2024-03-04 18:02:03 +03:00
parent fbf851e1a6
commit 9a4b18b4c1
3 changed files with 47 additions and 0 deletions
+36
View File
@@ -82,3 +82,39 @@ class UserFeedListApiView(ListAPIView):
offset = serializer.validated_data.get("offset")
return user.posts.all()[offset: offset + limit]
class LikePostApiView(APIView):
permission_classes = [IsAuthenticated]
def post(self, request, post_id):
try:
post = Post.objects.get(id=post_id)
self.check_object_permissions(request, post)
request.user.like_post(post)
return Response(
{"status": "ok"},
status=status.HTTP_200_OK,
)
except Post.DoesNotExist:
raise NotFound(
{"detail": "Post not found."},
) from None
class DislikePostApiView(APIView):
permission_classes = [IsAuthenticated]
def post(self, request, post_id):
try:
post = Post.objects.get(id=post_id)
self.check_object_permissions(request, post)
request.user.dislike_post(post)
return Response(
{"status": "ok"},
status=status.HTTP_200_OK,
)
except Post.DoesNotExist:
raise NotFound(
{"detail": "Post not found."},
) from None