def getRequestById(request_id): if request.method == 'GET': return RequestHandler().get_request_by_id(request_id) elif request.method == 'PUT': return RequestHandler().update_request(request_id, request.form) elif request.method == 'DELETE': return RequestHandler().delete_request(request_id) else: return jsonify(Error="Method not allowed."), 405
def getAllRequests(): if request.method == 'POST': if request.json is not None and request.form is None: return RequestHandler().insert_request_json(request.json) elif request.form is not None and request.json is None: return RequestHandler().insert_request(request.form) else: return jsonify(Error="Malformed post request") elif request.method == 'GET': if not request.args: return RequestHandler().get_all_requests() else: return RequestHandler().search_requests(request.args) else: return jsonify(Error="Method not allowed."), 405
def insert_fulfilledRequest(self, form): if len(form) != 3: return jsonify(Error="Malformed post request"), 400 else: dao = FulfilledRequestDAO() request_id = form['request_id'] person_id = form['person_id'] fquantity = int(form['fquantity']) if person_id and request_id and fquantity: requestRow = RequestDAO().getRequestById(request_id) request = RequestHandler().build_request_dict(requestRow) sellerAccountRow = AccountDAO().getAccountByPersonId(person_id) sellerAccount = AccountHandler().build_account_dict( sellerAccountRow) buyerAccountRow = AccountDAO().getAccountByPersonId( int(request.get("person_id"))) buyerAccount = AccountHandler().build_account_dict( buyerAccountRow) if request.get("needed") < fquantity: return jsonify(Error="Resource overflow"), 400 elif buyerAccount.get("balance") < ( fquantity * request.get("max_unit_price")): return jsonify(Error="Insufficient funds"), 400 else: transactionTotal = fquantity * request.get( "max_unit_price") new_needed = request.get("needed") - fquantity newSellerBalance = sellerAccount.get( "balance") + transactionTotal newBuyerBalance = buyerAccount.get( "balance") - transactionTotal fulfilledRequest_id = dao.insert( request_id, person_id, fquantity, request.get("max_unit_price")) RequestDAO().updateStock(int(request.get("request_id")), new_needed) AccountDAO().updateBalance( int(sellerAccount.get("account_id")), newSellerBalance) AccountDAO().updateBalance( int(buyerAccount.get("account_id")), newBuyerBalance) result = self.build_fulfilled_request_attributes( fulfilledRequest_id, request_id, person_id, fquantity, request.get("max_unit_price")) return jsonify(FulfilledRequest=result), 201 else: return jsonify( Error="Unexpected attributes in post request"), 400
def getNeededRequestsByPersonId(person_id): return RequestHandler().get_needed_requests_by_person_id(person_id)
def getRequestsByPersonId(person_id): return RequestHandler().get_requests_by_person_id(person_id)
def getAllNeededRequests(): if not request.args: return RequestHandler().get_all_needed_requests() else: return RequestHandler().search_needed_requests(request.args)
def getTotalNeededRequestCount(): return RequestHandler().get_total_needed_requests()
def getTotalRequestCount(): return RequestHandler().get_total_requests()