def get(self): """ Return all the products""" name = request.args.get('name') category = request.args.get('category') if name: products = Product.find_by_name(name) elif category: products = Product.find_by_category(category) else: products = Product.all() results = [product.serialize() for product in products] return results, status.HTTP_200_OK
def test_find_by_category(self): """ Find Products by Category """ Product(1, "Couch", "White couch", "Furniture", 200, "Boxed", 50, " ", 8).save() Product(2, "Table", "Oak table", "Home", 150, "Boxed", 100, " ", 7).save() products = Product.find_by_category("Furniture") self.assertEqual(products[0].id, 1) self.assertEqual(products[0].category, "Furniture") self.assertEqual(products[0].name, "Couch") self.assertEqual(products[0].description, "White couch") self.assertEqual(products[0].price, 200) self.assertEqual(products[0].condition, "Boxed") self.assertEqual(products[0].inventory, 50) self.assertEqual(products[0].rating, 8)
def list_products(): """ Return all the products""" products = [] name = request.args.get('name') app.logger.info(name) category = request.args.get('category') id = request.args.get("id") if name: products = Product.find_by_name(name) elif category: products = Product.find_by_category(category) elif id: products = Product.find_by_id(id) else: products = Product.all() results = [product.serialize() for product in products] return make_response(jsonify(results), status.HTTP_200_OK)