Initial commit: Product Slip Manager
This commit is contained in:
65
slips/api.py
Normal file
65
slips/api.py
Normal file
@@ -0,0 +1,65 @@
|
||||
from typing import List, Optional
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.utils import timezone
|
||||
from ninja import NinjaAPI, Query
|
||||
|
||||
from .models import Product, Order
|
||||
from .schemas import ProductOut, ProductIn, OrderOut, OrderIn, OrderStatusIn
|
||||
|
||||
api = NinjaAPI(title='Product Slip API', version='1.0.0', urls_namespace='slips_api')
|
||||
|
||||
|
||||
# ─── Products ────────────────────────────────────────────
|
||||
|
||||
@api.get('/products', response=List[ProductOut], tags=['Products'])
|
||||
def list_products(request):
|
||||
return Product.objects.all()
|
||||
|
||||
|
||||
@api.post('/products', response={201: ProductOut}, tags=['Products'])
|
||||
def create_product(request, payload: ProductIn):
|
||||
product = Product.objects.create(**payload.dict())
|
||||
return 201, product
|
||||
|
||||
|
||||
# ─── Orders ──────────────────────────────────────────────
|
||||
|
||||
@api.get('/orders', response=List[OrderOut], tags=['Orders'])
|
||||
def list_orders(request, status: Optional[str] = Query(None)):
|
||||
qs = Order.objects.select_related('product').all()
|
||||
if status:
|
||||
qs = qs.filter(status=status)
|
||||
return qs
|
||||
|
||||
|
||||
@api.post('/orders', response={201: OrderOut}, tags=['Orders'])
|
||||
def create_order(request, payload: OrderIn):
|
||||
product = get_object_or_404(Product, id=payload.product_id)
|
||||
order = Order(
|
||||
product=product,
|
||||
batch_number=payload.batch_number,
|
||||
production_date=payload.production_date,
|
||||
next_control_date=payload.next_control_date,
|
||||
total_weight=payload.total_weight,
|
||||
canister_weight=payload.canister_weight,
|
||||
canisters_per_box=payload.canisters_per_box,
|
||||
)
|
||||
order.save()
|
||||
return 201, order
|
||||
|
||||
|
||||
@api.patch('/orders/{order_id}', response=OrderOut, tags=['Orders'])
|
||||
def update_order_status(request, order_id: int, payload: OrderStatusIn):
|
||||
order = get_object_or_404(Order, id=order_id)
|
||||
order.status = payload.status
|
||||
if payload.status == 'done':
|
||||
order.printed_at = timezone.now()
|
||||
order.save()
|
||||
return order
|
||||
|
||||
|
||||
@api.delete('/orders/{order_id}', response={204: None}, tags=['Orders'])
|
||||
def delete_order(request, order_id: int):
|
||||
order = get_object_or_404(Order, id=order_id)
|
||||
order.delete()
|
||||
return 204, None
|
||||
Reference in New Issue
Block a user