From 8116cd0d8c4b2f2acbce210eee80cde85aec5a01 Mon Sep 17 00:00:00 2001 From: dkfla Date: Thu, 8 Aug 2024 16:44:36 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=B9=9C=EA=B5=AC=20=ED=8F=89=EA=B0=80?= =?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 --- friends/urls.py | 2 +- friends/views.py | 49 +++++++++++++++++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/friends/urls.py b/friends/urls.py index de465fe..c853796 100644 --- a/friends/urls.py +++ b/friends/urls.py @@ -3,7 +3,7 @@ urlpatterns = [ path( - "friends//restaurants/", + "friends//restaurants/", views.friend_restaurant_list, name="friend-restaurant-list", ), diff --git a/friends/views.py b/friends/views.py index 5ffd19c..8966de1 100644 --- a/friends/views.py +++ b/friends/views.py @@ -10,6 +10,7 @@ # from django.contrib.auth.decorators import login_required from restaurants.models import UserRestaurantsList, Restaurant from .serializers import ( + UserSerializer, FriendSerializer, FriendRequestSerializer, RestaurantlistSerializer, @@ -27,21 +28,47 @@ from django.shortcuts import get_object_or_404 -@api_view(["GET"]) -# @login_required -def friend_restaurant_list(request, id): +@api_view(["GET", "POST"]) +# @authentication_classes([TokenAuthentication]) +# @permission_classes([IsAuthenticated]) +def friend_restaurant_list(request, pk): try: - # id에 해당하는 친구를 가져옴 - friend = User.objects.get(id=id) + friend = User.objects.get(pk=pk) + + if request.method == "GET": + friend_restaurants = UserRestaurantsList.objects.filter(user=friend) + restaurant_ids = friend_restaurants.values_list("restaurant_id", flat=True) + restaurants = Restaurant.objects.filter(id__in=restaurant_ids) + + friend_info = UserSerializer(friend).data + restaurants_data = RestaurantlistSerializer(restaurants, many=True).data + + response_data = {"freind": friend_info, "restaurants": restaurants_data} + return Response(response_data, status=status.HTTP_200_OK) - # 친구의 맛집 리스트를 가져옴 - friend_restaurants = UserRestaurantsList.objects.filter(user=friend) - restaurant_ids = friend_restaurants.values_list("restaurant_id", flat=True) - restaurants = Restaurant.objects.filter(id__in=restaurant_ids) + elif request.method == "POST": + evaluate_value = request.data.get("evaluate") - serializer = RestaurantlistSerializer(restaurants, many=True) + if evaluate_value not in [0, 1]: # 0:싫어요 / 1:좋아요 + return Response( + {"error": "Invalid evaluate value"}, + status=status.HTTP_400_BAD_REQUEST, + ) + + if evaluate_value == 1: + friend.reliability += 5 + elif evaluate_value == 0: + friend.reliability -= 5 + friend.save() + + return Response( + { + "message": "Friend evaluated successfully", + "reliability": friend.reliability, + }, + status=status.HTTP_200_OK, + ) - return Response(serializer.data, status=status.HTTP_200_OK) except User.DoesNotExist: return Response( {"message": "Friend not found"}, status=status.HTTP_404_NOT_FOUND