Skip to content

Commit

Permalink
Merge branch 'feature/issue-009' of https://github.com/Django-Wanted-…
Browse files Browse the repository at this point in the history
  • Loading branch information
simseulnyang committed Oct 27, 2023
2 parents 8ec84cb + 60ccaba commit c50f23c
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 2 deletions.
2 changes: 2 additions & 0 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
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")),
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Expand Down
9 changes: 9 additions & 0 deletions likes/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 PostLikeIncrementSerializer(ModelSerializer):
class Meta:
model = Post
fields = ("like_count",)
50 changes: 49 additions & 1 deletion likes/tests.py
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)
7 changes: 7 additions & 0 deletions likes/urls.py
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"),
]
36 changes: 35 additions & 1 deletion likes/views.py
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)
2 changes: 2 additions & 0 deletions posts/migrations/0002_post_content_id.py
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

Expand Down

0 comments on commit c50f23c

Please sign in to comment.