Esempio n. 1
0
 def put(id, title, description, price, image_url):
     """ Update an prdouct based on the sent information """
     repository = ProductRepository()
     product = repository.update(id=id,
                                 title=title,
                                 description=description,
                                 price=price,
                                 image_url=image_url)
     return jsonify(product.json)
Esempio n. 2
0
    def run(self):
        for i in range(10):
            job = fake.job()
            image_url = "https://via.placeholder.com/150/" + fake.safe_hex_color(
            ).replace("#", "") + "/000000?Text=" + job

            ProductRepository.create(title=job,
                                     description=fake.paragraph(
                                         nb_sentences=3,
                                         variable_nb_sentences=True,
                                         ext_word_list=None),
                                     price=fake.pydecimal(left_digits=2,
                                                          right_digits=5,
                                                          positive=True),
                                     image_url=image_url)
Esempio n. 3
0
 def post(title, description, price, image_url):
     """ Create an product based on the sent information """
     product = ProductRepository.create(title=title,
                                        description=description,
                                        price=price,
                                        image_url=image_url)
     return jsonify(product.json)
Esempio n. 4
0
    def get(id):
        """ Return an product key information based on his name """
        products = ProductRepository.get(id=id)

        if hasattr(products, "json"):
            return jsonify(products.json)
        else:
            return jsonify([product.json for product in products])
Esempio n. 5
0
 def post(self):
     """
     Create Product
     """
     request_json = request.get_json(silent=True)
     name: str = request_json['name']
     desc: str = request_json['desc']
     
     try:
         response = ProductRepository.create(name, desc)
         return response, 200
     except Exception as e:
         response = jsonify(e.to_dict())
         response.status_code = e.status_code
         return response
Esempio n. 6
0
 def post(name,
          brand,
          price,
          category,
          subcategory,
          subsubcategory,
          description=None,
          image=None,
          rating=None,
          units_sold=0):
     """ Create an product based on the sent information """
     product = ProductRepository.create(name, brand, price, category,
                                        subcategory, subsubcategory,
                                        description, image, rating,
                                        units_sold)
     return jsonify({'product': product.json})
Esempio n. 7
0
    def get(start_price=None,
            end_price=None,
            category=None,
            subcategory=None,
            subsubcategory=None,
            pagination=True,
            page=1,
            results_per_page=10,
            sorted="best_selling"):
        """
            Return all products filtered by:
            - Price
            - Category, subcategory or subsubcategory
            Products can also be paginated and sorted by price
        """

        products = ProductRepository.get_all(
            start_price,
            end_price,
            category,
            subcategory,
            subsubcategory,
            sorted,
        )

        if pagination:
            paginated_products = products.paginate(int(page),
                                                   int(results_per_page),
                                                   False)
            products = paginated_products.items
            total = paginated_products.total
        else:
            products = products.all()
            total = len(products)

        product_list = [product.json for product in products]

        return jsonify({"products": product_list, "count": total})
Esempio n. 8
0
    def get(product_id):
        """ Return a product key information based on its id """

        product = ProductRepository.get(product_id)
        return jsonify(
            {'product': product.one_or_none().json if product else None})
Esempio n. 9
0
 def get(self):
     # TODO: error handler
     # move to another resource
     data = ProductRepository.findAll()
     return data, 200