bbs-backend/apis/main/get_tips.py
2023-04-23 16:01:46 +08:00

27 lines
687 B
Python

import secrets
from defs import app
from models.services.tip import TipAction
from typing import Dict, Optional, List
system_random = secrets.SystemRandom()
async def get_random_tips() -> List[Optional[Dict]]:
tips = await TipAction.get_tips()
if not tips:
return None
if len(tips) < 4:
return [tip.dict_tip() for tip in tips]
data = []
while len(data) < 4:
num = system_random.randint(0, len(tips) - 1)
if tips[num] not in data:
data.append(tips[num])
return [tip.dict_tip() for tip in data]
@app.get("/get_tips")
async def get_tips():
return {"code": 200, "msg": "success", "data": await get_random_tips()}