from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session from app.api.dependencies.db import get_db from app.crud.crud_item import create_user_item from app.crud.crud_user import get_user_by_email, create_user, get_users, get_user from app.schemas.item import Item, ItemCreate from app.schemas.user import User, UserCreate router = APIRouter() @router.post("/", response_model=User) def create_new_user(user: UserCreate, db: Session = Depends(get_db)): db_user = get_user_by_email(db, email=user.email) if db_user: raise HTTPException(status_code=400, detail="Email already registered") return create_user(db=db, user=user) @router.get("/", response_model=list[User]) def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): users = get_users(db, skip=skip, limit=limit) return users @router.get("/{user_id}", response_model=User) def read_user(user_id: int, db: Session = Depends(get_db)): db_user = get_user(db, user_id=user_id) if db_user is None: raise HTTPException(status_code=404, detail="User not found") return db_user @router.post("/{user_id}/item", response_model=Item) def create_item_for_user(user_id: int, item: ItemCreate, db: Session = Depends(get_db)): return create_user_item(db, item=item, user_id=user_id)