mirror of
https://github.com/wowlikon/LibraryAPI.git
synced 2025-12-11 21:30:46 +00:00
Global refactoring of the project to use poetry and implement tests,
fixing bugs, changing the handling of dto and db models, preparing to add new functionality
This commit is contained in:
2
library_service/models/__init__.py
Normal file
2
library_service/models/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from .dto import *
|
||||
from .db import *
|
||||
7
library_service/models/db/__init__.py
Normal file
7
library_service/models/db/__init__.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from .author import Author
|
||||
from .book import Book
|
||||
from .links import AuthorBookLink, AuthorWithBooks, BookWithAuthors
|
||||
|
||||
__all__ = [
|
||||
'Author', 'Book', 'AuthorBookLink', 'AuthorWithBooks', 'BookWithAuthors'
|
||||
]
|
||||
14
library_service/models/db/author.py
Normal file
14
library_service/models/db/author.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from typing import List, Optional, TYPE_CHECKING
|
||||
from sqlmodel import SQLModel, Field, Relationship
|
||||
from ..dto.author import AuthorBase
|
||||
from .links import AuthorBookLink
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .book import Book
|
||||
|
||||
class Author(AuthorBase, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True, index=True)
|
||||
books: List["Book"] = Relationship(
|
||||
back_populates="authors",
|
||||
link_model=AuthorBookLink
|
||||
)
|
||||
14
library_service/models/db/book.py
Normal file
14
library_service/models/db/book.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from typing import List, Optional, TYPE_CHECKING
|
||||
from sqlmodel import SQLModel, Field, Relationship
|
||||
from ..dto.book import BookBase
|
||||
from .links import AuthorBookLink
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .author import Author
|
||||
|
||||
class Book(BookBase, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True, index=True)
|
||||
authors: List["Author"] = Relationship(
|
||||
back_populates="books",
|
||||
link_model=AuthorBookLink
|
||||
)
|
||||
15
library_service/models/db/links.py
Normal file
15
library_service/models/db/links.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from sqlmodel import SQLModel, Field
|
||||
from typing import List
|
||||
|
||||
from library_service.models.dto.author import AuthorRead
|
||||
from library_service.models.dto.book import BookRead
|
||||
|
||||
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 AuthorWithBooks(AuthorRead):
|
||||
books: List[BookRead] = Field(default_factory=list)
|
||||
|
||||
class BookWithAuthors(BookRead):
|
||||
authors: List[AuthorRead] = Field(default_factory=list)
|
||||
15
library_service/models/dto/__init__.py
Normal file
15
library_service/models/dto/__init__.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from .author import (
|
||||
AuthorBase, AuthorCreate, AuthorUpdate,
|
||||
AuthorRead, AuthorList
|
||||
)
|
||||
from .book import (
|
||||
BookBase, BookCreate, BookUpdate,
|
||||
BookRead, BookList
|
||||
)
|
||||
# from .common import PaginatedResponse
|
||||
|
||||
__all__ = [
|
||||
'AuthorBase', 'AuthorCreate', 'AuthorUpdate', 'AuthorRead', 'AuthorList',
|
||||
'BookBase', 'BookCreate', 'BookUpdate', 'BookRead', 'BookList',
|
||||
# 'PaginatedResponse'
|
||||
]
|
||||
25
library_service/models/dto/author.py
Normal file
25
library_service/models/dto/author.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from sqlmodel import SQLModel
|
||||
from pydantic import ConfigDict
|
||||
from typing import Optional, List
|
||||
|
||||
class AuthorBase(SQLModel):
|
||||
name: str
|
||||
|
||||
model_config = ConfigDict( #pyright: ignore
|
||||
json_schema_extra={
|
||||
"example": {"name": "author_name"}
|
||||
}
|
||||
)
|
||||
|
||||
class AuthorCreate(AuthorBase):
|
||||
pass
|
||||
|
||||
class AuthorUpdate(SQLModel):
|
||||
name: Optional[str] = None
|
||||
|
||||
class AuthorRead(AuthorBase):
|
||||
id: int
|
||||
|
||||
class AuthorList(SQLModel):
|
||||
authors: List[AuthorRead]
|
||||
total: int
|
||||
30
library_service/models/dto/book.py
Normal file
30
library_service/models/dto/book.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from sqlmodel import SQLModel
|
||||
from pydantic import ConfigDict
|
||||
from typing import Optional, List
|
||||
|
||||
class BookBase(SQLModel):
|
||||
title: str
|
||||
description: str
|
||||
|
||||
model_config = ConfigDict( #pyright: ignore
|
||||
json_schema_extra={
|
||||
"example": {
|
||||
"title": "book_title",
|
||||
"description": "book_description"
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
class BookCreate(BookBase):
|
||||
pass
|
||||
|
||||
class BookUpdate(SQLModel):
|
||||
title: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
|
||||
class BookRead(BookBase):
|
||||
id: int
|
||||
|
||||
class BookList(SQLModel):
|
||||
books: List[BookRead]
|
||||
total: int
|
||||
Reference in New Issue
Block a user