felicity-lims/felicity/apps/abstract/service.py

75 lines
2.5 KiB
Python
Raw Normal View History

2024-07-24 17:04:53 +08:00
from typing import TypeVar, Generic
2024-07-21 15:06:51 +08:00
from pydantic import BaseModel
2024-07-21 21:44:22 +08:00
from sqlalchemy.orm import DeclarativeBase
2024-07-21 15:06:51 +08:00
2024-07-24 04:30:01 +08:00
from felicity.apps.abstract.repository import BaseRepository
2024-07-21 15:06:51 +08:00
2024-07-21 21:44:22 +08:00
E = TypeVar("E", bound=DeclarativeBase)
C = TypeVar("C", bound=BaseModel)
U = TypeVar("U", bound=BaseModel)
class BaseService(Generic[E, C, U]):
2024-07-24 17:04:53 +08:00
def __init__(self, repository) -> None:
self.repository: BaseRepository = repository()
2024-07-21 15:06:51 +08:00
async def paging_filter(
2024-07-24 17:04:53 +08:00
self,
page_size: int | None = None,
after_cursor: str | None = None,
before_cursor: str | None = None,
2024-07-26 16:11:54 +08:00
filters: list[dict] | dict = None,
2024-07-24 17:04:53 +08:00
sort_by: list[str] | None = None,
**kwargs
2024-07-21 21:44:22 +08:00
):
2024-07-24 17:04:53 +08:00
return await self.repository.paginate(
2024-07-26 16:11:54 +08:00
page_size, after_cursor, before_cursor, filters, sort_by, **kwargs
2024-07-21 15:06:51 +08:00
)
2024-07-21 21:44:22 +08:00
async def search(self, **kwargs) -> list[E]:
2024-07-21 15:06:51 +08:00
return await self.repository.search(**kwargs)
2024-07-21 21:44:22 +08:00
async def all(self) -> list[E]:
2024-07-21 15:06:51 +08:00
return await self.repository.all()
2024-07-21 21:44:22 +08:00
async def get(self, **kwargs) -> E:
2024-07-21 15:06:51 +08:00
return await self.repository.get(**kwargs)
2024-07-21 21:44:22 +08:00
async def get_by_uids(self, uids: list[str]) -> list[E]:
2024-07-21 15:06:51 +08:00
return await self.repository.get_by_uids(uids)
2024-07-21 21:44:22 +08:00
async def get_all(self, **kwargs) -> list[E]:
2024-07-21 15:06:51 +08:00
return await self.repository.get_all(**kwargs)
2024-07-21 21:44:22 +08:00
async def get_related(self, uid: str, related: list[str]) -> E:
2024-07-24 17:04:53 +08:00
return await self.repository.get_related(related=related, uid=uid)
2024-07-21 15:06:51 +08:00
2024-07-21 21:44:22 +08:00
async def create(self, c: C | dict) -> E:
data = self._import(c)
return await self.repository.create(**data)
2024-07-21 15:06:51 +08:00
2024-07-24 04:30:01 +08:00
async def bulk_create(self, bulk: list[dict | C]) -> None:
return await self.repository.bulk_create([self._import(b) for b in bulk])
2024-07-21 15:06:51 +08:00
2024-07-24 04:30:01 +08:00
async def update(self, uid: str, update: U | dict) -> E:
2024-07-24 17:04:53 +08:00
if "uid" in update:
del update["uid"]
2024-07-24 04:30:01 +08:00
return await self.repository.update(uid, **self._import(update))
2024-07-21 15:06:51 +08:00
2024-07-24 17:04:53 +08:00
async def save(self, entity: E) -> E:
return await self.repository.save(entity)
async def bulk_update_with_mappings(self, mappings: list[dict]) -> None:
2024-07-21 15:06:51 +08:00
return await self.repository.bulk_update_with_mappings(mappings)
async def delete(self, uid: str) -> None:
return await self.repository.delete(uid)
2024-07-24 17:04:53 +08:00
2024-07-21 21:44:22 +08:00
@classmethod
def _import(cls, schema_in: C | U | dict) -> dict:
"""Convert Pydantic schema to dict"""
if isinstance(schema_in, dict):
return schema_in
return schema_in.model_dump(exclude_unset=True)