Created models for genres

This commit is contained in:
2025-06-24 21:47:07 +03:00
parent 5db6416d79
commit 83dbb1824e
7 changed files with 76 additions and 10 deletions

View File

@@ -1,7 +1,5 @@
# LibraryAPI # LibraryAPI
## WARNING: Documentation may be partially out of date at this time.
This project is a test web application built using FastAPI, a modern web framework for creating APIs in Python. It showcases the use of Pydantic for data validation, SQLModel for database interactions, Alembic for migration management, PostgreSQL as the database system, and Docker Compose for easy deployment. This project is a test web application built using FastAPI, a modern web framework for creating APIs in Python. It showcases the use of Pydantic for data validation, SQLModel for database interactions, Alembic for migration management, PostgreSQL as the database system, and Docker Compose for easy deployment.
### **Key Components:** ### **Key Components:**
@@ -96,6 +94,4 @@ For run tests:
### **TODO List** ### **TODO List**
- Implement tests - Geners table and endpoints
- Geners table
- Check and update documentation

View File

@@ -1,7 +1,14 @@
from .author import Author from .author import Author
from .book import Book from .book import Book
from .links import AuthorBookLink, AuthorWithBooks, BookWithAuthors from .links import (
AuthorBookLink, GenreBookLink,
AuthorWithBooks, BookWithAuthors,
GenreWithBooks, BookWithAuthorsAndGenres
)
__all__ = [ __all__ = [
'Author', 'Book', 'AuthorBookLink', 'AuthorWithBooks', 'BookWithAuthors' 'Author', 'Book',
'AuthorBookLink', 'AuthorWithBooks',
'BookWithAuthors', 'GenreBookLink',
'GenreWithBooks', 'BookWithAuthorsAndGenres'
] ]

View File

@@ -1,10 +1,11 @@
from typing import List, Optional, TYPE_CHECKING from typing import List, Optional, TYPE_CHECKING
from sqlmodel import SQLModel, Field, Relationship from sqlmodel import SQLModel, Field, Relationship
from ..dto.book import BookBase from ..dto.book import BookBase
from .links import AuthorBookLink from .links import AuthorBookLink, GenreBookLink
if TYPE_CHECKING: if TYPE_CHECKING:
from .author import Author from .author import Author
from .genre import Genre
class Book(BookBase, table=True): class Book(BookBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True, index=True) id: Optional[int] = Field(default=None, primary_key=True, index=True)
@@ -12,3 +13,7 @@ class Book(BookBase, table=True):
back_populates="books", back_populates="books",
link_model=AuthorBookLink link_model=AuthorBookLink
) )
genres: List["Genre"] = Relationship(
back_populates="books",
link_model=GenreBookLink
)

View File

@@ -0,0 +1,14 @@
from typing import List, Optional, TYPE_CHECKING
from sqlmodel import SQLModel, Field, Relationship
from ..dto.genre import GenreBase
from .links import GenreBookLink
if TYPE_CHECKING:
from .book import Book
class Genre(GenreBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True, index=True)
books: List["Book"] = Relationship(
back_populates="authors",
link_model=GenreBookLink
)

View File

@@ -3,13 +3,28 @@ from typing import List
from library_service.models.dto.author import AuthorRead from library_service.models.dto.author import AuthorRead
from library_service.models.dto.book import BookRead from library_service.models.dto.book import BookRead
from library_service.models.dto.genre import GenreRead
class AuthorBookLink(SQLModel, table=True): 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)
class GenreBookLink(SQLModel, table=True):
genre_id: int | None = Field(default=None, foreign_key="genre.id", primary_key=True)
book_id: int | None = Field(default=None, foreign_key="book.id", primary_key=True)
class AuthorWithBooks(AuthorRead): class AuthorWithBooks(AuthorRead):
books: List[BookRead] = Field(default_factory=list) books: List[BookRead] = Field(default_factory=list)
class BookWithAuthors(BookRead): class BookWithAuthors(BookRead):
authors: List[AuthorRead] = Field(default_factory=list) authors: List[AuthorRead] = Field(default_factory=list)
class BookWithGenres(BookRead):
genres: List[GenreRead] = Field(default_factory=list)
class GenreWithBooks(GenreRead):
books: List[BookRead] = Field(default_factory=list)
class BookWithAuthorsAndGenres(BookRead):
authors: List[AuthorRead] = Field(default_factory=list)
genres: List[GenreRead] = Field(default_factory=list)

View File

@@ -6,10 +6,14 @@ from .book import (
BookBase, BookCreate, BookUpdate, BookBase, BookCreate, BookUpdate,
BookRead, BookList BookRead, BookList
) )
# from .common import PaginatedResponse
from .genre import (
GenreBase, GenreCreate, GenreUpdate,
GenreRead, GenreList
)
__all__ = [ __all__ = [
'AuthorBase', 'AuthorCreate', 'AuthorUpdate', 'AuthorRead', 'AuthorList', 'AuthorBase', 'AuthorCreate', 'AuthorUpdate', 'AuthorRead', 'AuthorList',
'BookBase', 'BookCreate', 'BookUpdate', 'BookRead', 'BookList', 'BookBase', 'BookCreate', 'BookUpdate', 'BookRead', 'BookList',
# 'PaginatedResponse' 'GenreBase', 'GenreCreate', 'GenreUpdate', 'GenreRead', 'GenreList',
] ]

View File

@@ -0,0 +1,25 @@
from sqlmodel import SQLModel
from pydantic import ConfigDict
from typing import Optional, List
class GenreBase(SQLModel):
name: str
model_config = ConfigDict( #pyright: ignore
json_schema_extra={
"example": {"name": "genre_name"}
}
)
class GenreCreate(GenreBase):
pass
class GenreUpdate(SQLModel):
name: Optional[str] = None
class GenreRead(GenreBase):
id: int
class GenreList(SQLModel):
genres: List[GenreRead]
total: int