
- Create project structure with FastAPI and SQLAlchemy - Implement database models for items, categories, suppliers, and stock movements - Add CRUD operations for all models - Configure Alembic for database migrations - Create RESTful API endpoints for inventory management - Add health endpoint for system monitoring - Update README with setup and usage instructions generated with BackendIM... (backend.im)
64 lines
2.1 KiB
Python
64 lines
2.1 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
|
from sqlalchemy.orm import Session
|
|
from typing import List, Optional
|
|
from app.database import get_db
|
|
from app.schemas.item import ItemCreate, ItemUpdate, ItemResponse, ItemDetailResponse
|
|
from app.crud import item as item_crud
|
|
|
|
router = APIRouter(
|
|
prefix="/items",
|
|
tags=["Items"]
|
|
)
|
|
|
|
@router.get("/", response_model=List[ItemResponse])
|
|
def get_items(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
category_id: Optional[int] = None,
|
|
supplier_id: Optional[int] = None,
|
|
include_inactive: bool = False,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
return item_crud.get_items(
|
|
db,
|
|
skip=skip,
|
|
limit=limit,
|
|
category_id=category_id,
|
|
supplier_id=supplier_id,
|
|
include_inactive=include_inactive
|
|
)
|
|
|
|
@router.get("/{item_id}", response_model=ItemDetailResponse)
|
|
def get_item(item_id: int, db: Session = Depends(get_db)):
|
|
db_item = item_crud.get_item(db, item_id=item_id)
|
|
if db_item is None:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Item not found")
|
|
|
|
# Convert related objects to dict for response
|
|
response = ItemDetailResponse.model_validate(db_item)
|
|
|
|
if db_item.category:
|
|
response.category = {
|
|
"id": db_item.category.id,
|
|
"name": db_item.category.name
|
|
}
|
|
|
|
if db_item.supplier:
|
|
response.supplier = {
|
|
"id": db_item.supplier.id,
|
|
"name": db_item.supplier.name
|
|
}
|
|
|
|
return response
|
|
|
|
@router.post("/", response_model=ItemResponse, status_code=status.HTTP_201_CREATED)
|
|
def create_item(item: ItemCreate, db: Session = Depends(get_db)):
|
|
return item_crud.create_item(db=db, item=item)
|
|
|
|
@router.put("/{item_id}", response_model=ItemResponse)
|
|
def update_item(item_id: int, item: ItemUpdate, db: Session = Depends(get_db)):
|
|
return item_crud.update_item(db=db, item_id=item_id, item=item)
|
|
|
|
@router.delete("/{item_id}")
|
|
def delete_item(item_id: int, db: Session = Depends(get_db)):
|
|
return item_crud.delete_item(db=db, item_id=item_id) |