add search filter for product list api
This commit is contained in:
@@ -1,47 +1,58 @@
|
||||
from django.db.models import Q
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from rest_framework import generics, views
|
||||
from rest_framework.response import Response
|
||||
|
||||
from core.apps.accounts.permissions.permissions import HasRolePermission
|
||||
from core.apps.products.models import Product
|
||||
from core.apps.products.serializers import product as serializers
|
||||
from core.apps.accounts.permissions.permissions import HasRolePermission
|
||||
from core.apps.shared.paginations.custom import CustomPageNumberPagination
|
||||
|
||||
|
||||
class ProductListApiView(generics.ListAPIView):
|
||||
serializer_class = serializers.ProductListSerializer
|
||||
queryset = Product.objects.select_related('unity').only(
|
||||
'id', 'name', 'type', 'unity'
|
||||
queryset = Product.objects.select_related("unity").only(
|
||||
"id", "name", "type", "unity"
|
||||
)
|
||||
permission_classes = [HasRolePermission]
|
||||
pagination_class = CustomPageNumberPagination
|
||||
|
||||
def get(self, request):
|
||||
search = request.query_params.get("search")
|
||||
if search:
|
||||
self.queryset = self.queryset.filter(
|
||||
Q(name__istartswith=search) | Q(unity__value__istartswith=search)
|
||||
)
|
||||
page = self.paginate_queryset(self.queryset)
|
||||
if page is not None:
|
||||
serializer = self.serializer_class(page, many=True)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
|
||||
|
||||
class ProductCreateApiView(generics.GenericAPIView):
|
||||
serializer_class = serializers.ProductSerializer
|
||||
queryset = Product.objects.all()
|
||||
permission_classes = [HasRolePermission]
|
||||
|
||||
|
||||
def post(self, request):
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if serializer.is_valid(raise_exception=True):
|
||||
data = serializer.save()
|
||||
return Response(
|
||||
{
|
||||
'success': True,
|
||||
'message': "product successfully created!",
|
||||
'product': serializers.ProductListSerializer(data).data
|
||||
"success": True,
|
||||
"message": "product successfully created!",
|
||||
"product": serializers.ProductListSerializer(data).data,
|
||||
},
|
||||
status=201
|
||||
status=201,
|
||||
)
|
||||
return Response(
|
||||
{
|
||||
"success": False,
|
||||
"message": "an error occurred while adding th product.",
|
||||
"error": serializer.errors
|
||||
"error": serializer.errors,
|
||||
},
|
||||
status=400
|
||||
status=400,
|
||||
)
|
||||
|
||||
|
||||
@@ -56,34 +67,36 @@ class ProductUpdateApiView(generics.GenericAPIView):
|
||||
if serializer.is_valid(raise_exception=True):
|
||||
serializer.save()
|
||||
return Response(
|
||||
{'success': True, 'message': 'product successfully updated!'},
|
||||
status=200
|
||||
{"success": True, "message": "product successfully updated!"},
|
||||
status=200,
|
||||
)
|
||||
return Response(
|
||||
{
|
||||
'success': False,
|
||||
'message': "an error occurred while updating the product.",
|
||||
"success": False,
|
||||
"message": "an error occurred while updating the product.",
|
||||
"error": serializer.errors,
|
||||
},
|
||||
status=400
|
||||
status=400,
|
||||
)
|
||||
|
||||
def patch(self, request, product_id):
|
||||
product = get_object_or_404(Product, id=product_id)
|
||||
serializer = self.serializer_class(data=request.data, instance=product, partial=True)
|
||||
serializer = self.serializer_class(
|
||||
data=request.data, instance=product, partial=True
|
||||
)
|
||||
if serializer.is_valid(raise_exception=True):
|
||||
serializer.save()
|
||||
return Response(
|
||||
{'success': True, "message": "product successfully updated!"},
|
||||
status=200
|
||||
{"success": True, "message": "product successfully updated!"},
|
||||
status=200,
|
||||
)
|
||||
return Response(
|
||||
{
|
||||
"successs": False,
|
||||
"message": "an error accurred while updating the product.",
|
||||
"error": serializer.errors
|
||||
"error": serializer.errors,
|
||||
},
|
||||
status=400
|
||||
status=400,
|
||||
)
|
||||
|
||||
|
||||
@@ -94,6 +107,5 @@ class ProductDeleteApiView(views.APIView):
|
||||
product = get_object_or_404(Product, id=product_id)
|
||||
product.delete()
|
||||
return Response(
|
||||
{'success': True, 'message': 'product successfully deleted!'},
|
||||
status=204
|
||||
)
|
||||
{"success": True, "message": "product successfully deleted!"}, status=204
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user