Skip to content

Commit

Permalink
fix: 검색 기록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
dkfla committed Aug 7, 2024
1 parent 57f2f3f commit 1f46fa8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
32 changes: 24 additions & 8 deletions restaurants/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from django.views.decorators.csrf import csrf_exempt
import logging
from accounts.models import User # 임시 유저 지정을 위한 임포트, 추후 삭제
from django.db.models import Q
from django.db.models import Q, Subquery, OuterRef
from django.utils import timezone


@csrf_exempt
Expand All @@ -34,10 +35,16 @@ def restaurant_list(request):
def search(request):
user = User.objects.get(id=21) # 임시 유저 지정, 추후 삭제
if request.method == "GET":
histories = SearchHistory.objects.filter(user=user) # 추후 삭제
# histories = SearchHistory.objects.filter(user=request.user)
serializer = SearchHistorySerializer(histories, many=True)
return Response({"histories": serializer.data})
latest_searches = SearchHistory.objects.filter(
user=user,
id=Subquery(
SearchHistory.objects.filter(user=user, query=OuterRef("query"))
.order_by("-timestamp")
.values("id")[:1]
),
)
serializer = SearchHistorySerializer(latest_searches, many=True)
return Response(serializer.data)

elif request.method == "POST":
query = request.data.get("query", "")
Expand All @@ -47,8 +54,17 @@ def search(request):
status=status.HTTP_400_BAD_REQUEST,
)

SearchHistory.objects.create(user=user, query=query) # 추후 삭제
# SearchHistory.objects.create(user=request.user, query=query)
existing_history = SearchHistory.objects.filter(
user=user, query=query
).first() # 추후 삭제
# existing_history = SearchHistory.objects.filter(user=request.user, query=query).first()

if existing_history:
existing_history.timestamp = timezone.now()
existing_history.save()
else:
SearchHistory.objects.create(user=user, query=query) # 추후 삭제
# SearchHistory.objects.create(user=request.user, query=query)

query_terms = query.split()
q_objects = Q()
Expand All @@ -59,7 +75,7 @@ def search(request):
serializer = RestaurantListSerializer(restaurants, many=True)
data = serializer.data
logging.debug("Serialized data: %s", data)
return Response({"results": data})
return Response(data)

elif request.method == "DELETE":
history_id = request.data.get("id", "")
Expand Down
4 changes: 1 addition & 3 deletions reviews/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ def review(request, pk):
한줄평 작성 기능 (POST)
"""
if request.method == "GET":
# 리뷰 조회 기능
reviews = Review.objects.filter(restaurant_id=pk).order_by("-date")
serializer = ReviewListSerializer(reviews, many=True)
return Response({"results": serializer.data}, status=status.HTTP_200_OK)
return Response(serializer.data, status=status.HTTP_200_OK)

elif request.method == "POST":
# 리뷰 작성 기능
try:
restaurant = Restaurant.objects.get(pk=pk)
except Restaurant.DoesNotExist:
Expand Down

0 comments on commit 1f46fa8

Please sign in to comment.