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)
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)
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)
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])
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
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})
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})
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})
def get(self): # TODO: error handler # move to another resource data = ProductRepository.findAll() return data, 200