mirror of
https://github.com/testdrivenio/fastapi-sqlmodel-alembic.git
synced 2025-08-14 17:11:09 +08:00
29 lines
853 B
Python
29 lines
853 B
Python
from fastapi import Depends, FastAPI
|
|
from sqlmodel import select
|
|
from sqlmodel.ext.asyncio.session import AsyncSession
|
|
|
|
from app.db import get_session, init_db
|
|
from app.models import Song, SongCreate
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
@app.get("/ping")
|
|
async def pong():
|
|
return {"ping": "pong!"}
|
|
|
|
|
|
@app.get("/songs", response_model=list[Song])
|
|
async def get_songs(session: AsyncSession = Depends(get_session)):
|
|
result = await session.execute(select(Song))
|
|
songs = result.scalars().all()
|
|
return [Song(name=song.name, artist=song.artist, year=song.year, id=song.id) for song in songs]
|
|
|
|
|
|
@app.post("/songs")
|
|
async def add_song(song: SongCreate, session: AsyncSession = Depends(get_session)):
|
|
song = Song(name=song.name, artist=song.artist, year=song.year)
|
|
session.add(song)
|
|
await session.commit()
|
|
await session.refresh(song)
|
|
return song |