2020-03-23 01:55:25 +00:00
|
|
|
from typing import *
|
2020-06-26 14:13:11 +00:00
|
|
|
import royalnet.constellation.api as rca
|
|
|
|
import royalnet.utils as ru
|
2020-03-23 01:55:25 +00:00
|
|
|
from ..tables import *
|
|
|
|
from sqlalchemy import func
|
|
|
|
|
|
|
|
|
2020-06-26 14:13:11 +00:00
|
|
|
class ApiDiarioRandomStar(rca.ApiStar):
|
2020-03-23 01:55:25 +00:00
|
|
|
path = "/api/diario/random/v1"
|
|
|
|
|
|
|
|
parameters = {
|
2020-06-22 17:27:11 +00:00
|
|
|
"get": {
|
|
|
|
"amount": "The number of diario entries to get."
|
|
|
|
}
|
2020-03-23 01:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tags = ["diario"]
|
|
|
|
|
2020-06-26 14:13:11 +00:00
|
|
|
@rca.magic
|
|
|
|
async def get(self, data: rca.ApiData) -> ru.JSON:
|
2020-06-22 17:27:11 +00:00
|
|
|
"""Get random diario entries."""
|
2020-03-23 01:55:25 +00:00
|
|
|
DiarioT = self.alchemy.get(Diario)
|
|
|
|
try:
|
|
|
|
amount = int(data["amount"])
|
|
|
|
except ValueError:
|
2020-06-26 14:13:11 +00:00
|
|
|
raise rca.InvalidParameterError("'amount' is not a valid int.")
|
|
|
|
entries: List[Diario] = await ru.asyncify(
|
2020-03-23 01:55:25 +00:00
|
|
|
data.session
|
|
|
|
.query(DiarioT)
|
|
|
|
.order_by(func.random())
|
|
|
|
.limit(amount)
|
|
|
|
.all
|
|
|
|
)
|
|
|
|
if len(entries) < amount:
|
2020-06-26 14:13:11 +00:00
|
|
|
raise rca.NotFoundError("Not enough diario entries.")
|
2020-03-23 01:55:25 +00:00
|
|
|
return list(map(lambda e: e.json(), entries))
|