rename BookService CRUD overrides to domain-specific methods

The create, update, and delete methods had incompatible signatures
resulting in typecheck errors. Renamed to create_book, update_book, and
delete_books.
This commit is contained in:
2026-03-07 11:58:24 -05:00
parent 67fab3f9c6
commit 8117f0dbfe
2 changed files with 11 additions and 12 deletions

View File

@@ -85,7 +85,7 @@ class BookController(Controller):
""" """
result = await books_service.create(data, library) result = await books_service.create_book(data, library)
book = await books_service.get(result.id) book = await books_service.get(result.id)
return books_service.to_schema(book, schema_type=s.BookRead) return books_service.to_schema(book, schema_type=s.BookRead)
@@ -213,7 +213,7 @@ class BookController(Controller):
Returns: Returns:
The updated book as a BookRead schema. The updated book as a BookRead schema.
""" """
await books_service.update(book_id, data, library) await books_service.update_book(book_id, data, library)
book = await books_service.get(book_id) book = await books_service.get(book_id)
return books_service.to_schema(book, schema_type=s.BookRead) return books_service.to_schema(book, schema_type=s.BookRead)
@@ -244,7 +244,7 @@ class BookController(Controller):
The updated book as a BookRead schema. The updated book as a BookRead schema.
""" """
await books_service.update(book_id, {"cover_image": data}, library) await books_service.update_book(book_id, {"cover_image": data}, library)
updated_book = await books_service.get(book_id) updated_book = await books_service.get(book_id)
return books_service.to_schema(updated_book, schema_type=s.BookRead) return books_service.to_schema(updated_book, schema_type=s.BookRead)
@@ -386,7 +386,7 @@ class BookController(Controller):
""" """
await books_service.delete(book_ids, library, delete_files=delete_files) await books_service.delete_books(book_ids, library, delete_files=delete_files)
@post(path="/progress/{book_id:int}") @post(path="/progress/{book_id:int}")
async def update_progress( async def update_progress(

View File

@@ -17,6 +17,7 @@ from advanced_alchemy.service import (
SQLAlchemyAsyncRepositoryService, SQLAlchemyAsyncRepositoryService,
ModelDictT, ModelDictT,
is_dict, is_dict,
schema_dump
) )
from advanced_alchemy.repository import SQLAlchemyAsyncRepository from advanced_alchemy.repository import SQLAlchemyAsyncRepository
from advanced_alchemy.filters import CollectionFilter from advanced_alchemy.filters import CollectionFilter
@@ -67,7 +68,7 @@ class BookService(SQLAlchemyAsyncRepositoryService[Book]):
async def create(self, data: ModelDictT[Book], library: Library, **kwargs) -> Book: async def create_book(self, data: ModelDictT[Book], library: Library, **kwargs) -> Book:
""" """
Create a new book entity. Create a new book entity.
@@ -83,9 +84,7 @@ class BookService(SQLAlchemyAsyncRepositoryService[Book]):
Returns: Returns:
The created Book entity. The created Book entity.
""" """
if not is_dict(data): data = schema_dump(data)
data = data.model_dump()
await self._parse_metadata_from_files(data) await self._parse_metadata_from_files(data)
await self._save_cover_image(data) await self._save_cover_image(data)
@@ -126,7 +125,7 @@ class BookService(SQLAlchemyAsyncRepositoryService[Book]):
books[filepath].append(file) books[filepath].append(file)
return [ return [
await self.create( await self.create_book(
{"files": [file for file in files], "library_id": library.id}, {"files": [file for file in files], "library_id": library.id},
library, library,
**kwargs, **kwargs,
@@ -211,7 +210,7 @@ class BookService(SQLAlchemyAsyncRepositoryService[Book]):
return books return books
async def delete( async def delete_books(
self, self,
book_ids: list[int], book_ids: list[int],
library: Library, library: Library,
@@ -309,7 +308,7 @@ class BookService(SQLAlchemyAsyncRepositoryService[Book]):
break break
yield chunk yield chunk
async def update( async def update_book(
self, book_id: int, update_data: ModelDictT[Book], library: Library self, book_id: int, update_data: ModelDictT[Book], library: Library
) -> Book: ) -> Book:
""" """
@@ -370,7 +369,7 @@ class BookService(SQLAlchemyAsyncRepositoryService[Book]):
data["files"] = files data["files"] = files
await self._save_book_files(library, data) await self._save_book_files(library, data)
book.files.extend(data["files"]) book.files.extend(data["files"])
await self.update(book.id, {"files": [file for file in book.files]}, library) await self.update_book(book.id, {"files": [file for file in book.files]}, library)
async def remove_files( async def remove_files(
self, book_id: int, file_ids: list[int], delete_files: bool, library: Library self, book_id: int, file_ids: list[int], delete_files: bool, library: Library