bbs-backend/apis/main/get_tips.py
2023-04-23 14:08:36 +08:00

21 lines
481 B
Python

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