generated from john/python-template
service base
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import asyncio
|
||||
from abc import ABC
|
||||
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
|
||||
from ..config import Settings
|
||||
from ..config import get_settings
|
||||
from ..db.runtime import get_session_factory
|
||||
|
||||
|
||||
class ServiceBase(ABC):
|
||||
"""Thin service class for managing documents in the database."""
|
||||
|
||||
settings: Settings
|
||||
session_factory: async_sessionmaker[AsyncSession]
|
||||
queue: asyncio.Queue
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
session_factory: async_sessionmaker[AsyncSession] | None = None,
|
||||
queue: asyncio.Queue | None = None,
|
||||
):
|
||||
self.settings = get_settings()
|
||||
self.session_factory = session_factory or get_session_factory()
|
||||
self.queue = queue or asyncio.Queue()
|
||||
@@ -1,23 +1,22 @@
|
||||
from collections.abc import Sequence
|
||||
from pathlib import Path
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker
|
||||
from sqlalchemy.orm import selectinload
|
||||
from sqlmodel import select
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
|
||||
from ..db.runtime import get_session_factory
|
||||
from ..errors import AppError
|
||||
from ..models import Document
|
||||
from .base import ServiceBase
|
||||
|
||||
|
||||
class DocumentService:
|
||||
class MissingImageError(AppError):
|
||||
"""Raised when a required image is missing."""
|
||||
|
||||
|
||||
class DocumentService(ServiceBase):
|
||||
"""Thin service class for managing documents in the database."""
|
||||
|
||||
session_factory: async_sessionmaker[AsyncSession]
|
||||
|
||||
def __init__(self, session_factory: async_sessionmaker[AsyncSession] | None = None):
|
||||
self.session_factory = session_factory or get_session_factory()
|
||||
|
||||
async def create_document(self, document: Document) -> Document:
|
||||
"""Create a new document in the database."""
|
||||
async with self.session_factory() as session:
|
||||
@@ -39,6 +38,10 @@ class DocumentService:
|
||||
)
|
||||
if document is None:
|
||||
raise ValueError(f"Document with id {document_id} not found")
|
||||
elif not Path(document.file_path).exists():
|
||||
raise MissingImageError(
|
||||
f"Document with id {document_id} is missing its image file in {self.settings.upload_dir:!s}"
|
||||
)
|
||||
return document
|
||||
|
||||
async def update_document(self, document: Document) -> Document:
|
||||
|
||||
@@ -1,24 +1,17 @@
|
||||
from collections.abc import Sequence
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker
|
||||
from sqlalchemy.orm import selectinload
|
||||
from sqlmodel import select
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
|
||||
from ..db.runtime import get_session_factory
|
||||
from ..models import Job
|
||||
from ..models import JobStatus
|
||||
from .base import ServiceBase
|
||||
|
||||
|
||||
class JobService:
|
||||
class JobService(ServiceBase):
|
||||
"""Thin service class for managing jobs in the database."""
|
||||
|
||||
session_factory: async_sessionmaker[AsyncSession]
|
||||
|
||||
def __init__(self, session_factory: async_sessionmaker[AsyncSession] | None = None):
|
||||
self.session_factory = session_factory or get_session_factory()
|
||||
|
||||
async def create_job(self, job: Job) -> Job:
|
||||
"""Create a new job in the database."""
|
||||
async with self.session_factory() as session:
|
||||
|
||||
Reference in New Issue
Block a user