from http import HTTPStatus
from typing import List

from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from sqlmodel import select
from starlette.responses import Response

from icon_governance.db import get_session
from icon_governance.models.preps import Prep

router = APIRouter()


@router.get("/preps")
async def get_preps(
    session: AsyncSession = Depends(get_session),
) -> List[Prep]:
    """Return list of preps which is limitted to 150 records so no skip."""
    result = await session.execute(select(Prep).order_by(Prep.delegated.desc()))
    preps = result.scalars().all()

    return preps


@router.get("/preps/{address}")
async def get_prep(
    address: str,
    session: AsyncSession = Depends(get_session),
) -> List[Prep]:
    """Return a single prep."""
    result = await session.execute(select(Prep).where(Prep.address == address))
    preps = result.scalars().all()

    if len(preps) == 0:
        return Response(status_code=HTTPStatus.NO_CONTENT.value)

    return preps

starlette.responses.Response fastapi.Depends sqlmodel.select icon_governance.models.preps.Prep.delegated.desc fastapi.APIRouter
[((346, 357), 'fastapi.APIRouter', 'APIRouter', ([], {}), '()\n', (355, 357), False, 'from fastapi import APIRouter, Depends\n'), ((431, 451), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (438, 451), False, 'from fastapi import APIRouter, Depends\n'), ((780, 800), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (787, 800), False, 'from fastapi import APIRouter, Depends\n'), ((1006, 1055), 'starlette.responses.Response', 'Response', ([], {'status_code': 'HTTPStatus.NO_CONTENT.value'}), '(status_code=HTTPStatus.NO_CONTENT.value)\n', (1014, 1055), False, 'from starlette.responses import Response\n'), ((603, 624), 'icon_governance.models.preps.Prep.delegated.desc', 'Prep.delegated.desc', ([], {}), '()\n', (622, 624), False, 'from icon_governance.models.preps import Prep\n'), ((581, 593), 'sqlmodel.select', 'select', (['Prep'], {}), '(Prep)\n', (587, 593), False, 'from sqlmodel import select\n'), ((886, 898), 'sqlmodel.select', 'select', (['Prep'], {}), '(Prep)\n', (892, 898), False, 'from sqlmodel import select\n')]