mirror of
https://github.com/wowlikon/LibraryAPI.git
synced 2025-12-11 21:30:46 +00:00
Created models for genres
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
# 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.
|
||||
|
||||
### **Key Components:**
|
||||
@@ -96,6 +94,4 @@ For run tests:
|
||||
|
||||
### **TODO List**
|
||||
|
||||
- Implement tests
|
||||
- Geners table
|
||||
- Check and update documentation
|
||||
- Geners table and endpoints
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
from .author import Author
|
||||
from .book import Book
|
||||
from .links import AuthorBookLink, AuthorWithBooks, BookWithAuthors
|
||||
from .links import (
|
||||
AuthorBookLink, GenreBookLink,
|
||||
AuthorWithBooks, BookWithAuthors,
|
||||
GenreWithBooks, BookWithAuthorsAndGenres
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
'Author', 'Book', 'AuthorBookLink', 'AuthorWithBooks', 'BookWithAuthors'
|
||||
'Author', 'Book',
|
||||
'AuthorBookLink', 'AuthorWithBooks',
|
||||
'BookWithAuthors', 'GenreBookLink',
|
||||
'GenreWithBooks', 'BookWithAuthorsAndGenres'
|
||||
]
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
from typing import List, Optional, TYPE_CHECKING
|
||||
from sqlmodel import SQLModel, Field, Relationship
|
||||
from ..dto.book import BookBase
|
||||
from .links import AuthorBookLink
|
||||
from .links import AuthorBookLink, GenreBookLink
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .author import Author
|
||||
from .genre import Genre
|
||||
|
||||
class Book(BookBase, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True, index=True)
|
||||
@@ -12,3 +13,7 @@ class Book(BookBase, table=True):
|
||||
back_populates="books",
|
||||
link_model=AuthorBookLink
|
||||
)
|
||||
genres: List["Genre"] = Relationship(
|
||||
back_populates="books",
|
||||
link_model=GenreBookLink
|
||||
)
|
||||
|
||||
14
library_service/models/db/genre.py
Normal file
14
library_service/models/db/genre.py
Normal 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
|
||||
)
|
||||
@@ -3,13 +3,28 @@ from typing import List
|
||||
|
||||
from library_service.models.dto.author import AuthorRead
|
||||
from library_service.models.dto.book import BookRead
|
||||
from library_service.models.dto.genre import GenreRead
|
||||
|
||||
class AuthorBookLink(SQLModel, table=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)
|
||||
|
||||
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):
|
||||
books: List[BookRead] = Field(default_factory=list)
|
||||
|
||||
class BookWithAuthors(BookRead):
|
||||
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)
|
||||
|
||||
@@ -6,10 +6,14 @@ from .book import (
|
||||
BookBase, BookCreate, BookUpdate,
|
||||
BookRead, BookList
|
||||
)
|
||||
# from .common import PaginatedResponse
|
||||
|
||||
from .genre import (
|
||||
GenreBase, GenreCreate, GenreUpdate,
|
||||
GenreRead, GenreList
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
'AuthorBase', 'AuthorCreate', 'AuthorUpdate', 'AuthorRead', 'AuthorList',
|
||||
'BookBase', 'BookCreate', 'BookUpdate', 'BookRead', 'BookList',
|
||||
# 'PaginatedResponse'
|
||||
'GenreBase', 'GenreCreate', 'GenreUpdate', 'GenreRead', 'GenreList',
|
||||
]
|
||||
|
||||
25
library_service/models/dto/genre.py
Normal file
25
library_service/models/dto/genre.py
Normal 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
|
||||
Reference in New Issue
Block a user