Skip to content

Commit

Permalink
✨Feat : 게시물 좋아요 기능 추가 I-deul-of-zoo#9
Browse files Browse the repository at this point in the history
  • Loading branch information
sysgaeng committed Oct 30, 2023
1 parent 0407ce6 commit d959441
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
4 changes: 2 additions & 2 deletions posts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
urlpatterns =[
path("", views.SearchPostsList.as_view()),
path('<int:pk>/', views.PostsDetail.as_view()),
]

path('<int:pk>/like/', views.LikeView.as_view()), # post/1/like
]
50 changes: 46 additions & 4 deletions posts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
from rest_framework.permissions import AllowAny, IsAuthenticated

from posts.models import Posts, HashTags
from posts.serializers import PostSerializer
from posts.serializers import PostSerializer, LikeSerializer
import requests


# Blog의 detail을 보여주는 역할
class PostsDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Posts.objects.all()
serializer_class = PostSerializer

# permission_classes = [IsAuthenticated, ]
permission_classes = [AllowAny, ]

def get(self, request, *args, **kwargs):
Expand All @@ -48,8 +49,7 @@ class PostsPagination(PageNumberPagination):


class SearchPostsList(ListAPIView):
# permission_classes = [IsAuthenticated, ]
permission_classes = [AllowAny, ]
permission_classes = [AllowAny]

serializer_class = PostSerializer
pagination_class = PostsPagination
Expand Down Expand Up @@ -88,3 +88,45 @@ def get_queryset(self):
# raise ValueError(f'쿼리 파라미터 "search"의 값이 필요합니다.')

return queryset


class LikeView(APIView):
permission_classes = [AllowAny]

def post(self, request, pk):
post = Posts.objects.get(id=pk)
sns = post.type
content_id = post.content_id

if sns == 'facebook':
api_url = f'https://www.facebook.com/likes/{content_id}'
elif sns == 'twitter':
api_url = f'https://www.twitter.com/likes/{content_id}'
elif sns == 'instagram':
api_url = f'https://www.instagram.com/likes/{content_id}'
elif sns == 'threads':
api_url = f'https://www.threads.com/likes/{content_id}'
else:
raise

post.like_count += 1
post.save()

response = requests.get(api_url)

IS_LOCAL = True
if response.status_code == 200 or IS_LOCAL:
return Response(
{
'message': f'{sns} 게시글에 좋아요 개수가 올라갔습니다.',
'like_count': post.like_count,
},
status=status.HTTP_200_OK
)
else:
return Response(
{
'message': 'API 요청 실패'
},
status=status.HTTP_400_BAD_REQUEST,
)

0 comments on commit d959441

Please sign in to comment.