Automated Action 4e60587fda Create simple inventory management app with FastAPI and SQLite
- Set up project structure with FastAPI
- Implement SQLAlchemy models for inventory items and categories
- Create database connection with SQLite
- Add CRUD operations for inventory management
- Implement API endpoints for categories, items, and inventory transactions
- Add health check endpoint
- Set up Alembic for database migrations
- Update README with documentation
2025-06-17 03:54:50 +00:00

77 lines
3.0 KiB
Python

from pydantic import BaseModel, Field, field_validator
from typing import Optional
from datetime import datetime
# Shared properties
class ItemBase(BaseModel):
name: str = Field(..., description="Item name", example="Laptop")
description: Optional[str] = Field(None, description="Item description", example="High-performance laptop")
quantity: int = Field(0, description="Current quantity in stock", example=10, ge=0)
price: Optional[float] = Field(None, description="Item price", example=999.99, ge=0)
sku: Optional[str] = Field(None, description="Stock Keeping Unit", example="LAPTOP-001")
category_id: Optional[int] = Field(None, description="Category ID", example=1)
# Properties to receive via API on creation
class ItemCreate(ItemBase):
pass
# Properties to receive via API on update
class ItemUpdate(BaseModel):
name: Optional[str] = Field(None, description="Item name", example="Laptop")
description: Optional[str] = Field(None, description="Item description", example="High-performance laptop")
quantity: Optional[int] = Field(None, description="Current quantity in stock", example=10)
price: Optional[float] = Field(None, description="Item price", example=999.99)
sku: Optional[str] = Field(None, description="Stock Keeping Unit", example="LAPTOP-001")
category_id: Optional[int] = Field(None, description="Category ID", example=1)
@field_validator("quantity", "price")
@classmethod
def validate_non_negative(cls, v):
if v is not None and v < 0:
raise ValueError("Value cannot be negative")
return v
# Properties shared by models stored in DB
class ItemInDBBase(ItemBase):
id: int = Field(..., description="The unique identifier of the item")
created_at: datetime = Field(..., description="Item creation timestamp")
updated_at: datetime = Field(..., description="Item last update timestamp")
class Config:
from_attributes = True
# Properties to return to client
class Item(ItemInDBBase):
pass
# Properties for transaction
class InventoryTransactionBase(BaseModel):
item_id: int = Field(..., description="Item ID", example=1)
quantity_change: int = Field(..., description="Quantity change (positive for additions, negative for removals)", example=5)
transaction_type: str = Field(..., description="Transaction type", example="addition")
notes: Optional[str] = Field(None, description="Transaction notes", example="Received new shipment")
# Properties to receive via API on creation
class InventoryTransactionCreate(InventoryTransactionBase):
pass
# Properties shared by models stored in DB
class InventoryTransactionInDBBase(InventoryTransactionBase):
id: int = Field(..., description="The unique identifier of the transaction")
timestamp: datetime = Field(..., description="Transaction timestamp")
class Config:
from_attributes = True
# Properties to return to client
class InventoryTransaction(InventoryTransactionInDBBase):
pass