def updateProductType(id): # parse json requestJson = request.get_json(force=True) # validate params sku = requestJson.get("sku") inventory = requestJson.get("inventory") price = requestJson.get("price") productType = models.ProductType.query.get(id) # update if productType: try: if productType and sku: # change sku productType.sku = sku if productType and inventory: # increase inventory by given amount productType.inventory = int(inventory) if productType and price: # set new price productType.price = helpers.parsePrice(price) db.session.commit() except exc.SQLAlchemyError as err: return jsonify(error="Failed to update ProductType"), 400 # publish update helpers.publishUpdate("UPDATE_PRODUCT_TYPE", dict(id=productType.id, inventory=productType.inventory)) # return result of any updates return jsonify(formatProductTypeForResponse(productType)), 200 else: return jsonify(error="Failed to find given ProductType"), 400
def createProductType(): # parse json requestJson = request.get_json(force=True) # validate params sku = requestJson.get("sku") inventory = requestJson.get("inventory") price = requestJson.get("price") if (not sku) or (not price): return jsonify(error="One or more required data is not set"), 400 if not inventory: inventory = 0 # convert dollar formatted price to number of cents price = helpers.parsePrice(price) productType = models.ProductType(sku, inventory, price) db.session.add(productType) try: db.session.commit() # check for errors, uniquness constratint except exc.IntegrityError as err: return jsonify(error="Duplicate ProductType name not allowed"), 400 except exc.SQLAlchemyError as err: return jsonify(error="Failed to create ProductType"), 400 # publish update helpers.publishUpdate( "CREATE_PRODUCT_TYPE", dict( id=productType.id, inventory=productType.inventory, price=helpers.convertIntToFormattedPrice(productType.price), sku=productType.sku, ), ) # return newly created productType return jsonify(formatProductTypeForResponse(productType)), 201