
- Implemented user authentication with JWT - Added CRUD operations for users and items - Setup database connection with SQLAlchemy - Added migration scripts for easy database setup - Included health check endpoint for monitoring generated with BackendIM... (backend.im)
124 lines
3.3 KiB
Python
124 lines
3.3 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
|
|
from app.database import get_db
|
|
from app.models import Item, User
|
|
from app.schemas import ItemCreate, ItemUpdate, ItemResponse
|
|
from app.utils.security import get_current_active_user
|
|
|
|
router = APIRouter(prefix="/items", tags=["items"])
|
|
|
|
|
|
@router.post("/", response_model=ItemResponse, status_code=status.HTTP_201_CREATED)
|
|
def create_item(
|
|
item: ItemCreate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
db_item = Item(
|
|
**item.dict(),
|
|
owner_id=current_user.id,
|
|
)
|
|
db.add(db_item)
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
|
|
@router.get("/", response_model=List[ItemResponse])
|
|
def read_items(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user),
|
|
):
|
|
items = db.query(Item).filter(Item.is_active == True).offset(skip).limit(limit).all()
|
|
return items
|
|
|
|
|
|
@router.get("/my-items", response_model=List[ItemResponse])
|
|
def read_my_items(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user),
|
|
):
|
|
items = (
|
|
db.query(Item)
|
|
.filter(Item.owner_id == current_user.id)
|
|
.offset(skip)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
return items
|
|
|
|
|
|
@router.get("/{item_id}", response_model=ItemResponse)
|
|
def read_item(
|
|
item_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user),
|
|
):
|
|
db_item = db.query(Item).filter(Item.id == item_id).first()
|
|
if db_item is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Item not found",
|
|
)
|
|
return db_item
|
|
|
|
|
|
@router.put("/{item_id}", response_model=ItemResponse)
|
|
def update_item(
|
|
item_id: int,
|
|
item: ItemUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user),
|
|
):
|
|
db_item = db.query(Item).filter(Item.id == item_id).first()
|
|
if db_item is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Item not found",
|
|
)
|
|
|
|
# Check ownership
|
|
if db_item.owner_id != current_user.id:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Not enough permissions",
|
|
)
|
|
|
|
# Update item attributes
|
|
for key, value in item.dict(exclude_unset=True).items():
|
|
setattr(db_item, key, value)
|
|
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
|
|
@router.delete("/{item_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
def delete_item(
|
|
item_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user),
|
|
):
|
|
db_item = db.query(Item).filter(Item.id == item_id).first()
|
|
if db_item is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Item not found",
|
|
)
|
|
|
|
# Check ownership
|
|
if db_item.owner_id != current_user.id:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Not enough permissions",
|
|
)
|
|
|
|
db.delete(db_item)
|
|
db.commit()
|
|
return None |