151 lines
4.0 KiB
Python
151 lines
4.0 KiB
Python
from fastapi import (
|
|
APIRouter,
|
|
Body,
|
|
Depends,
|
|
Form,
|
|
HTTPException,
|
|
Request,
|
|
Response,
|
|
status,
|
|
)
|
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncConnection
|
|
|
|
from api.db.connection.session import get_connection_dep
|
|
|
|
from api.db.logic.keyring import get_key_id,create_key,update_key_id
|
|
|
|
|
|
from api.schemas.account.account import Status
|
|
from api.schemas.endpoints.account_keyring import AccountKeyringUpdate
|
|
|
|
from api.schemas.account.account_keyring import AccountKeyring
|
|
|
|
from api.services.user_role_validation import db_user_role_validation
|
|
from api.services.update_data_validation import update_key_data_changes
|
|
|
|
|
|
api_router = APIRouter(
|
|
prefix="/keyring",
|
|
tags=["User KeyringModel"],
|
|
)
|
|
|
|
|
|
@api_router.get("/{user_id}/{key_id}")
|
|
async def get_keyring(
|
|
key_id: str,
|
|
request: Request,
|
|
connection: AsyncConnection = Depends(get_connection_dep)
|
|
):
|
|
|
|
current_user = request.state.current_user
|
|
|
|
authorize_user = await db_user_role_validation(connection, current_user)
|
|
|
|
keyring = await get_key_id(connection, key_id)
|
|
|
|
if keyring is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Key not found")
|
|
|
|
return keyring
|
|
|
|
|
|
@api_router.post("/{user_id}/{key_id}")
|
|
async def create_keyring(
|
|
user_id: int,
|
|
key_id: str,
|
|
request: Request,
|
|
key: AccountKeyringUpdate,
|
|
connection: AsyncConnection = Depends(get_connection_dep)
|
|
):
|
|
|
|
current_user = request.state.current_user
|
|
|
|
authorize_user = await db_user_role_validation(connection, current_user)
|
|
|
|
keyring = await get_key_id(connection, key_id)
|
|
|
|
if keyring is None:
|
|
user_new = await create_key(connection,key, key_id, )
|
|
return user_new
|
|
|
|
else:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="An keyring with this information already exists.")
|
|
|
|
|
|
|
|
@api_router.put("/{user_id}/{key_id}")
|
|
async def update_keyring(
|
|
user_id: int,
|
|
key_id: str,
|
|
request: Request,
|
|
keyring_update: AccountKeyringUpdate,
|
|
connection: AsyncConnection = Depends(get_connection_dep)
|
|
):
|
|
|
|
current_user = request.state.current_user
|
|
|
|
authorize_user = await db_user_role_validation(connection, current_user)
|
|
|
|
|
|
keyring = await get_key_id(connection, key_id)
|
|
if keyring is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="keyring not found")
|
|
|
|
|
|
update_values = update_key_data_changes(keyring_update,keyring)
|
|
|
|
if update_values is None:
|
|
return keyring
|
|
|
|
keyring_update_data = AccountKeyring.model_validate({**keyring.model_dump(), **update_values})
|
|
|
|
|
|
await update_key_id(connection, update_values, keyring)
|
|
|
|
|
|
keyring = await get_key_id(connection, key_id)
|
|
|
|
return keyring
|
|
|
|
@api_router.delete("/{user_id}/{key_id}")
|
|
async def delete_keyring(
|
|
user_id: int,
|
|
key_id: str,
|
|
request: Request,
|
|
connection: AsyncConnection = Depends(get_connection_dep)
|
|
):
|
|
|
|
current_user = request.state.current_user
|
|
|
|
authorize_user = await db_user_role_validation(connection, current_user)
|
|
|
|
|
|
keyring = await get_key_id(connection, key_id)
|
|
if keyring is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="keyring not found")
|
|
|
|
|
|
keyring_update = AccountKeyringUpdate(status=Status.DELETED.value)
|
|
|
|
update_values = update_key_data_changes(keyring_update,keyring)
|
|
|
|
if update_values is None:
|
|
return keyring
|
|
|
|
await update_key_id(connection, update_values, keyring)
|
|
|
|
|
|
keyring = await get_key_id(connection, key_id)
|
|
|
|
return keyring
|