create tips
This commit is contained in:
parent
cdf4f91901
commit
265c335b0f
19
apis/main/create_tips.py
Normal file
19
apis/main/create_tips.py
Normal 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"}
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user