Skip to content

Commit

Permalink
feat: 답글 조회 기능
Browse files Browse the repository at this point in the history
  • Loading branch information
dkfla committed Aug 7, 2024
1 parent a29de61 commit f998ed8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 19 deletions.
2 changes: 1 addition & 1 deletion restaurants/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
name="restaurant-detail",
),
path("restaurants/", views.user_restaurant_list, name="user-restaurant-list"),
path("restaurants/<int:pk>/reviews/", review, name="review-write"),
path("restaurants/<int:pk>/reviews/", review, name="review"),
path(
"restaurants/<int:restaurant_id>/reviews/<int:review_id>/",
evaluate_review,
Expand Down
13 changes: 13 additions & 0 deletions reviews/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
2 changes: 1 addition & 1 deletion reviews/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@


urlpatterns = [
path("reviews/<int:pk>/", views.reply_write, name="reply-write"),
path("reviews/<int:pk>/", views.reply, name="reply"),
]
47 changes: 30 additions & 17 deletions reviews/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"])
Expand Down

0 comments on commit f998ed8

Please sign in to comment.