diff --git a/restaurants/migrations/0003_searchhistory.py b/restaurants/migrations/0003_searchhistory.py new file mode 100644 index 0000000..1040573 --- /dev/null +++ b/restaurants/migrations/0003_searchhistory.py @@ -0,0 +1,41 @@ +# Generated by Django 4.2.14 on 2024-07-30 17:48 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ("restaurants", "0002_alter_restaurant_latitude_alter_restaurant_longitude"), + ] + + operations = [ + migrations.CreateModel( + name="SearchHistory", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("query", models.CharField(max_length=255)), + ("timestamp", models.DateTimeField(auto_now_add=True)), + ( + "user", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to=settings.AUTH_USER_MODEL, + ), + ), + ], + options={ + "ordering": ["-timestamp"], + }, + ), + ] diff --git a/restaurants/models.py b/restaurants/models.py index 8524d60..0dc9d4e 100644 --- a/restaurants/models.py +++ b/restaurants/models.py @@ -1,4 +1,5 @@ from django.db import models +from accounts.models import User # Create your models here. @@ -17,3 +18,19 @@ 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) + + def rating_average(self): + ratings = [self.rating_naver, self.rating_kakao, self.rating_google] + valid_ratings = [rating for rating in ratings if rating is not None] + if valid_ratings: + return sum(valid_ratings) / len(valid_ratings) + return None + + +class SearchHistory(models.Model): + user = models.ForeignKey(User, on_delete=models.CASCADE) + query = models.CharField(max_length=255) + timestamp = models.DateTimeField(auto_now_add=True) + + class Meta: + ordering = ["-timestamp"] diff --git a/restaurants/serializers.py b/restaurants/serializers.py index ea58b97..452e64a 100644 --- a/restaurants/serializers.py +++ b/restaurants/serializers.py @@ -1,8 +1,25 @@ from rest_framework import serializers -from .models import Restaurant +from .models import Restaurant, SearchHistory class RestaurantSerializer(serializers.ModelSerializer): class Meta: model = Restaurant fields = "__all__" + + +class RestaurantListSerializer(serializers.ModelSerializer): + rating_average = serializers.SerializerMethodField() + + class Meta: + model = Restaurant + fields = ["name", "rating_average", "address"] + + def get_rating_average(self, obj): + return obj.rating_average + + +class SearchHistorySerializer(serializers.ModelSerializer): + class Meta: + model = SearchHistory + fields = ["query", "timestamp"] diff --git a/restaurants/urls.py b/restaurants/urls.py index a6ffb5d..9bf61e7 100644 --- a/restaurants/urls.py +++ b/restaurants/urls.py @@ -5,4 +5,5 @@ urlpatterns = [ path("restaurants/", views.restaurant_list, name="restaurant-list"), path("api-auth/", include("rest_framework.urls", namespace="rest_framework")), + path("search/", views.search, name="search"), ] diff --git a/restaurants/views.py b/restaurants/views.py index 15a01a6..9ec565e 100644 --- a/restaurants/views.py +++ b/restaurants/views.py @@ -1,13 +1,42 @@ # from django.shortcuts import render -from django.http import JsonResponse -from django.views.decorators.csrf import csrf_exempt -from .models import Restaurant -from .serializers import RestaurantSerializer +from rest_framework.decorators import api_view +from rest_framework.response import Response +from .models import Restaurant, SearchHistory +from .serializers import ( + RestaurantSerializer, + RestaurantListSerializer, + SearchHistorySerializer, +) -@csrf_exempt +@api_view(["GET"]) def restaurant_list(request): + restaurants = Restaurant.objects.all() + serializer = RestaurantSerializer(restaurants, many=True) + return Response(serializer.data) + + +@api_view(["GET", "POST"]) +def search(request): if request.method == "GET": - restaurants = Restaurant.objects.all() - serializer = RestaurantSerializer(restaurants, many=True) - return JsonResponse(serializer.data, safe=False) + if request.user.is_authenticated: + histories = SearchHistory.objects.filter(user=request.user) + serializer = SearchHistorySerializer(histories, many=True) + return Response({"histories": serializer.data}) + else: + return Response({"error": "User not authenticated"}, status=401) + + elif request.method == "POST": + query = request.data.get("query", "") + if not query: + return Response({"error": "No search query provided"}, status=400) + + if request.user.is_authenticated: + SearchHistory.objects.create(user=request.user, query=query) + + restaurants = Restaurant.objects.filter(name__icontains=query) + serializer = RestaurantListSerializer(restaurants, many=True) + return Response({"results": serializer.data}) + + else: + return Response({"error": "Unsupported method"}, status=405)