def getSupplyById(supply_id): if request.method == 'GET': return SupplyHandler().get_supply_by_id(supply_id) elif request.method == 'PUT': return SupplyHandler().update_supply(supply_id, request.form) elif request.method == 'DELETE': return SupplyHandler().delete_supply(supply_id) else: return jsonify(Error="Method not allowed."), 405
def getAllSupplies(): if request.method == 'POST': # Mutually exclusive if request.json is not None and request.form is None: return SupplyHandler().insert_supply_json(request.json) elif request.form is not None and request.json is None: return SupplyHandler().insert_supply(request.form) else: return jsonify(Error="Malformed post request."), 400 elif request.method == 'GET': if not request.args: return SupplyHandler().get_all_supplies() else: return SupplyHandler().search_supplies(request.args) else: return jsonify(Error="Method not allowed."), 405
def insert_purchasedSupply(self, form): if len(form) != 3: return jsonify(Error="Malformed post request"), 400 else: dao = PurchasedSupplyDAO() supply_id = form['supply_id'] person_id = form['person_id'] pquantity = int(form['pquantity']) if person_id and supply_id and pquantity: supply = SupplyDAO().getSupplyById(supply_id) supply = SupplyHandler().build_supply_dict(supply) buyerAccountRow = AccountDAO().getAccountByPersonId(person_id) buyerAccount = AccountHandler().build_account_dict( buyerAccountRow) supplierAccountRow = AccountDAO().getAccountByPersonId( int(supply.get("person_id"))) supplierAccount = AccountHandler().build_account_dict( supplierAccountRow) if supply.get("available") < pquantity: return jsonify(Error="Insufficient stock"), 400 elif buyerAccount.get("balance") < (pquantity * supply.get("sunit_price")): return jsonify(Error="Insufficient funds"), 400 else: transactionTotal = pquantity * supply.get("sunit_price") new_available = supply.get("available") - pquantity newBuyerBalance = buyerAccount.get( "balance") - transactionTotal newSupplierBalance = supplierAccount.get( "balance") + transactionTotal purchasedSupply_id = dao.insert(supply_id, person_id, pquantity, supply.get("sunit_price")) SupplyDAO().updateStock(int(supply.get("supply_id")), new_available) AccountDAO().updateBalance( int(buyerAccount.get("account_id")), newBuyerBalance) AccountDAO().updateBalance( int(supplierAccount.get("account_id")), newSupplierBalance) result = self.build_purchased_supply_attributes( purchasedSupply_id, supply_id, person_id, pquantity, supply.get("sunit_price")) return jsonify(PurchasedSupply=result), 201 else: return jsonify( Error="Unexpected attributes in post request"), 400
def getAvailableSuppliesByPersonId(person_id): return SupplyHandler().get_available_supplies_by_person_id(person_id)
def getSuppliesByPersonId(person_id): return SupplyHandler().get_supplies_by_person_id(person_id)
def getAllAvailableSupplies(): if not request.args: return SupplyHandler().get_all_available_supplies() else: return SupplyHandler().search_available_supplies(request.args)
def getTotalAvailableSupplyCount(): return SupplyHandler().get_total_available_supplies()
def getTotalSupplyCount(): return SupplyHandler().get_total_supplies()