create inherited class of from typing import TypeVar, Generic from uuid import UUID from…
- User
create inherited class of from typing import TypeVar, Generic from uuid import UUID
from src.db.postgresql_manager import postgresql_manager
INPUT_DTO = TypeVar('INPUT_DTO') OUTPUT_DTO = TypeVar('OUTPUT_DTO') ORM_CLASS = TypeVar('ORM_CLASS')
class BaseBusinessLogic(Generic[INPUT_DTO, OUTPUT_DTO, ORM_CLASS]):
codeasync def create_city(self, create_dto: INPUT_DTO) -> OUTPUT_DTO: create_dto.verify() orm = ORM_CLASS(**create_dto.dict()) orm = await postgresql_manager.create(ORM_CLASS, orm) return OUTPUT_DTO.from_orm(orm) async def read_all(self) -> list[OUTPUT_DTO]: orm_list = await postgresql_manager.read_all(ORM_CLASS) return [OUTPUT_DTO.from_orm(orm) for orm in orm_list] async def read(self, id: UUID) -> OUTPUT_DTO: orm = await postgresql_manager.read(ORM_CLASS, id) return OUTPUT_DTO.from_orm(orm) async def update(self, id: UUID, update_dto: INPUT_DTO) -> OUTPUT_DTO: update_dto.verify() updates = update_dto.dict(exclude_unset=True) orm = await postgresql_manager.update(ORM_CLASS, id, updates) return OUTPUT_DTO.from_orm(orm) async def delete(self, id: UUID) -> OUTPUT_DTO: city_orm = await postgresql_manager.delete(ORM_CLASS, id) return OUTPUT_DTO.from_orm(city_orm) - Assistant
class ChildBusinessLogic(BaseBusinessLogic[INPUT_DTO, OUTPUT_DTO, ORM_CLASS]):
codeasync def create_child(self, create_dto: INPUT_DTO) -> OUTPUT_DTO: create_dto.verify() orm = ORM_CLASS(**create_dto.dict()) orm = await postgresql_manager.create(ORM_CLASS, orm) return OUTPUT_DTO.from_orm(orm) async def read_all_children(self) -> list[OUTPUT_DTO]: orm_list = await postgresql_manager.read_all(ORM_CLASS) return [OUTPUT_DTO.from_orm(orm) for orm in orm_list] async def read_child(self, id: UUID) -> OUTPUT_DTO: orm = await postgresql_manager.read(ORM_CLASS, id) return OUTPUT_DTO.from_orm(orm) async def update_child(self, id: UUID, update_dto: INPUT_DTO) -> OUTPUT_DTO: update_dto.verify() updates = update_dto.dict(exclude_unset=True) orm = await postgresql_manager.update(ORM_CLASS, id, updates) return OUTPUT_DTO.from_orm(orm) async def delete_child(self, id: UUID) -> OUTPUT_DTO: orm = await postgresql_manager.delete(ORM_CLASS, id) return OUTPUT_DTO.from_orm(orm) - User
how to pass it INPUT_DTO, OUTPUT_DTO, ORM_CLASS
- Assistant
When you create an instance of the
ChildBusinessLogicclass, you need to pass theINPUT_DTO,OUTPUT_DTO, andORM_CLASSto it as type arguments. For example, if you have aCitymodel class andCityCreateDTOandCityDTOdata transfer objects, you can create an instance of theChildBusinessLogicclass as follows:codefrom typing import TypeVar from uuid import UUID from src.models.city import City from src.dtos.city_dto import CityCreateDTO, CityDTO from src.business_logic.child_business_logic import ChildBusinessLogic INPUT_DTO = TypeVar('INPUT_DTO', bound=CityCreateDTO) OUTPUT_DTO = TypeVar('OUTPUT_DTO', bound=CityDTO) ORM_CLASS = TypeVar('ORM_CLASS', bound=City) child_logic = ChildBusinessLogic[CityCreateDTO, CityDTO, City]()In this example,
CityCreateDTO,CityDTO, andCityare passed as type arguments , , and , respectively. You can replace them with your own data transfer objects and model classes. Make sure they are compatible with the business logic operations.