from typing import Any, List from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session from app import crud, models, schemas from app.api import deps router = APIRouter() @router.get("/", response_model=List[schemas.NftPrint]) # @router.get("/") def read_items( db: Session = Depends(deps.get_db), skip: int = 0, limit: int = 100, current_user: models.users = Depends(deps.get_current_active_superuser), ) -> Any: """ Retrieve items. """ if crud.user.is_superuser(current_user): nfts = crud.nft.get_group_by_title(db, skip=skip, limit=limit) else: nfts = crud.nft.get_multi_by_owner( db=db, owner_id=current_user.userid, skip=skip, limit=limit ) return nfts @router.post("/", response_model=schemas.NftBase) def create_item( *, db: Session = Depends(deps.get_db), item_in: schemas.NftCreate, current_user: models.users = Depends(deps.get_current_active_user), ) -> Any: """ Create new item. """ nft = crud.nft.create_with_owner( db=db, obj_in=item_in, owner_id=current_user.userid) return nft @router.put("/{id}", response_model=schemas.NftCreate) def update_item( *, db: Session = Depends(deps.get_db), id: int, item_in: schemas.NftUpdate, current_user: models.users = Depends(deps.get_current_active_user), ) -> Any: """ Update an item. """ nft = crud.nft.get(db=db, id=id) if not nft: raise HTTPException(status_code=404, detail="Item not found") if not crud.user.is_superuser(current_user) and (nft.userid != current_user.userid): raise HTTPException(status_code=400, detail="Not enough permissions") nft = crud.nft.update(db=db, db_obj=nft, obj_in=item_in) return nft @router.get("/{id}", response_model=schemas.NftCreate) def read_item( *, db: Session = Depends(deps.get_db), id: int, current_user: models.users = Depends(deps.get_current_active_user), ) -> Any: """ Get item by ID. """ nft = crud.nft.get(db=db, id=id) if not nft: raise HTTPException(status_code=404, detail="Item not found") if not crud.user.is_superuser(current_user) and (nft.userid != current_user.userid): raise HTTPException(status_code=400, detail="Not enough permissions") return nft # @router.post("/title", response_model=schemas.NftCreate) @router.post("/title") def read_user_by_title( *, db: Session = Depends(deps.get_db), title: str, skip: int = 0, limit: int = 100, current_user: models.users = Depends(deps.get_current_active_user), ) -> Any: """ Get item by title. """ nft = crud.nft.get_user_by_title(db=db, title=title, skip=skip, limit=limit) if not nft: raise HTTPException(status_code=404, detail="Item not found") if not crud.user.is_superuser(current_user) and (nft.userid != current_user.userid): raise HTTPException(status_code=400, detail="Not enough permissions") return nft @router.delete("/{id}", response_model=schemas.NftCreate) def delete_item( *, db: Session = Depends(deps.get_db), id: int, current_user: models.users = Depends(deps.get_current_active_user), ) -> Any: """ Delete an item. """ nft = crud.nft.get(db=db, id=id) if not nft: raise HTTPException(status_code=404, detail="Item not found") if not crud.user.is_superuser(current_user) and (nft.userid != current_user.userid): raise HTTPException(status_code=400, detail="Not enough permissions") nft = crud.nft.remove(db=db, id=id) return nft