completed brand API routes

This commit is contained in:
2024-03-03 04:29:28 +03:00
parent 1ed28fdc7d
commit c805d6ee81
7 changed files with 139 additions and 4 deletions
+13 -2
View File
@@ -5,14 +5,25 @@
</component>
<component name="ChangeListManager">
<list default="true" id="03e03f4d-45bc-4bed-b39e-371f1d24898e" name="Changes" comment="remove unnecessary hooks">
<change afterPath="$PROJECT_DIR$/products/serializers.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/products/urls.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/products/models.py" beforeDir="false" afterPath="$PROJECT_DIR$/products/models.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/db.sqlite3" beforeDir="false" afterPath="$PROJECT_DIR$/db.sqlite3" afterDir="false" />
<change beforePath="$PROJECT_DIR$/products/views.py" beforeDir="false" afterPath="$PROJECT_DIR$/products/views.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/store/urls.py" beforeDir="false" afterPath="$PROJECT_DIR$/store/urls.py" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Python Script" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
@@ -89,7 +100,7 @@
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1709421008833</updated>
<workItem from="1709421138204" duration="6199000" />
<workItem from="1709421138204" duration="8081000" />
</task>
<task id="LOCAL-00001" summary="Initial commit">
<option name="closed" value="true" />
BIN
View File
Binary file not shown.
+48
View File
@@ -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"),
),
],
),
]
+21
View File
@@ -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__"
+8
View File
@@ -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
+48 -2
View File
@@ -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)
+1
View File
@@ -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"),
]