gamingflexer commited on
Commit
5f901fb
·
1 Parent(s): 28f1741

Add new API endpoints for searching, retrieving, and updating products

Browse files
Files changed (2) hide show
  1. src/app/api/urls.py +6 -1
  2. src/app/api/views.py +14 -3
src/app/api/urls.py CHANGED
@@ -1,6 +1,6 @@
1
  from django.urls import path
2
  from . import views
3
- from .views import ProductAPIView
4
 
5
  urlpatterns = [
6
  path('', views.catalouge_page, name='index'),
@@ -10,4 +10,9 @@ urlpatterns = [
10
  path('upload/', views.upload_image_and_audio, name='add_product_by_image'),
11
  path('api/products/', ProductAPIView.as_view(), name='product_api'),
12
  path('api/delete_product/<int:product_id>/', views.delete_product_api, name='delete_product_api'),
 
 
 
 
 
13
  ]
 
1
  from django.urls import path
2
  from . import views
3
+ from .views import ProductAPIView,SearchProducts,TotalNumberOfProducts,ProductDetailsById,UpdateProduct
4
 
5
  urlpatterns = [
6
  path('', views.catalouge_page, name='index'),
 
10
  path('upload/', views.upload_image_and_audio, name='add_product_by_image'),
11
  path('api/products/', ProductAPIView.as_view(), name='product_api'),
12
  path('api/delete_product/<int:product_id>/', views.delete_product_api, name='delete_product_api'),
13
+
14
+ path('api/search_products/', SearchProducts.as_view(), name='search_products'),
15
+ path('api/total_number_of_products/', TotalNumberOfProducts.as_view(), name='total_number_of_products'),
16
+ path('api/product_details/<int:pk>/', ProductDetailsById.as_view(), name='product_details_by_id'),
17
+ path('api/update_product/<int:pk>/', UpdateProduct.as_view(), name='update_product'),
18
  ]
src/app/api/views.py CHANGED
@@ -43,13 +43,11 @@ def edit_product(request, product_id):
43
 
44
  if request.method == 'POST':
45
  # Update the product fields with the submitted data
 
46
  product.barcode = request.POST.get('barcode')
47
  product.brand = request.POST.get('brand')
48
  product.sub_brand = request.POST.get('sub_brand')
49
  product.manufacturer = request.POST.get('manufacturer')
50
- # Update other fields similarly
51
-
52
- # Save the updated product
53
  product.save()
54
 
55
  # Redirect to the product page after saving
@@ -160,5 +158,18 @@ class ProductDetailsById(APIView):
160
  product = Product.objects.get(pk=pk)
161
  product_serializer = ProductSerializer(product)
162
  return Response(product_serializer.data)
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  except Product.DoesNotExist:
164
  return Response({"error": "Product not found"}, status=404)
 
43
 
44
  if request.method == 'POST':
45
  # Update the product fields with the submitted data
46
+ product = Product.objects.get(pk=product_id)
47
  product.barcode = request.POST.get('barcode')
48
  product.brand = request.POST.get('brand')
49
  product.sub_brand = request.POST.get('sub_brand')
50
  product.manufacturer = request.POST.get('manufacturer')
 
 
 
51
  product.save()
52
 
53
  # Redirect to the product page after saving
 
158
  product = Product.objects.get(pk=pk)
159
  product_serializer = ProductSerializer(product)
160
  return Response(product_serializer.data)
161
+ except Product.DoesNotExist:
162
+ return Response({"error": "Product not found"}, status=404)
163
+
164
+ class UpdateProduct(APIView):
165
+ def put(self, request, pk):
166
+ try:
167
+ product = Product.objects.get(pk=pk)
168
+ serializer = ProductSerializer(product, data=request.data)
169
+ print(serializer.is_valid(), serializer.errors)
170
+ if serializer.is_valid():
171
+ serializer.save()
172
+ return Response(serializer.data)
173
+ return Response(serializer.errors, status=400)
174
  except Product.DoesNotExist:
175
  return Response({"error": "Product not found"}, status=404)