Skip to content

Commit

Permalink
feat: 친구 평가 기능
Browse files Browse the repository at this point in the history
  • Loading branch information
dkfla committed Aug 8, 2024
1 parent 6ce25a3 commit 8116cd0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
2 changes: 1 addition & 1 deletion friends/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

urlpatterns = [
path(
"friends/<int:id>/restaurants/",
"friends/<int:pk>/restaurants/",
views.friend_restaurant_list,
name="friend-restaurant-list",
),
Expand Down
49 changes: 38 additions & 11 deletions friends/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 8116cd0

Please sign in to comment.