diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 218660a..2bf8ba6 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -5,14 +5,25 @@
+
+
-
+
+
+
+
+
+
@@ -89,7 +100,7 @@
1709421008833
-
+
diff --git a/db.sqlite3 b/db.sqlite3
index 35e8709..fb42da0 100644
Binary files a/db.sqlite3 and b/db.sqlite3 differ
diff --git a/products/migrations/0001_initial.py b/products/migrations/0001_initial.py
new file mode 100644
index 0000000..d81ce8e
--- /dev/null
+++ b/products/migrations/0001_initial.py
@@ -0,0 +1,48 @@
+# Generated by Django 5.0.2 on 2024-03-03 01:24
+
+import django.db.models.deletion
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ initial = True
+
+ dependencies = []
+
+ operations = [
+ migrations.CreateModel(
+ name="Brand",
+ fields=[
+ ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
+ ("name", models.CharField(max_length=100)),
+ ("description", models.TextField(blank=True)),
+ ],
+ ),
+ migrations.CreateModel(
+ name="Supplier",
+ fields=[
+ ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
+ ("name", models.CharField(max_length=100)),
+ ("email", models.EmailField(max_length=254)),
+ ("phone", models.CharField(max_length=20)),
+ ],
+ ),
+ migrations.CreateModel(
+ name="Product",
+ fields=[
+ ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
+ ("name", models.CharField(max_length=100)),
+ ("description", models.TextField()),
+ ("sku", models.CharField(max_length=50)),
+ ("price", models.DecimalField(decimal_places=2, max_digits=10)),
+ ("quantity", models.IntegerField(default=0)),
+ ("barcode", models.CharField(blank=True, max_length=100, null=True)),
+ ("brand", models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to="products.brand")),
+ (
+ "supplier",
+ models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to="products.supplier"),
+ ),
+ ],
+ ),
+ ]
diff --git a/products/serializers.py b/products/serializers.py
new file mode 100644
index 0000000..21684ec
--- /dev/null
+++ b/products/serializers.py
@@ -0,0 +1,21 @@
+from rest_framework import serializers
+
+from .models import Brand, Product, Supplier
+
+
+class BrandSerializer(serializers.ModelSerializer):
+ class Meta:
+ model = Brand
+ fields = "__all__"
+
+
+class SupplierSerializer(serializers.ModelSerializer):
+ class Meta:
+ model = Supplier
+ fields = "__all__"
+
+
+class ProductSerializer(serializers.ModelSerializer):
+ class Meta:
+ model = Product
+ fields = "__all__"
diff --git a/products/urls.py b/products/urls.py
new file mode 100644
index 0000000..473ea04
--- /dev/null
+++ b/products/urls.py
@@ -0,0 +1,8 @@
+from django.urls import include, path
+from rest_framework.routers import DefaultRouter
+
+from .views import BrandViewSet
+
+router = DefaultRouter()
+router.register(r"Brands", BrandViewSet)
+urlpatterns = router.urls
diff --git a/products/views.py b/products/views.py
index 91ea44a..e358358 100644
--- a/products/views.py
+++ b/products/views.py
@@ -1,3 +1,49 @@
-from django.shortcuts import render
+from rest_framework import status, viewsets
+from rest_framework.response import Response
-# Create your views here.
+from .models import Brand, Product, Supplier
+from .serializers import BrandSerializer, ProductSerializer, SupplierSerializer
+
+
+class BrandViewSet(viewsets.ModelViewSet):
+ queryset = Brand.objects.all()
+ serializer_class = BrandSerializer
+
+ def list(self, request, *args, **kwargs):
+ data = list(Brand.objects.all().values())
+ return Response(data, status=status.HTTP_200_OK)
+
+ def retrieve(self, request, *args, **kwargs):
+ data = list(Brand.objects.filter(id=kwargs["pk"]).values())
+ return Response(data, status=status.HTTP_200_OK)
+
+ def create(self, request, *args, **kwargs):
+ brand_serializer_data = BrandSerializer(data=request.data)
+ if brand_serializer_data.is_valid():
+ brand_serializer_data.save()
+ status_code = status.HTTP_201_CREATED
+ return Response({"message": "Brand added successfully"}, status=status_code)
+ else:
+ status_code = status.HTTP_400_BAD_REQUEST
+ return Response({"message": "please fill the details"}, status=status_code)
+
+ def destroy(self, request, *args, **kwargs):
+ brand_data = Brand.objects.filter(id=kwargs["pk"])
+ if brand_data:
+ brand_data.delete()
+ status_code = status.HTTP_201_CREATED
+ return Response({"message": "Brand deleted succesfully"}, status=status_code)
+ else:
+ status_code = status.HTTP_400_BAD_REQUEST
+ return Response({"message": "Brand not found"}, status=status_code)
+
+ def update(self, request, *args, **kwargs):
+ brand_details = Brand.objects.get(id=kwargs["pk"])
+ brand_serializer_data = BrandSerializer(brand_details, data=request, partial=True)
+ if brand_serializer_data.is_valid():
+ brand_serializer_data.save()
+ status_code = status.HTTP_201_CREATED
+ return Response({"message": "Brand updated successfully"}, status=status_code)
+ else:
+ status_code = status.HTTP_400_BAD_REQUEST
+ return Response({"message": "Brand not found"}, status=status)
diff --git a/store/urls.py b/store/urls.py
index a5c7c1c..09d4b63 100644
--- a/store/urls.py
+++ b/store/urls.py
@@ -37,5 +37,6 @@ schema_view = get_schema_view(
urlpatterns = [
path("admin/", admin.site.urls),
+ path("api/", include("products.urls")),
path("docs/", schema_view.with_ui("swagger", cache_timeout=0), name="schema-swagger-ui"),
]