def update(product_id, product): """ This function updates an esixting product in database """ # Get the products requested from the db into session update_product = Product.query.filter( Product.product_id == product_id ).one_or_none() # Did we find an existing product? if update_product is not None: schema = ProductSchema() update = schema.load(product, session=db.session) update.product_id = update_product.product_id db.session.merge(update) db.session.commit() return schema.dump(update_product), 200 else: abort(404, f"Product is not found for the id: {product_id}")
def create_product(product): productName = PRODUCTS.get("productName") productDesc = PRODUCTS.get("productDesc") productCost = PRODUCTS.get("productCost") productCurrentSale = PRODUCTS.get("productCurrentSale") productStock = PRODUCTS.get("productStock") existing_product = (Products.query.filter( Products.productName == productName).filter( Products.productDesc == productDesc).filter( Products.productCost == productCost).filter( Products.productCurrentSale == productCurrentSale).filter( Products.productStock == productStock).one_or_none()) if existing_product is None: schema = ProductSchema() new_product = schema.load(product, session=db.session).data db.session.add(new_product) data = schema.dump(new_product).data return data, 201 else: abort( 409, "{productName} already exists".format(productName=productName), )
def update(product_id, product): """ This function updates an existing product in the products structure Throws an error if a product with the name we want to update to already exists in the database. :param product_id: Id of the product to update in the products structure :param product: product to update :return: updated product structure """ # Get the product requested from the db into session update_product = Product.query.filter( Product.product_id == product_id).one_or_none() # Try to find an existing product with the same name as the update name = product.get("name") inventory_level = product.get("inventory_level") existing_product = (Product.query.filter(Product.name == name).filter( Product.inventory_level == inventory_level).one_or_none()) # Are we trying to find a product that does not exist? if update_product is None: abort( 404, "product not found for Id: {product_id}".format( product_id=product_id), ) # Would our update create a duplicate of another product already existing? elif (existing_product is not None and existing_product.product_id != product_id): abort( 409, "Product {name} {inventory_level} exists already".format( name=name, inventory_level=inventory_level), ) # Otherwise go ahead and update! else: # turn the passed in product into a db object schema = ProductSchema() update = schema.load(product, session=db.session) # Set the id to the product we want to update update.product_id = update_product.product_id # merge the new object into the old and commit it to the db db.session.merge(update) db.session.commit() # return updated product in the response data = schema.dump(update_product) return data, 200
def create(product): """ This function creates a new product in the product structure based on the passed in product data :param product: product to create in products structure :return: 201 on success, 406 on product exists """ name = product.get("name") inventory_level = product.get("inventory_level") # we aren't this kind of database... # existing_product = ( # Product.query.filter(Product.name == name) # .filter(Product.inventory_level == inventory_level) # .one_or_none() # ) existing_product = None # Can we insert this product? if existing_product is None: # Create a new product id based on max value if there isn't an id already # this allows us to add a new inventory_level for an existing product. # Or create an entirely new product by leaving out the id. if "id" not in product: product["id"] = db.session.query(func.max(Product.id)).scalar() + 1 # Create a product instance using the schema and the passed in product schema = ProductSchema() new_product = schema.load(product, session=db.session) # Add the product to the database db.session.add(new_product) db.session.commit() # Serialize and return the newly created product in the response data = schema.dump(new_product) return data, 201 # Otherwise, nope, product exists already else: abort( 409, "Product {name} {inventory_level} exists already".format( name=name, inventory_level=inventory_level), )
def create(products): """ This function responds to a post request for /products """ schema = ProductSchema(many=True) new_products = schema.load(products, session=db.session) if not new_products: abort(400, "No products for creation are specified") db.session.add_all(new_products) db.session.commit() return schema.dump(new_products), 201
def update_product(productID, productName): update_products = Product.query.filter( Product.productID == productID).one_or_none() if update_products is not None: schema = ProductSchema() update = schema.load(productName, session=db.session).data update.productID = update_products.productID db.session.merge(update) db.session.commit() data = schema.dump(update_products).data return data, 200 else: abort( 404, "Product not found for ID: {productID}".format( productID=productID), )