-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/issue-009' of https://github.com/Django-Wanted-…
…Internship-3-Team/repo1_feed-service into feature/issue-009
- Loading branch information
Showing
6 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from rest_framework.serializers import ModelSerializer | ||
|
||
from posts.models import Post | ||
|
||
|
||
class PostLikeIncrementSerializer(ModelSerializer): | ||
class Meta: | ||
model = Post | ||
fields = ("like_count",) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,49 @@ | ||
# Create your tests here. | ||
from django.urls import reverse | ||
from rest_framework.test import APIClient, APITestCase | ||
|
||
from posts.models import Post | ||
from users.models import User | ||
|
||
|
||
class LikeAPITestCase(APITestCase): | ||
client = APIClient(enforce_csrf_checks=True) | ||
viewname = "likes" | ||
|
||
def setUp(self): | ||
self.user = User.objects.create(email="user") | ||
|
||
self.post = Post.objects.create(title="title", post_type="facebook", content="content") | ||
|
||
def test_post_without_auth(self): | ||
"""logout and post like""" | ||
|
||
self.client.logout() | ||
|
||
response = self.client.post( | ||
path=reverse( | ||
viewname=self.viewname, | ||
kwargs={ | ||
"content_id": self.post.content_id, | ||
}, | ||
), | ||
) | ||
|
||
self.assertEqual(response.status_code, 401) | ||
|
||
def test_post_with_auth(self): | ||
"""login and post like""" | ||
|
||
# self.client.force_login(self.user) | ||
self.client.force_authenticate(user=self.user) | ||
|
||
response = self.client.post( | ||
path=reverse( | ||
viewname=self.viewname, | ||
kwargs={ | ||
"content_id": self.post.content_id, | ||
}, | ||
), | ||
) | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(self.post.like_count, Post.objects.first().like_count - 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.urls import path | ||
|
||
from likes import views | ||
|
||
urlpatterns = [ | ||
path("<str:content_id>/", views.LikesAPIView.as_view(), name="likes"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,35 @@ | ||
# Create your views here. | ||
from django.shortcuts import get_object_or_404 | ||
from drf_yasg import openapi | ||
from drf_yasg.utils import swagger_auto_schema | ||
from rest_framework.permissions import IsAuthenticatedOrReadOnly | ||
from rest_framework.response import Response | ||
from rest_framework.status import HTTP_200_OK, HTTP_401_UNAUTHORIZED | ||
from rest_framework.views import APIView | ||
|
||
from likes.serializers import PostLikeIncrementSerializer | ||
from posts.models import Post | ||
|
||
|
||
class LikesAPIView(APIView): | ||
permission_classes = [IsAuthenticatedOrReadOnly] | ||
|
||
@swagger_auto_schema( | ||
operation_summary="게시물에 좋아요", | ||
responses={ | ||
HTTP_200_OK: openapi.Response(description="ok"), | ||
HTTP_401_UNAUTHORIZED: openapi.Response(description="unauthorized"), | ||
}, | ||
) | ||
def post(self, request, content_id): | ||
post = get_object_or_404(Post, content_id=content_id) | ||
|
||
serializer = PostLikeIncrementSerializer( | ||
post, | ||
data={"like_count": post.like_count + 1}, | ||
partial=True, | ||
) | ||
|
||
serializer.is_valid(raise_exception=True) | ||
serializer.save() | ||
|
||
return Response(status=200) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# Generated by Django 4.2.6 on 2023-10-27 23:19 | ||
|
||
|
||
|
||
from django.db import migrations, models | ||
import uuid | ||
|
||
|