def put(self, request, product_id): #getting the category, then pass the data product = Product.objects.get(id=product_id) serializer = ProductSerializer(product, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): serializer = ProductSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, product_id = None): if product_id is not None: product = Product.objects.get(id=product_id) serializer = ProductSerializer(product, many=False) return Response(serializer.data) else: product_set = Product.objects.all() serializer = ProductSerializer(product_set, many=True) return Response(serializer.data)
def list(self, request): if request.query_params['recent'] == 'true': products = Product.objects.filter( is_active=True).order_by('-updated_at') else: products = Product.objects.filter( is_active=True).order_by('product_name') if not products: return Response(status=status.HTTP_204_NO_CONTENT) else: serializer = ProductSerializer(data=products, many=True) serializer.is_valid() return Response(serializer.data, status=status.HTTP_200_OK)