Spaces:
No application file
No application file
| from fastapi import APIRouter, Depends, HTTPException, status | |
| from sqlalchemy.orm import Session | |
| from typing import List | |
| from app.database import get_db | |
| from app.schemas.item import Item, ItemCreate | |
| import app.crud as crud | |
| router = APIRouter( | |
| prefix="/api/items", | |
| tags=["items"], | |
| ) | |
| # CREATE operation | |
| def create_item(item: ItemCreate, db: Session = Depends(get_db)): | |
| """Create a new item""" | |
| return crud.create_item(db=db, item=item) | |
| from fastapi import Header | |
| def read_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db), api_key: str = Header(..., alias="api_key")): | |
| """Get all items with pagination""" | |
| return crud.get_items(db=db, skip=skip, limit=limit, api_key=api_key) | |
| def read_item(item_id: int, db: Session = Depends(get_db)): | |
| """Get a specific item by ID""" | |
| db_item = crud.get_item(db=db, item_id=item_id) | |
| if db_item is None: | |
| raise HTTPException(status_code=404, detail="Item not found") | |
| return db_item | |
| # UPDATE operation | |
| def update_item(item_id: int, item: ItemCreate, db: Session = Depends(get_db)): | |
| """Update an existing item""" | |
| db_item = crud.update_item(db=db, item_id=item_id, item=item) | |
| if db_item is None: | |
| raise HTTPException(status_code=404, detail="Item not found") | |
| return db_item | |
| # DELETE operation | |
| def delete_item(item_id: int, db: Session = Depends(get_db)): | |
| """Delete a specific item""" | |
| success = crud.delete_item(db=db, item_id=item_id) | |
| if not success: | |
| raise HTTPException(status_code=404, detail="Item not found") | |
| return None |