Skip to content

Commit

Permalink
feat: 함께 저장한 식당 수
Browse files Browse the repository at this point in the history
  • Loading branch information
dkfla committed Aug 2, 2024
1 parent b006826 commit 8f102bd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
25 changes: 23 additions & 2 deletions friends/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,36 @@
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ["name", "profile_img", "reliability"]
fields = ["id", "name", "profile_img", "reliability"]


class FriendRequestSerializer(serializers.ModelSerializer):
user = UserSerializer(read_only=True)
common_restaurant_count = serializers.SerializerMethodField()

class Meta:
model = Friend
fields = ["user", "state"]
fields = ["user", "state", "common_restaurant_count"]

def get_common_restaurant_count(self, obj):
try:
user = obj.user
# friend_user = self.context.get('request').user
friend_user = User.objects.get(id=21)

user_restaurants = set(
UserRestaurantsList.objects.filter(user=user).values_list(
"restaurant_id", flat=True
)
)
friend_restaurants = set(
UserRestaurantsList.objects.filter(user=friend_user).values_list(
"restaurant_id", flat=True
)
)
return len(user_restaurants.intersection(friend_restaurants))
except User.DoesNotExist:
return 0


class FriendSerializer(serializers.ModelSerializer):
Expand Down
26 changes: 16 additions & 10 deletions friends/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,23 @@ def friend_restaurant_list(request, id):
@api_view(["GET"])
# @login_required
def friend_list(request):
user = User.objects.get(id=21)
try:
user = User.objects.get(id=21)

friend_request = Friend.objects.filter(friend=user, state="request")
friend_request_serialized = FriendRequestSerializer(
friend_request, context={"request": request}, many=True
).data

friend_request = Friend.objects.filter(friend=user, state="request")
friend_request_serialized = FriendRequestSerializer(friend_request, many=True).data
friends = Friend.objects.filter(user=user, state="approve")
friends_serialized = FriendSerializer(friends, many=True).data

friends = Friend.objects.filter(user=user, state="approve")
friends_serialized = FriendSerializer(friends, many=True).data
data = {
"friend_request": friend_request_serialized,
"friends": friends_serialized,
}

data = {
"friend_request": friend_request_serialized,
"friends": friends_serialized,
}
return Response(data)

return Response(data)
except User.DoesNotExist:
return Response({"message": "User not found"}, status=status.HTTP_404_NOT_FOUND)

0 comments on commit 8f102bd

Please sign in to comment.