spliting models

This commit is contained in:
2025-05-29 00:08:05 +03:00
parent 695ab6aa44
commit 4a433f3191
2 changed files with 78 additions and 67 deletions

View File

@@ -1,10 +1,10 @@
from alembic import command from alembic import command
from alembic.config import Config from alembic.config import Config
from fastapi import FastAPI, HTTPException from fastapi import FastAPI, Depends, HTTPException
from sqlmodel import SQLModel, Session, select from sqlmodel import SQLModel, Session, select
from typing import List from typing import List
from .database import engine from .database import engine, get_session
from .models import Author, Book from .models import Author, AuthorBase, Book, BookBase
alembic_cfg = Config("alembic.ini") alembic_cfg = Config("alembic.ini")
app = FastAPI( app = FastAPI(
@@ -38,81 +38,72 @@ async def hello_world():
# Create an author # Create an author
@app.post("/authors/", response_model=Author, tags=["authors"]) @app.post("/authors/", response_model=Author, tags=["authors"])
def create_author(author: Author): def create_author(author: AuthorBase, session: Session = Depends(get_session)):
with Session(engine) as session: db_author = Author(name=author.name)
session.add(author) session.add(db_author)
session.commit() session.commit()
session.refresh(author) session.refresh(db_author)
return author return db_author
# Read authors # Read authors
@app.get("/authors/", response_model=List[Author], tags=["authors"]) @app.get("/authors/", response_model=List[Author], tags=["authors"])
def read_authors(): def read_authors(session: Session = Depends(get_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, tags=["authors"]) @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: AuthorBase, session: Session = Depends(get_session)):
with Session(engine) as session: db_author = session.get(Author, author_id)
db_author = session.get(Author, author_id) if not db_author:
if not db_author: raise HTTPException(status_code=404, detail="Author not found")
raise HTTPException(status_code=404, detail="Author not found") db_author.name = author.name
db_author.name = author.name session.commit()
session.add(db_author) session.refresh(db_author)
session.commit() return db_author
session.refresh(db_author)
return db_author
# Delete an author # Delete an author
@app.delete("/authors/{author_id}", tags=["authors"]) @app.delete("/authors/{author_id}", response_model=AuthorBase, tags=["authors"])
def delete_author(author_id: int): def delete_author(author_id: int, session: Session = Depends(get_session)):
with Session(engine) as session: db_author = session.get(Author, author_id)
db_author = session.get(Author, author_id) if not db_author:
if not db_author: raise HTTPException(status_code=404, detail="Author not found")
raise HTTPException(status_code=404, detail="Author not found") session.delete(db_author)
session.delete(db_author) session.commit()
session.commit() return {"message": "Author deleted"}
return {"message": "Author deleted"}
# Create a book # Create a book
@app.post("/books/", response_model=Book, tags=["books"]) @app.post("/books/", response_model=Book, tags=["books"])
def create_book(book: Book): def create_book(book: BookBase, session: Session = Depends(get_session)):
with Session(engine) as session: session.add(book)
session.add(book) session.commit()
session.commit() session.refresh(book)
session.refresh(book) return book
return book
# Read books # Read books
@app.get("/books/", response_model=List[Book], tags=["books"]) @app.get("/books/", response_model=List[Book], tags=["books"])
def read_books(): def read_books(session: Session = Depends(get_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, tags=["books"]) @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, session: Session = Depends(get_session)):
with Session(engine) as session: db_book = session.get(Book, book_id)
db_book = session.get(Book, book_id) if not db_book:
if not db_book: raise HTTPException(status_code=404, detail="Book not found")
raise HTTPException(status_code=404, detail="Book not found") db_book.title = book.title
db_book.title = book.title db_book.authors = book.authors
db_book.authors = book.authors session.commit()
session.add(db_book) session.refresh(db_book)
session.commit() return db_book
session.refresh(db_book)
return db_book
# Delete a book # Delete a book
@app.delete("/books/{book_id}", tags=["books"]) @app.delete("/books/{book_id}", tags=["books"])
def delete_book(book_id: int): def delete_book(book_id: int, session: Session = Depends(get_session)):
with Session(engine) as session: db_book = session.get(Book, book_id)
db_book = session.get(Book, book_id) if not db_book:
if not db_book: raise HTTPException(status_code=404, detail="Book not found")
raise HTTPException(status_code=404, detail="Book not found") session.delete(db_book)
session.delete(db_book) session.commit()
session.commit() return {"message": "Book deleted"}
return {"message": "Book deleted"}

View File

@@ -6,16 +6,36 @@ class AuthorBookLink(SQLModel, table=True):
author_id: int | None = Field(default=None, foreign_key="author.id", primary_key=True) author_id: int | None = Field(default=None, foreign_key="author.id", primary_key=True)
book_id: int | None = Field(default=None, foreign_key="book.id", primary_key=True) book_id: int | None = Field(default=None, foreign_key="book.id", primary_key=True)
# Author model # Author DTO model
class Author(SQLModel, table=True): class AuthorBase(SQLModel):
id: int | None = Field(primary_key=True, index=True)
name: str name: str
class Config: # pyright: ignore
json_schema_extra = {
"example": {
"name": "author_name",
}
}
# Author DB model
class Author(AuthorBase, table=True):
id: int | None = Field(default=None, primary_key=True, index=True)
books: List["Book"] = Relationship(back_populates="authors", link_model=AuthorBookLink) books: List["Book"] = Relationship(back_populates="authors", link_model=AuthorBookLink)
# Book model # Book DTO model
class Book(SQLModel, table=True): class BookBase(SQLModel):
id: int | None = Field(primary_key=True, index=True)
title: str title: str
description: str description: str
class Config: # pyright: ignore
json_schema_extra = {
"example": {
"title": "book_title",
"description": "book_description",
}
}
# Book DB model
class Book(BookBase, table=True):
id: int | None = Field(default=None, primary_key=True, index=True)
authors: List[Author] = Relationship(back_populates="books", link_model=AuthorBookLink) authors: List[Author] = Relationship(back_populates="books", link_model=AuthorBookLink)