Files
API-DB-example/main.py
2023-10-01 01:22:11 +03:00

30 lines
571 B
Python

from fastapi import FastAPI
from pydantic import BaseModel
import database
app = FastAPI()
class Message(BaseModel):
author: str
recipient: str
content: str
@app.get("/")
def read_recents(history: int = 10):
rs = database.DB_get_recent_msgs(history)
return rs
@app.post("/msg")
def post_msg(msg: Message):
database.DB_new_msg(author=msg.author, recipient=msg.recipient, content=msg.content)
return f"Message sent to {msg.recipient}"
@app.get("/msg/{msg_id}")
def get_msg(msg_id: int):
r = database.DB_get_msg(msg_id)
return r