Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.8 kB
3
Indexable
Never
url.py


from django.urls import path
from django.contrib import admin
from inventoryapp.views import (
    InventoryItemListAPIView,
    InventoryItemCategoryListAPIView,
    InventoryItemSortAPIView,
    InventoryItemEditAPIView,
)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('inventory/items/', InventoryItemListAPIView.as_view(), name='inventory_items_list'),
    path('inventory/items/query/<str:category>/', InventoryItemCategoryListAPIView.as_view(), name='inventory_items_by_category'),
    path('items/query/<str:category>/', InventoryItemCategoryListAPIView.as_view(), name='inventory_items_by_category'),
    path('items/sort/', InventoryItemSortAPIView.as_view(), name='inventory_items_sort'),
    path('inventory/items/<int:item_id>/', InventoryItemEditAPIView.as_view(), name='inventory_items_edit_delete'),
]

views.py

from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from inventoryapp.models import Inventory
from inventoryapp.serializer import InventorySerializer

class InventoryItemListAPIView(APIView):
    def get(self, request, *args, **kwargs):
        items = Inventory.objects.all()
        serializer = InventorySerializer(items, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

    def post(self, request, *args, **kwargs):
        serializer = InventorySerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class InventoryItemCategoryListAPIView(APIView):
    def get(self, request, category, *args, **kwargs):
        items = Inventory.objects.filter(category=category)
        serializer = InventorySerializer(items, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

class InventoryItemSortAPIView(APIView):
    def get(self, request, *args, **kwargs):
        items = Inventory.objects.all().order_by('-price')
        serializer = InventorySerializer(items, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

class InventoryItemEditAPIView(APIView):
    def put(self, request, item_id, *args, **kwargs):
        try:
            item = Inventory.objects.get(id=item_id)
        except Inventory.DoesNotExist:
            return Response({'error': 'Item not found'}, status=status.HTTP_404_NOT_FOUND)

        serializer = InventorySerializer(instance=item, data=request.data, partial=True)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def delete(self, request, item_id, *args, **kwargs):
        try:
            item = Inventory.objects.get(id=item_id)
            item.delete()
            return Response(status=status.HTTP_204_NO_CONTENT)
        except Inventory.DoesNotExist:
            return Response({'error': 'Item not found'}, status=status.HTTP_404_NOT_FOUND)

serializer.py

from rest_framework import serializers
from inventoryapp.models import Inventory

class InventorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Inventory
        fields = 'all'

models.py

from django.db import models

class Inventory(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255)
    category = models.CharField(max_length=100)
    price = models.IntegerField()
    quantity = models.IntegerField()
    barcode = models.IntegerField(unique=True)

    def str(self):
        return self.name
Leave a Comment