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 작성 #17

Merged
merged 10 commits into from
Oct 27, 2023
4 changes: 3 additions & 1 deletion config/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from rest_framework import permissions

from django.contrib import admin
from django.urls import path
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

Expand All @@ -24,6 +24,8 @@
path("admin/", admin.site.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)
JaeHyuckSa marked this conversation as resolved.
Show resolved Hide resolved
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)
19 changes: 19 additions & 0 deletions posts/migrations/0002_post_content_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.6 on 2023-10-26 11:12

from django.db import migrations, models
import uuid


class Migration(migrations.Migration):

dependencies = [
('posts', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='post',
name='content_id',
field=models.UUIDField(default=uuid.uuid4, editable=False),
),
]
3 changes: 3 additions & 0 deletions posts/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import uuid

from django.db import models

from users.models import User
Expand All @@ -10,6 +12,7 @@ class PostType(models.TextChoices):
INSTAGRAM = "instagram"
THREADS = "threads"

content_id = models.UUIDField(default=uuid.uuid4, editable=False)
JaeHyuckSa marked this conversation as resolved.
Show resolved Hide resolved
post_type = models.CharField(max_length=16, choices=PostType.choices)
title = models.CharField(max_length=32)
content = models.TextField()
Expand Down