diff --git a/app/conf/api.py b/app/conf/api.py index d5c30c1..189e6e9 100644 --- a/app/conf/api.py +++ b/app/conf/api.py @@ -1,7 +1,8 @@ from django.urls import path -from search.api.views import SearchApi +from search.api.views import SearchApi, HintApi urlpatterns = [ - path("search", SearchApi.as_view(), name="search_api") + path("search", SearchApi.as_view(), name="search_api"), + path("hint", HintApi.as_view(), name="hint api") ] diff --git a/app/search/api/serializers.py b/app/search/api/serializers.py index 0671e5e..f40723e 100644 --- a/app/search/api/serializers.py +++ b/app/search/api/serializers.py @@ -19,3 +19,15 @@ class ResponseSerializer(serializers.Serializer): def update(self, instance, validated_data): raise NotImplemented + + +class HintRequestSerializer(serializers.Serializer): + content = serializers.CharField() + + def create(self, validated_data): + raise NotImplemented + + +class HintResponseSerializer(serializers.Serializer): + type = serializers.CharField() + content = serializers.CharField() diff --git a/app/search/api/views.py b/app/search/api/views.py index 12d54a3..952a655 100644 --- a/app/search/api/views.py +++ b/app/search/api/views.py @@ -3,12 +3,13 @@ from drf_yasg.utils import swagger_auto_schema from rest_framework import status from rest_framework.response import Response from rest_framework.views import APIView +from search.api.serializers import HintRequestSerializer -from search.api.serializers import SearchSerializer, ResponseSerializer +from search.api.serializers import SearchSerializer, ResponseSerializer, HintResponseSerializer from search.services.search import process_string user_response = openapi.Response("search results", ResponseSerializer) - +hint_response = openapi.Response("hints", HintResponseSerializer) class SearchApi(APIView): @swagger_auto_schema(request_body=SearchSerializer, responses={200: user_response}) @@ -18,3 +19,17 @@ class SearchApi(APIView): return Response( process_string(serializer.data["body"]), status=status.HTTP_200_OK ) + + +class HintApi(APIView): + @swagger_auto_schema(request_body=HintRequestSerializer, responses={200: hint_response}) + def post(self, request, format=None): + serializer = HintRequestSerializer(data=request.data) + serializer.is_valid(raise_exception=True) + return Response( + { + 'type': 'category', + 'value': serializer.data['content'] + }, + status=status.HTTP_200_OK + ) \ No newline at end of file diff --git a/app/search/services/search.py b/app/search/services/search.py index f8dc801..5ebf07e 100644 --- a/app/search/services/search.py +++ b/app/search/services/search.py @@ -1,5 +1,6 @@ from search.models import Product +from typing import List -def process_string(text: str) -> [dict]: +def process_string(text: str) -> List[dict]: return [x.serialize_self() for x in Product.objects.filter(name__contains=text)[5:]] diff --git a/requirements/base.txt b/requirements/base.txt index 51f028d..6da3976 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -2,7 +2,7 @@ djangorestframework==3.14.0 Django==4.0.8 django-cors-headers==3.13.0 django-environ==0.9.0 -"drf-yasg[validation]" +drf-yasg[validation] typing-extensions Pillow==9.2.0