mirror of
https://github.com/wowlikon/LibraryAPI.git
synced 2025-12-11 21:30:46 +00:00
Swagger UI tags
This commit is contained in:
34
app/main.py
34
app/main.py
@@ -6,8 +6,22 @@ from typing import List
|
|||||||
from .database import engine
|
from .database import engine
|
||||||
from .models import Author, Book
|
from .models import Author, Book
|
||||||
|
|
||||||
app = FastAPI()
|
|
||||||
alembic_cfg = Config("alembic.ini")
|
alembic_cfg = Config("alembic.ini")
|
||||||
|
app = FastAPI(
|
||||||
|
title="My API",
|
||||||
|
description="This is a sample API for managing authors and books.",
|
||||||
|
version="1.0.0",
|
||||||
|
openapi_tags=[
|
||||||
|
{
|
||||||
|
"name": "authors",
|
||||||
|
"description": "Operations with authors.",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "books",
|
||||||
|
"description": "Operations with books.",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
# Initialize the database
|
# Initialize the database
|
||||||
@app.on_event("startup")
|
@app.on_event("startup")
|
||||||
@@ -18,12 +32,12 @@ def on_startup():
|
|||||||
command.upgrade(alembic_cfg, "head")
|
command.upgrade(alembic_cfg, "head")
|
||||||
|
|
||||||
# Root endpoint
|
# Root endpoint
|
||||||
@app.get("/")
|
@app.get("/", tags=["authors", "books"])
|
||||||
async def hello_world():
|
async def hello_world():
|
||||||
return {"message": "Hello world!"}
|
return {"message": "Hello world!"}
|
||||||
|
|
||||||
# Create an author
|
# Create an author
|
||||||
@app.post("/authors/", response_model=Author)
|
@app.post("/authors/", response_model=Author, tags=["authors"])
|
||||||
def create_author(author: Author):
|
def create_author(author: Author):
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
session.add(author)
|
session.add(author)
|
||||||
@@ -32,14 +46,14 @@ def create_author(author: Author):
|
|||||||
return author
|
return author
|
||||||
|
|
||||||
# Read authors
|
# Read authors
|
||||||
@app.get("/authors/", response_model=List[Author])
|
@app.get("/authors/", response_model=List[Author], tags=["authors"])
|
||||||
def read_authors():
|
def read_authors():
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
authors = session.exec(select(Author)).all()
|
authors = session.exec(select(Author)).all()
|
||||||
return authors
|
return authors
|
||||||
|
|
||||||
# Update an author
|
# Update an author
|
||||||
@app.put("/authors/{author_id}", response_model=Author)
|
@app.put("/authors/{author_id}", response_model=Author, tags=["authors"])
|
||||||
def update_author(author_id: int, author: Author):
|
def update_author(author_id: int, author: Author):
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
db_author = session.get(Author, author_id)
|
db_author = session.get(Author, author_id)
|
||||||
@@ -52,7 +66,7 @@ def update_author(author_id: int, author: Author):
|
|||||||
return db_author
|
return db_author
|
||||||
|
|
||||||
# Delete an author
|
# Delete an author
|
||||||
@app.delete("/authors/{author_id}")
|
@app.delete("/authors/{author_id}", tags=["authors"])
|
||||||
def delete_author(author_id: int):
|
def delete_author(author_id: int):
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
db_author = session.get(Author, author_id)
|
db_author = session.get(Author, author_id)
|
||||||
@@ -63,7 +77,7 @@ def delete_author(author_id: int):
|
|||||||
return {"message": "Author deleted"}
|
return {"message": "Author deleted"}
|
||||||
|
|
||||||
# Create a book
|
# Create a book
|
||||||
@app.post("/books/", response_model=Book)
|
@app.post("/books/", response_model=Book, tags=["books"])
|
||||||
def create_book(book: Book):
|
def create_book(book: Book):
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
session.add(book)
|
session.add(book)
|
||||||
@@ -72,14 +86,14 @@ def create_book(book: Book):
|
|||||||
return book
|
return book
|
||||||
|
|
||||||
# Read books
|
# Read books
|
||||||
@app.get("/books/", response_model=List[Book])
|
@app.get("/books/", response_model=List[Book], tags=["books"])
|
||||||
def read_books():
|
def read_books():
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
books = session.exec(select(Book)).all()
|
books = session.exec(select(Book)).all()
|
||||||
return books
|
return books
|
||||||
|
|
||||||
# Update a book
|
# Update a book
|
||||||
@app.put("/books/{book_id}", response_model=Book)
|
@app.put("/books/{book_id}", response_model=Book, tags=["books"])
|
||||||
def update_book(book_id: int, book: Book):
|
def update_book(book_id: int, book: Book):
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
db_book = session.get(Book, book_id)
|
db_book = session.get(Book, book_id)
|
||||||
@@ -93,7 +107,7 @@ def update_book(book_id: int, book: Book):
|
|||||||
return db_book
|
return db_book
|
||||||
|
|
||||||
# Delete a book
|
# Delete a book
|
||||||
@app.delete("/books/{book_id}")
|
@app.delete("/books/{book_id}", tags=["books"])
|
||||||
def delete_book(book_id: int):
|
def delete_book(book_id: int):
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
db_book = session.get(Book, book_id)
|
db_book = session.get(Book, book_id)
|
||||||
|
|||||||
Reference in New Issue
Block a user