Skip to content

Commit

Permalink
Merge pull request #17 from Django-Wanted-Internship-3-Team/feature/i…
Browse files Browse the repository at this point in the history
…ssue-011

게시물 좋아요 API 작성
  • Loading branch information
Chestnut90 authored Oct 27, 2023
2 parents 79ee2b5 + e7ed99b commit cdfb8b9
Show file tree
Hide file tree
Showing 7 changed files with 124 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)
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)
post_type = models.CharField(max_length=16, choices=PostType.choices)
title = models.CharField(max_length=32)
content = models.TextField()
Expand Down

0 comments on commit cdfb8b9

Please sign in to comment.