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 c559f15 commit 955e424
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion restaurants/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def get_rating_average(self, obj):
class SearchHistorySerializer(serializers.ModelSerializer):
class Meta:
model = SearchHistory
fields = ["query", "timestamp"]
fields = ["id", "query", "timestamp"]


class UserRestaurantListSerializer(serializers.ModelSerializer):
Expand Down
14 changes: 13 additions & 1 deletion restaurants/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def restaurant_list(request):


@csrf_exempt
@api_view(["GET", "POST"])
@api_view(["GET", "POST", "DELETE"])
# @login_required
def search(request):
user = User.objects.get(id=21) # 임시 유저 지정, 추후 삭제
Expand All @@ -50,6 +50,18 @@ def search(request):
logging.debug("Serialized data: %s", data)
return Response({"results": data})

elif request.method == "DELETE":
history_id = request.data.get("id", "")
if not history_id:
return Response({"error": "No history ID provided"}, status=400)

try:
history_to_delete = SearchHistory.objects.get(id=history_id, user=user)
history_to_delete.delete()
return Response({"message": "Search history deleted successfully"})
except SearchHistory.DoesNotExist:
return Response({"error": "No matching search history found"}, status=404)

else:
return Response({"error": "Unsupported method"}, status=405)

Expand Down

0 comments on commit 955e424

Please sign in to comment.