Example #1
0
    def post(self):
        payload = request.json
        data = ProductParams.parser.parse_args()
        try:
            product = Product.get_by_product_name(data['product_name'])
        except Exception as e:
            return make_response(jsonify({"message": e.args[0]}, 400))
        if product:
            return make_response(
                jsonify(
                    {'message': 'A product with that name already exists!'}),
                400)
        for i in payload['ingredients']:
            new_i = Ingredient.get_by_name(i)
            if new_i:
                # add to the list of ingredients
                self.ingredients.append(Ingredient(name=new_i.name).json())
            else:
                new_i = Ingredient(name=i)
                #fix check results of the insert
                new_i.save_to_mongo()
                self.ingredients.append(Ingredient(new_i.name).json())

        product = Product(product_name=data['product_name'])
        product.ingredients = self.ingredients
        #fixme return check for error
        product.save_to_mongo()
        return make_response(
            jsonify({"message": "Product Created Successfully "}, 201))
Example #2
0
 def test015_get_salt(self):
     x = Ingredient.get_by_name('salt_test')
     print(x)
     self.assertTrue(x.name == 'salt_test')
Example #3
0
 def test011_insert_ingredient(self):
     i = Ingredient("salt_test")
     Ingredient.save_to_mongo(i)
     self.assertTrue(Ingredient.get_by_name("salt_test") is not None)