add: add search api
This commit is contained in:
@@ -2,6 +2,7 @@ from django.urls import path, include
|
|||||||
|
|
||||||
from core.apps.products.views import category as category_veiws
|
from core.apps.products.views import category as category_veiws
|
||||||
from core.apps.products.views import product as product_views
|
from core.apps.products.views import product as product_views
|
||||||
|
from core.apps.products.views import search as search_views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('category/', include(
|
path('category/', include(
|
||||||
@@ -13,5 +14,6 @@ urlpatterns = [
|
|||||||
[
|
[
|
||||||
path('<uuid:category_id>/list/', product_views.ProductListApiView.as_view()),
|
path('<uuid:category_id>/list/', product_views.ProductListApiView.as_view()),
|
||||||
]
|
]
|
||||||
))
|
)),
|
||||||
|
path('search/', search_views.SearchApiView.as_view()),
|
||||||
]
|
]
|
||||||
26
core/apps/products/views/search.py
Normal file
26
core/apps/products/views/search.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
from rest_framework import views
|
||||||
|
from rest_framework.response import Response
|
||||||
|
|
||||||
|
from core.apps.products.models import Product, Category
|
||||||
|
from core.apps.products.serializers.category import CategoryListSerializer
|
||||||
|
from core.apps.products.serializers.product import ProductListSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class SearchApiView(views.APIView):
|
||||||
|
permission_classes = []
|
||||||
|
|
||||||
|
def get(self, request):
|
||||||
|
search = request.query_param.get('search')
|
||||||
|
if search is None:
|
||||||
|
return Response({"success": False, "message": 'search is required'}, status=400)
|
||||||
|
products = Product.objects.filter(name__istartswith=search)
|
||||||
|
categories = Category.objects.filter(name__istartswith=search)
|
||||||
|
|
||||||
|
return Response(
|
||||||
|
{
|
||||||
|
'products': ProductListSerializer(products, many=True).data,
|
||||||
|
'categories': CategoryListSerializer(categories, many=True).data,
|
||||||
|
},
|
||||||
|
status=200
|
||||||
|
)
|
||||||
|
|
||||||
Reference in New Issue
Block a user