Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

게시물 공유(API) #23

Merged
merged 5 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@
path("api/users/", include("users.urls")),
path("api/posts/", include("posts.urls")),
path("api/common/", include("common.urls")),
# Swagger
path("swagger/docs/", schema_view.with_ui("swagger", cache_timeout=0), name="schema-swagger-ui"),
# Likes
path("api/likes/", include("likes.urls")),
path("api/shares/", include("shares.urls")),
# Swagger
path(
"swagger/docs/",
schema_view.with_ui("swagger", cache_timeout=0),
name="schema-swagger-ui",
),
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Expand Down
9 changes: 9 additions & 0 deletions shares/serializers.py
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 PostShareCountIncrementSerializer(ModelSerializer):
class Meta:
model = Post
fields = ("share_count",)
1 change: 0 additions & 1 deletion shares/tests.py

This file was deleted.

Empty file added shares/tests/__init__.py
Empty file.
55 changes: 55 additions & 0 deletions shares/tests/test_shares_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from django.contrib.auth import get_user_model
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase

from posts.models import Post


class SharesAPITestCase(APITestCase):
"""
/shares/<content_id>/ post 에 대한 테스트 케이스
"""

viewname = "shares"

def test_post_failure_no_api(self):
"""
해당 view가 없으면 NoReverseMatch 예외가 수반됩니다.
"""
reverse(self.viewname, kwargs={"content_id": None})

def test_post_without_auth(self):
"""
인증되지 않은 사용자가 shares에 post 요청을 전달하고,
401(unauth) 응답을 얻습니다.
"""

post = Post.objects.create(title="title")
self.client.logout()
response = self.client.post(
path=reverse(self.viewname, kwargs={"content_id": post.content_id}),
data=None,
)

self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) # status 401

def test_post_with_auth(self):
"""
인증된 사용자가 shares에 post 요청을 전달하고,
200(Ok) 응답을 얻으며 share_count가 증가합니다.
"""

post = Post.objects.create(title="title")

user = get_user_model().objects.create(username="username")
self.client.force_authenticate(user=user)

response = self.client.post(
path=reverse(self.viewname, kwargs={"content_id": post.content_id}),
data=None,
)
self.assertEqual(response.status_code, status.HTTP_200_OK) # status 200

post_after_response = Post.objects.get(content_id=post.content_id)
self.assertEqual(post.share_count + 1, post_after_response.share_count) # share_count diff 1
7 changes: 7 additions & 0 deletions shares/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path

from shares.views import SharesAPIView

urlpatterns = [
path("<str:content_id>/", SharesAPIView.as_view(), name="shares"),
]
43 changes: 42 additions & 1 deletion shares/views.py
Original file line number Diff line number Diff line change
@@ -1 +1,42 @@
# 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 import status
from rest_framework.permissions import IsAuthenticatedOrReadOnly
from rest_framework.response import Response
from rest_framework.views import APIView

from posts.models import Post
from shares.serializers import PostShareCountIncrementSerializer


class SharesAPIView(APIView):
permission_classes = [IsAuthenticatedOrReadOnly]

@swagger_auto_schema(
operation_summary="게시물 좋아요",
responses={
status.HTTP_200_OK: openapi.Response(description="ok"),
status.HTTP_401_UNAUTHORIZED: openapi.Response(description="unauthorized"),
},
)
def post(self, request, content_id):
"""
게시물(content_id)에 대해 공유 요청을 합니다.
"""

post = get_object_or_404(Post, content_id=content_id)

# TODO : how to increment share_count using serializer not passing data.
serializer = PostShareCountIncrementSerializer(
instance=post,
data={
"share_count": post.share_count + 1,
},
partial=True,
)

serializer.is_valid(raise_exception=True)
serializer.save()

return Response(status=status.HTTP_200_OK)