backend/app/search/api/views.py

98 lines
3.1 KiB
Python
Raw Normal View History

2022-10-21 21:39:04 +03:00
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework import status
from rest_framework.generics import get_object_or_404
2022-10-21 21:39:04 +03:00
from rest_framework.response import Response
from rest_framework.views import APIView
2022-10-21 23:22:14 +03:00
from search.api.serializers import HintRequestSerializer
2022-10-21 21:39:04 +03:00
2022-10-22 05:07:25 +03:00
from search.api.serializers import (
SearchSerializer,
ResponseSerializer,
HintResponseSerializer,
AutoCompleteRequestSerializer,
AutoCompleteResponseSerializer,
)
from search.models import Product
from search.services.colors import group
2022-10-23 12:11:53 +03:00
from search.services.search.main import process_search
2022-10-22 03:09:14 +03:00
from search.services.autocomplete_schema import autocomplete_schema
2022-10-21 21:39:04 +03:00
2022-10-21 23:57:39 +03:00
from search.services.hints import get_hints
2022-10-21 21:39:04 +03:00
user_response = openapi.Response("search results", ResponseSerializer)
2022-10-21 23:22:14 +03:00
hint_response = openapi.Response("hints", HintResponseSerializer)
2022-10-22 05:07:25 +03:00
autocomplete_response = openapi.Response(
"autocomplete schema", AutoCompleteResponseSerializer
)
2022-10-22 03:09:14 +03:00
2022-10-21 21:39:04 +03:00
class SearchApi(APIView):
@swagger_auto_schema(request_body=SearchSerializer, responses={200: user_response})
2022-10-22 05:07:25 +03:00
def post(self, request):
2022-10-21 21:39:04 +03:00
serializer = SearchSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
return Response(
group(
process_search(
serializer.data["body"],
serializer.data["limit"],
serializer.data["offset"],
),
2022-10-22 11:43:30 +03:00
serializer.data["body"],
),
status=status.HTTP_200_OK,
2022-10-21 21:39:04 +03:00
)
2022-10-21 23:22:14 +03:00
class HintApi(APIView):
2022-10-22 05:07:25 +03:00
@swagger_auto_schema(
request_body=HintRequestSerializer, responses={200: hint_response}
)
def post(self, request):
2022-10-21 23:22:14 +03:00
serializer = HintRequestSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
return Response(
{
2022-10-22 05:07:25 +03:00
"type": get_hints(serializer.data["content"]),
"value": serializer.data["content"],
2022-10-21 23:22:14 +03:00
},
2022-10-22 05:07:25 +03:00
status=status.HTTP_200_OK,
2022-10-22 03:09:14 +03:00
)
2022-10-22 05:07:25 +03:00
2022-10-22 03:09:14 +03:00
class AutoCompleteApi(APIView):
2022-10-22 05:07:25 +03:00
@swagger_auto_schema(
request_body=AutoCompleteRequestSerializer,
responses={200: autocomplete_response},
)
def post(self, request):
2022-10-22 03:09:14 +03:00
serializer = AutoCompleteRequestSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
return Response(
{
"nodes": autocomplete_schema(
serializer.data["content"], serializer.data["exclude"]
)
},
2022-10-22 05:07:25 +03:00
status=status.HTTP_200_OK,
2022-10-22 03:09:14 +03:00
)
class IncreaseProductScoreApi(APIView):
@swagger_auto_schema(
manual_parameters=[
openapi.Parameter(
"id",
openapi.IN_PATH,
description="Product id",
type=openapi.TYPE_INTEGER,
)
]
)
def post(self, request, pk):
product = get_object_or_404(Product, id=pk)
product.score += 1
product.save(update_fields=["score"])
return Response({"score": product.score}, status=status.HTTP_200_OK)