1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-24 12:04:20 +00:00
royalnet/royalpack/stars/api_diario_list.py

36 lines
1.2 KiB
Python
Raw Normal View History

2019-11-11 08:56:08 +00:00
from starlette.responses import *
2020-02-08 00:49:07 +00:00
from royalnet.constellation.api import *
2019-11-11 08:56:08 +00:00
from royalnet.utils import *
from ..tables import *
2019-11-11 08:56:08 +00:00
2020-02-08 00:49:07 +00:00
class ApiDiarioListStar(ApiStar):
path = "/api/diario/list/v1"
2019-11-11 08:56:08 +00:00
2020-02-08 00:49:07 +00:00
async def api(self, data: ApiData) -> dict:
page_str = data["page"]
2019-11-11 08:56:08 +00:00
try:
page = int(page_str)
2020-02-08 00:49:07 +00:00
except ValueError:
raise InvalidParameterError("'page' is not a valid int.")
if page < 0:
page = -page-1
entries: typing.List[Diario] = await asyncify(
data.session
.query(self.alchemy.get(Diario))
.order_by(self.alchemy.get(Diario).diario_id.desc()).limit(500)
.offset(page * 500)
.all
)
else:
entries: typing.List[Diario] = await asyncify(
data.session
.query(self.alchemy.get(Diario))
.order_by(self.alchemy.get(Diario).diario_id)
.limit(500)
.offset(page * 500)
.all
)
response = [entry.json() for entry in entries]
return response