-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
114 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |