create tips

This commit is contained in:
xtaodada 2023-04-23 14:27:25 +08:00
parent cdf4f91901
commit 265c335b0f
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
2 changed files with 29 additions and 0 deletions

19
apis/main/create_tips.py Normal file
View File

@ -0,0 +1,19 @@
from pydantic import BaseModel
from defs import app
from models.services.tip import TipAction
class CreateTips(BaseModel):
content: str
async def create_tips_func(tips: CreateTips):
await TipAction.create_tips(tips.content)
@app.post("/create_tips")
async def create_tips(tips: CreateTips):
if not tips.content:
return {"code": 400, "msg": "content is required"}
await create_tips_func(tips)
return {"code": 200, "msg": "success"}

View File

@ -32,3 +32,13 @@ class TipAction:
statement = select(Tip)
results = await session.exec(statement)
return results.scalars().all()
@staticmethod
async def create_tips(content: str) -> Tip:
async with sqlite.session() as session:
session = cast(AsyncSession, session)
tip = Tip(content=content)
session.add(tip)
await session.commit()
await session.refresh(tip)
return tip