gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
Что нужно изменить, чтобы код сервиса соответствовал читсой архитектуре. Сам код: class…
- User
Что нужно изменить, чтобы код сервиса соответствовал читсой архитектуре. Сам код: class MetaProductService: def init(self, context: RequestContext) -> None: self._context = context self.content_service = ContentTemplateGroupService() assert self._context.price_level_id is not None self.product_service = ProductService(price_level_id=self._context.price_level_id, office_id=self._context.office_id)
def get_meta_product(self, url: str, frontend_path: str) -> MetaProductWrapperDto: assert self._context.location is not None assert self._context.office_id is not None assert self._context.price_level_id is not None assert self._context.price_level is not None try: meta_product = MetaProduct.objects.get_meta_product_by_url(url, self._context.price_level_id) except ObjectDoesNotExist: raise EntityNotFound(CommonErrorTypes.ENTITY_NOT_FOUND) ctx = MetaProductContentContext(user_location=self._context.location, entity=meta_product, price_level=self._context.price_level) content = self.content_service.render_template_group(frontend_path, ctx) products_dto = self.product_service.get_products(meta_product, include_stocks=self._should_include_stocks()) recommended_products_dto = self._create_recommended_products_dto(meta_product) return MetaProductWrapperDto.create(meta_product=meta_product, products=products_dto, recommended_products=recommended_products_dto, content=content) - Assistant
- Избавиться от assert'ов и заменить их на проверки условий и возбуждение исключений в случае их нарушения.
- Вынести инициализацию сервисов ProductService и ContentTemplateGroupService за пределы класса MetaProductService и передавать их в конструктор.
- Вынести получение MetaProduct из базы данных в отдельный метод или отдельный сервис.
- Вынести создание контекста MetaProductContentContext в отдельный метод или отдельный сервис.
- Вынести получение списка продуктов и списка рекомендуемых продуктов в отдельные методы или отдельные сервисы.
- Все методы или сервисы, упомянутые в пунктах 3-5, должны быть внедрены в MetaProductService через конструктор.