Skip to content

Commit

Permalink
feat: 검색 기능
Browse files Browse the repository at this point in the history
  • Loading branch information
dkfla committed Jul 30, 2024
1 parent 58d580d commit a83f1bf
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 9 deletions.
41 changes: 41 additions & 0 deletions restaurants/migrations/0003_searchhistory.py
Original file line number Diff line number Diff line change
@@ -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"],
},
),
]
17 changes: 17 additions & 0 deletions restaurants/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from accounts.models import User

# Create your models here.

Expand All @@ -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"]
19 changes: 18 additions & 1 deletion restaurants/serializers.py
Original file line number Diff line number Diff line change
@@ -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"]
1 change: 1 addition & 0 deletions restaurants/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
]
45 changes: 37 additions & 8 deletions restaurants/views.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit a83f1bf

Please sign in to comment.