59 lines
2.2 KiB
Python
59 lines
2.2 KiB
Python
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, Depends, Query
|
|
|
|
from app.core.deps import CurrentUser, DbDep, require_perm
|
|
from app.core.exceptions import ok
|
|
from app.schemas.platform_account import PlatformAccountCreate, PlatformAccountUpdate
|
|
from app.services import platform_account_service as pa_service
|
|
|
|
router = APIRouter(prefix="/platform-accounts", tags=["platform-account"])
|
|
|
|
|
|
@router.get("", dependencies=[Depends(require_perm("platform-account:view"))])
|
|
async def list_accounts(
|
|
db: DbDep,
|
|
page: int = Query(1, ge=1),
|
|
size: int = Query(20, ge=1, le=200),
|
|
keyword: str | None = None,
|
|
platform_name: str | None = None,
|
|
owner_id: int | None = None,
|
|
) -> dict:
|
|
rows, total = await pa_service.paginate(db, page, size, keyword, platform_name, owner_id)
|
|
return ok({
|
|
"list": [(await pa_service.to_read(db, a)).model_dump() for a in rows],
|
|
"total": total,
|
|
"page": page,
|
|
"size": size,
|
|
})
|
|
|
|
|
|
@router.get("/{aid}", dependencies=[Depends(require_perm("platform-account:view"))])
|
|
async def get_account(aid: int, db: DbDep) -> dict:
|
|
acc = await pa_service.get(db, aid)
|
|
return ok((await pa_service.to_read(db, acc)).model_dump())
|
|
|
|
|
|
@router.post("", dependencies=[Depends(require_perm("platform-account:create"))])
|
|
async def create_account(data: PlatformAccountCreate, db: DbDep, user: CurrentUser) -> dict:
|
|
acc = await pa_service.create(db, owner_id=user.id, data=data)
|
|
return ok((await pa_service.to_read(db, acc)).model_dump())
|
|
|
|
|
|
@router.patch("/{aid}", dependencies=[Depends(require_perm("platform-account:update"))])
|
|
async def update_account(aid: int, data: PlatformAccountUpdate, db: DbDep) -> dict:
|
|
acc = await pa_service.update(db, aid, data)
|
|
return ok((await pa_service.to_read(db, acc)).model_dump())
|
|
|
|
|
|
@router.delete("/{aid}", dependencies=[Depends(require_perm("platform-account:delete"))])
|
|
async def delete_account(aid: int, db: DbDep) -> dict:
|
|
await pa_service.delete(db, aid)
|
|
return ok()
|
|
|
|
|
|
@router.post("/{aid}/test", dependencies=[Depends(require_perm("platform-account:test"))])
|
|
async def test_account(aid: int, db: DbDep) -> dict:
|
|
acc = await pa_service.test_connection(db, aid)
|
|
return ok((await pa_service.to_read(db, acc)).model_dump())
|