
- Set up project structure for FastAPI application - Create database models for items, categories, suppliers, and transactions - Set up Alembic for database migrations - Implement API endpoints for all entities - Add authentication with JWT tokens - Add health check endpoint - Create comprehensive README with documentation
126 lines
3.2 KiB
Python
126 lines
3.2 KiB
Python
from typing import Any, List, Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import get_current_active_user, get_db
|
|
from app.models.item import Item
|
|
from app.models.user import User
|
|
from app.schemas.item import Item as ItemSchema, ItemCreate, ItemUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[ItemSchema])
|
|
def read_items(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
category_id: Optional[int] = None,
|
|
supplier_id: Optional[int] = None,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve inventory items.
|
|
"""
|
|
query = db.query(Item)
|
|
|
|
# Filter by owner
|
|
query = query.filter(Item.owner_id == current_user.id)
|
|
|
|
# Apply additional filters if provided
|
|
if category_id:
|
|
query = query.filter(Item.category_id == category_id)
|
|
if supplier_id:
|
|
query = query.filter(Item.supplier_id == supplier_id)
|
|
|
|
items = query.offset(skip).limit(limit).all()
|
|
return items
|
|
|
|
|
|
@router.post("/", response_model=ItemSchema)
|
|
def create_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
item_in: ItemCreate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Create new item.
|
|
"""
|
|
item = Item(
|
|
**item_in.dict(),
|
|
owner_id=current_user.id
|
|
)
|
|
db.add(item)
|
|
db.commit()
|
|
db.refresh(item)
|
|
return item
|
|
|
|
|
|
@router.put("/{item_id}", response_model=ItemSchema)
|
|
def update_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
item_id: int,
|
|
item_in: ItemUpdate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Update an item.
|
|
"""
|
|
item = db.query(Item).filter(Item.id == item_id, Item.owner_id == current_user.id).first()
|
|
if not item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Item not found",
|
|
)
|
|
|
|
update_data = item_in.dict(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(item, field, value)
|
|
|
|
db.add(item)
|
|
db.commit()
|
|
db.refresh(item)
|
|
return item
|
|
|
|
|
|
@router.get("/{item_id}", response_model=ItemSchema)
|
|
def read_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
item_id: int,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get item by ID.
|
|
"""
|
|
item = db.query(Item).filter(Item.id == item_id, Item.owner_id == current_user.id).first()
|
|
if not item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Item not found",
|
|
)
|
|
return item
|
|
|
|
|
|
@router.delete("/{item_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
item_id: int,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Delete an item.
|
|
"""
|
|
item = db.query(Item).filter(Item.id == item_id, Item.owner_id == current_user.id).first()
|
|
if not item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Item not found",
|
|
)
|
|
db.delete(item)
|
|
db.commit()
|
|
return None |