97 lines
3 KiB
Python
97 lines
3 KiB
Python
|
from uuid import UUID
|
||
|
from fastapi import APIRouter, HTTPException, Security
|
||
|
from fastapi.exceptions import RequestValidationError
|
||
|
from fastapi.params import Depends
|
||
|
from sqlalchemy.orm import Session
|
||
|
|
||
|
from app.database import get_db
|
||
|
from app.schemas.id_token import IdToken, IdTokenCreate, IdTokenUpdate
|
||
|
from app.models.id_token import IdToken as DbIdToken
|
||
|
from app.models.user import User as DbUser
|
||
|
from app.security import get_api_key
|
||
|
|
||
|
router = APIRouter(
|
||
|
prefix="/id-tokens",
|
||
|
tags=["IdToken (v1)"]
|
||
|
)
|
||
|
|
||
|
@router.get(path="", response_model=list[IdToken])
|
||
|
async def get_it_tokens(
|
||
|
skip: int = 0,
|
||
|
limit: int = 20,
|
||
|
api_key: str = Security(get_api_key),
|
||
|
db: Session = Depends(get_db)
|
||
|
):
|
||
|
return db.query(DbIdToken).offset(skip).limit(limit).all()
|
||
|
|
||
|
@router.get(path="/{id_token_id}", response_model=IdToken)
|
||
|
async def get_id_token(
|
||
|
id_token_id: UUID,
|
||
|
api_key: str = Security(get_api_key),
|
||
|
db: Session = Depends(get_db)
|
||
|
):
|
||
|
id_token = db.get(DbIdToken, id_token_id)
|
||
|
if id_token == None:
|
||
|
raise HTTPException(status_code=404, detail="IdToken not found")
|
||
|
return id_token
|
||
|
|
||
|
@router.post(path="", status_code=201, response_model=IdToken)
|
||
|
async def create_id_token(
|
||
|
create_id_token: IdTokenCreate,
|
||
|
api_key: str = Security(get_api_key),
|
||
|
db: Session = Depends(get_db)
|
||
|
):
|
||
|
owner = db.get(DbUser, create_id_token.owner_id)
|
||
|
if owner == None:
|
||
|
raise HTTPException(status_code=422, detail=[{
|
||
|
"loc": ["body", "owner_id"],
|
||
|
"msg": "Owner not found",
|
||
|
"type": "invalid_relation"
|
||
|
}])
|
||
|
id_token = DbIdToken(
|
||
|
friendly_name=create_id_token.friendly_name,
|
||
|
is_active=create_id_token.is_active,
|
||
|
token=create_id_token.token,
|
||
|
owner_id=create_id_token.owner_id
|
||
|
)
|
||
|
db.add(id_token)
|
||
|
db.commit()
|
||
|
db.refresh(id_token)
|
||
|
return id_token
|
||
|
|
||
|
@router.patch(path="/{id_token_id}", response_model=IdToken)
|
||
|
async def update_id_token(
|
||
|
id_token_id: UUID,
|
||
|
id_token_update: IdTokenUpdate,
|
||
|
api_key: str = Security(get_api_key),
|
||
|
db: Session = Depends(get_db)
|
||
|
):
|
||
|
id_token = db.get(DbIdToken, id_token_id)
|
||
|
if id_token is None:
|
||
|
raise HTTPException(status_code=404, detail="IdToken not found")
|
||
|
for key, value in id_token_update.model_dump(exclude_unset=True).items():
|
||
|
if key == "owner_id":
|
||
|
owner = db.get(DbUser, value)
|
||
|
if owner == None:
|
||
|
raise HTTPException(status_code=422, detail=[{
|
||
|
"loc": ["body", "owner_id"],
|
||
|
"msg": "Owner not found",
|
||
|
"type": "invalid_relation"
|
||
|
}])
|
||
|
setattr(id_token, key, value)
|
||
|
db.commit()
|
||
|
return id_token
|
||
|
|
||
|
@router.delete(path="/{id_token_id}", response_model=None)
|
||
|
async def delete_id_token(
|
||
|
id_token_id: UUID,
|
||
|
api_key: str = Security(get_api_key),
|
||
|
db: Session = Depends(get_db)
|
||
|
):
|
||
|
id_token = db.get(DbIdToken, id_token_id)
|
||
|
if id_token == None:
|
||
|
raise HTTPException(status_code=404, detail="IdToken not found")
|
||
|
db.delete(id_token)
|
||
|
db.commit()
|
||
|
return []
|