Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 리스트 응답 구조 변경 #59

Merged
merged 4 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion friends/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ class RestaurantlistSerializer(serializers.ModelSerializer):

class Meta:
model = Restaurant
fields = ["id", "name", "food_type", "rating_average", "latitude", "longitude"]
fields = [
"id",
"name",
"food_type",
"rating_average",
"latitude",
"longitude",
"image_url",
]

def get_rating_average(self, obj):
return obj.rating_average()
Expand Down
11 changes: 7 additions & 4 deletions friends/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from rest_framework import status

# from django.contrib.auth.decorators import login_required
from restaurants.models import UserRestaurantsList
from restaurants.models import UserRestaurantsList, Restaurant
from .serializers import (
FriendSerializer,
FriendRequestSerializer,
FriendRestaurantSerializer,
RestaurantlistSerializer,
)
from accounts.models import User
from .models import Friend
Expand All @@ -24,9 +24,12 @@ def friend_restaurant_list(request, id):

# 친구의 맛집 리스트를 가져옴
friend_restaurants = UserRestaurantsList.objects.filter(user=friend)
serializer = FriendRestaurantSerializer(friend_restaurants, many=True)
restaurant_ids = friend_restaurants.values_list("restaurant_id", flat=True)
restaurants = Restaurant.objects.filter(id__in=restaurant_ids)

return Response({"restaurants": serializer.data}, status=status.HTTP_200_OK)
serializer = RestaurantlistSerializer(restaurants, many=True)

return Response({"results": serializer.data}, status=status.HTTP_200_OK)
except User.DoesNotExist:
return Response(
{"message": "Friend not found"}, status=status.HTTP_404_NOT_FOUND
Expand Down
17 changes: 17 additions & 0 deletions restaurants/migrations/0006_restaurant_image_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.14 on 2024-08-04 08:48

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("restaurants", "0005_restaurant_food_type"),
]

operations = [
migrations.AddField(
model_name="restaurant",
name="image_url",
field=models.URLField(blank=True, max_length=500, null=True),
),
]
1 change: 1 addition & 0 deletions restaurants/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Restaurant(models.Model):
address = models.CharField(max_length=255)
latitude = models.DecimalField(max_digits=11, decimal_places=8)
longitude = models.DecimalField(max_digits=11, decimal_places=8)
image_url = models.URLField(max_length=500, null=True, blank=True)

def rating_average(self):
ratings = [self.rating_naver, self.rating_kakao, self.rating_google]
Expand Down
10 changes: 9 additions & 1 deletion restaurants/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ class RestaurantlistSerializer(serializers.ModelSerializer):

class Meta:
model = Restaurant
fields = ["id", "name", "food_type", "rating_average", "latitude", "longitude"]
fields = [
"id",
"name",
"food_type",
"rating_average",
"latitude",
"longitude",
"image_url",
]

def get_rating_average(self, obj):
return obj.rating_average()
Expand Down
19 changes: 13 additions & 6 deletions restaurants/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
RestaurantSerializer,
RestaurantListSerializer,
SearchHistorySerializer,
UserRestaurantListSerializer,
RestaurantlistSerializer,
RestaurantDetailSerializer,
)

Expand Down Expand Up @@ -70,11 +70,18 @@ def search(request):
@api_view(["GET"])
# @login_required
def user_restaurant_list(request):
user = User.objects.get(id=21) # 임시 유저 지정, 추후 삭제
user_restaurants = UserRestaurantsList.objects.filter(user=user) # 추후 삭제
# user_restaurants = UserRestaurantsList.objects.filter(user=request.user)
serializer = UserRestaurantListSerializer(user_restaurants, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
try:
user = User.objects.get(id=21) # 임시 유저 지정, 추후 삭제
user_restaurants = UserRestaurantsList.objects.filter(user=user) # 추후 삭제
# user_restaurants = UserRestaurantsList.objects.filter(user=request.user)
restaurant_ids = user_restaurants.values_list("restaurant_id", flat=True)
restaurants = Restaurant.objects.filter(id__in=restaurant_ids)
serializer = RestaurantlistSerializer(restaurants, many=True)
return Response({"results": serializer.data}, status=status.HTTP_200_OK)
except User.DoesNotExist:
return Response(
{"message": "Default user not found"}, status=status.HTTP_404_NOT_FOUND
)


@csrf_exempt
Expand Down
Loading