From f998ed850afe194927ae063b80c2176ca107d7d6 Mon Sep 17 00:00:00 2001 From: dkfla Date: Wed, 7 Aug 2024 13:27:56 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=8B=B5=EA=B8=80=20=EC=A1=B0=ED=9A=8C?= =?UTF-8?q?=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- restaurants/urls.py | 2 +- reviews/serializers.py | 13 ++++++++++++ reviews/urls.py | 2 +- reviews/views.py | 47 +++++++++++++++++++++++++++--------------- 4 files changed, 45 insertions(+), 19 deletions(-) diff --git a/restaurants/urls.py b/restaurants/urls.py index c3d0f06..a9b6b4c 100644 --- a/restaurants/urls.py +++ b/restaurants/urls.py @@ -19,7 +19,7 @@ name="restaurant-detail", ), path("restaurants/", views.user_restaurant_list, name="user-restaurant-list"), - path("restaurants//reviews/", review, name="review-write"), + path("restaurants//reviews/", review, name="review"), path( "restaurants//reviews//", evaluate_review, diff --git a/reviews/serializers.py b/reviews/serializers.py index 6f4a78e..4c14c44 100644 --- a/reviews/serializers.py +++ b/reviews/serializers.py @@ -35,3 +35,16 @@ class Meta: "date", "replies_count", ] + + +class ReplyListSerializer(serializers.ModelSerializer): + user_name = serializers.CharField(source="user.name", read_only=True) + + class Meta: + model = Reply + fields = [ + "id", + "user_name", + "content", + "date", + ] diff --git a/reviews/urls.py b/reviews/urls.py index 6658cba..34cea32 100644 --- a/reviews/urls.py +++ b/reviews/urls.py @@ -3,5 +3,5 @@ urlpatterns = [ - path("reviews//", views.reply_write, name="reply-write"), + path("reviews//", views.reply, name="reply"), ] diff --git a/reviews/views.py b/reviews/views.py index 838c3b8..d1b527c 100644 --- a/reviews/views.py +++ b/reviews/views.py @@ -3,8 +3,13 @@ from rest_framework import status from accounts.models import User from restaurants.models import Restaurant -from .models import Review, Recommend -from .serializers import ReviewSerializer, ReplySerializer, ReviewListSerializer +from .models import Review, Recommend, Reply +from .serializers import ( + ReviewSerializer, + ReplySerializer, + ReviewListSerializer, + ReplyListSerializer, +) # from rest_framework.authentication import TokenAuthentication # from rest_framework.permissions import IsAuthenticated @@ -46,26 +51,34 @@ def review(request, pk): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) -@api_view(["POST"]) +@api_view(["GET", "POST"]) # @authentication_classes([TokenAuthentication]) # @permission_classes([IsAuthenticated]) -def reply_write(request, pk): - user = User.objects.get(id=21) # 임시 유저 지정, 추후 삭제 +def reply(request, pk): + if request.method == "GET": + replies = Reply.objects.filter(review_id=pk).order_by("date") + serializer = ReplyListSerializer(replies, many=True) + return Response(serializer.data, status=status.HTTP_200_OK) - try: - review = Review.objects.get(id=pk) - except Review.DoesNotExist: - return Response({"error": "Review not found"}, status=status.HTTP_404_NOT_FOUND) + elif request.method == "POST": + user = User.objects.get(id=21) # 임시 유저 지정, 추후 삭제 + + try: + review = Review.objects.get(id=pk) + except Review.DoesNotExist: + return Response( + {"error": "Review not found"}, status=status.HTTP_404_NOT_FOUND + ) - data = request.data - data["user"] = user.id - data["review"] = review.id + data = request.data + data["user"] = user.id + data["review"] = review.id - serializer = ReplySerializer(data=data) - if serializer.is_valid(): - serializer.save() - return Response(serializer.data, status=status.HTTP_201_CREATED) - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + serializer = ReplySerializer(data=data) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data, status=status.HTTP_201_CREATED) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) @api_view(["POST"])