mirror of
https://github.com/wowlikon/LibraryAPI.git
synced 2025-12-11 21:30:46 +00:00
init
This commit is contained in:
42
app/main.py
Normal file
42
app/main.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from fastapi import FastAPI
|
||||
from sqlmodel import SQLModel, Session, select
|
||||
from .database import engine
|
||||
from .models import Author, Book
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
@app.on_event("startup")
|
||||
def on_startup():
|
||||
SQLModel.metadata.create_all(engine)
|
||||
|
||||
@app.get("/")
|
||||
async def read_root():
|
||||
return {"message": "Hello, FastAPI with SQLModel and PostgreSQL!"}
|
||||
|
||||
@app.post("/authors/")
|
||||
def create_author(author: Author):
|
||||
with Session(engine) as session:
|
||||
session.add(author)
|
||||
session.commit()
|
||||
session.refresh(author)
|
||||
return author
|
||||
|
||||
@app.get("/authors/")
|
||||
def read_authors():
|
||||
with Session(engine) as session:
|
||||
authors = session.exec(select(Author)).all()
|
||||
return authors
|
||||
|
||||
@app.post("/books/")
|
||||
def create_book(book: Book):
|
||||
with Session(engine) as session:
|
||||
session.add(book)
|
||||
session.commit()
|
||||
session.refresh(book)
|
||||
return book
|
||||
|
||||
@app.get("/books/")
|
||||
def read_books():
|
||||
with Session(engine) as session:
|
||||
books = session.exec(select(Book)).all()
|
||||
return books
|
||||
Reference in New Issue
Block a user