diff --git a/apis/main/create_tips.py b/apis/main/create_tips.py new file mode 100644 index 0000000..aea075b --- /dev/null +++ b/apis/main/create_tips.py @@ -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"} diff --git a/models/services/tip.py b/models/services/tip.py index 75fef71..d5fcf71 100644 --- a/models/services/tip.py +++ b/models/services/tip.py @@ -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