Service
home
WOW Onboarding
home
📝

식당 리스트 대소동

models.py

class UserRestaurantList(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) restaurant = models.ForeignKey(Restaurant, on_delete=models.CASCADE) class Meta: unique_together = ('user', 'restaurant')
Python
복사
ERD 따라 만들었습니다.
중복된 식당을 추가할 수 없도록 했습니다

urls.py

urlpatterns = [ # path("restaurants/", views.restaurant_list, name="restaurant-list"), path('restaurants/', views.user_restaurant_list, name='user-restaurant-list'), path('restaurants/<int:pk>/', views.add_restaurant, name='add-restaurant'), path('restaurants/<int:pk>/', views.remove_restaurant, name='remove-restaurant'), path("api-auth/", include("rest_framework.urls", namespace="rest_framework")), ]
Python
복사
API 명세서 기반으로 적었습니다.
맨 위에 주석 처리된 부분은 테스트를 위해 만든 부분이라고 하셔서 주석으로 해두었습니다.

views.py

@csrf_exempt @login_required def user_restaurant_list(request): if request.method == "GET": user_restaurants = UserRestaurantList.objects.filter(user=request.user) serializer = UserRestaurantListSerializer(user_restaurants, many=True) return JsonResponse(serializer.data, safe=False) @csrf_exempt @login_required def add_restaurant(request, pk): if request.method == "POST": try: restaurant = Restaurant.objects.get(pk=pk) UserRestaurantList.objects.create(user=request.user, restaurant=restaurant) return JsonResponse({"message": "Restaurant added successfully"}, status=201) except Restaurant.DoesNotExist: return JsonResponse({"message": "Restaurant not found"}, status=404) @csrf_exempt @login_required def remove_restaurant(request, pk): if request.method == "DELETE": try: user_restaurant = UserRestaurantList.objects.get(user=request.user, restaurant_id=pk) user_restaurant.delete() return JsonResponse({"message": "Restaurant deleted successfully"}, status=204) except UserRestaurantList.DoesNotExist: return JsonResponse({"message": "Restaurant not found in your list"}, status=404)
Python
복사

serializers.py

class UserRestaurantListSerializer(serializers.ModelSerializer): restaurant = RestaurantSerializer() class Meta: model = UserRestaurantList fields = '__all__'
Python
복사
테스트 중…
@api_view(['GET']) @login_required def friend_restaurant_list(request, id): try: # id에 해당하는 친구를 가져옴 friend = User.objects.get(id=id) # 친구의 맛집 리스트를 가져옴 friend_restaurants = UserRestaurantList.objects.filter(user=friend) serializer = RestaurantSerializer(friend_restaurants, many=True) return Response({'restaurants': serializer.data}, status=status.HTTP_200_OK) except User.DoesNotExist: return Response({"message": "Friend not found"}, status=status.HTTP_404_NOT_FOUND)
Python
복사
친구 리스트를 불러오는 부분을 작성했습니다.
검토를 하다가 rating 부분에서 구글, 네이버, 카카오 별점을 다 필요할 듯하여 API 명세 를 수정
로그인을 확인하던 부분을 주석처리하고 테스트를 위한 임시 유저(id=21)로 연결해두었습니다.
내 리스트 조회
친구 리스트 조회

cURL

설치 확인 : $ curl --version
POST
curl -X POST http://127.0.0.1:8000/restaurants/8/ \ -H "Content-Type: application/json"
PowerShell
복사
DELETE
curl -X DELETE http://127.0.0.1:8000/restaurants/8/
PowerShell
복사
로컬에서 내 리스트에 식당 추가 삭제 기능 테스트입니다. id 8번 식당을 추가 및 삭제