brestok's picture
add account CRUD
aabf8ec
from typing import Optional
from fastapi import Depends, Query
from trauma.api.account import account_router
from trauma.api.account.db_requests import get_all_model_obj, create_account_obj, delete_account
from trauma.api.account.dto import AccountType
from trauma.api.account.model import AccountModel
from trauma.api.account.schemas import AccountWrapper, AllAccountsResponse, CreateAccountRequest
from trauma.api.common.dto import Paging
from trauma.core.security import PermissionDependency
from trauma.core.wrappers import TraumaResponseWrapper
@account_router.get('/all')
async def get_all_accounts(
pageSize: Optional[int] = Query(10, description="Number of countries to return per page"),
pageIndex: Optional[int] = Query(0, description="Page index to retrieve"),
_: AccountModel = Depends(PermissionDependency([AccountType.Admin]))
) -> TraumaResponseWrapper[AllAccountsResponse]:
countries, total_count = await get_all_model_obj(pageSize, pageIndex)
response = AllAccountsResponse(
paging=Paging(pageSize=pageSize, pageIndex=pageIndex, totalCount=total_count),
data=countries
)
return TraumaResponseWrapper(data=response)
@account_router.get('')
async def get_account(
account: AccountModel = Depends(PermissionDependency([AccountType.Admin, AccountType.User]))
) -> AccountWrapper:
return AccountWrapper(data=account)
@account_router.post('')
async def create_account(
account_data: CreateAccountRequest,
_: AccountModel = Depends(PermissionDependency([AccountType.Admin]))
) -> AccountWrapper:
account = await create_account_obj(account_data)
return AccountWrapper(data=account)
@account_router.delete('/{accountId}')
async def update_account(
accountId: str,
_: AccountModel = Depends(PermissionDependency([AccountType.Admin]))
) -> TraumaResponseWrapper:
await delete_account(accountId)
return TraumaResponseWrapper()