Exemplo n.º 1
0
    def _convert_product_model_to_product(
            product_model: ProductModel) -> Product:
        _type = ProductType(id=product_model.product_type.id,
                            name=product_model.product_type.name)
        product = Product(id=product_model.id,
                          name=product_model.name,
                          type=_type,
                          quantity=product_model.quantity)
        product.set_product_price(product_model.price_value,
                                  product_model.price_currency)
        for image in product_model.images:
            product.add_image(image.get("name"), image.get("url"))

        return product
    def create_product(self, product_dto: ProductDTO) -> Product:
        _id = self.repository.generate_id()
        product_type = self.find_or_create_product_type(
            product_dto.product_type_id, product_dto.product_type_name)
        product = Product(
            id=_id,
            name=product_dto.name,
            quantity=product_dto.quantity,
            type=product_type,
        )
        images = product_dto.images or []
        for image in images:
            if not self.image_validator(image.get("url")):
                raise InvalidProductException(
                    "Image with provided url does not exist.")
            product.add_image(name=image.get("name"), url=image.get("url"))

        product.set_product_price(product_dto.price, product_dto.currency)

        self.repository.create_from(product)

        return product