def delete_fulfilledRequest(self, fulfilledRequest_id):
     dao = FulfilledRequestDAO()
     if not dao.getFulfilledRequestById(fulfilledRequest_id):
         return jsonify(Error="Post not found."), 404
     else:
         dao.delete(fulfilledRequest_id)
     return jsonify(DeleteStatus="OK"), 200
 def getAllFulfilledRequests(self):
     dao = FulfilledRequestDAO()
     fulfilledRequest_list = dao.getAllFulfilledRequests()
     result_list = []
     for row in fulfilledRequest_list:
         result = self.build_all_fulfilled_request_dict(row)
         result_list.append(result)
     return jsonify(All_Fulfilled_Requests=result_list)
 def getFulfilledRequestsByRequestId(self, sid):
     dao = FulfilledRequestDAO()
     fulfilledRequest_list = dao.getFulfilledRequestsByRequestId(sid)
     result_list = []
     for row in fulfilledRequest_list:
         result = self.build_fulfillment_info_dict(row, 3)
         result_list.append(result)
     return jsonify(Fulfillment_Info=result_list)
 def getFulfillmentStatisticsPerCategory(self):
     dao = FulfilledRequestDAO()
     fulfilledRequest_list = dao.getFulfillmentStatisticsPerCategory()
     result_list = []
     for row in fulfilledRequest_list:
         result = self.build_fulfillment_stat_dict(row, 3)
         result_list.append(result)
     return jsonify(Requests_Fulfullied_Per_Category=result_list)
 def getTotalFulfillmentsPerCategory(self):
     dao = FulfilledRequestDAO()
     fulfilledRequest_list = dao.getTotalFulfillmentsPerCategory()
     result_list = []
     for row in fulfilledRequest_list:
         result = self.build_fulfillment_stat_dict(row, 1)
         result_list.append(result)
     return jsonify(Fulfillments_Per_Category=result_list)
    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 update_fulfilledRequest(self, fulfilledRequest_id, unitprice,
                             quantity):
     dao = FulfilledRequestDAO()
     if not dao.getFulfilledRequestById(fulfilledRequest_id):
         return jsonify(Error="Post not found."), 404
     else:
         if int(quantity) <= 0:
             return jsonify(
                 Error="Cannot put non-positive value in quantity"), 400
         else:
             dao.update(fulfilledRequest_id, unitprice, quantity)
             row = dao.getFulfilledRequestById(fulfilledRequest_id)
             result = self.build_all_fulfilled_request_dict(row)
             return jsonify(Part=result), 200
 def getFulfilledRequestById(self, pid):
     dao = FulfilledRequestDAO()
     row = dao.getFulfilledRequestById(pid)
     result = self.build_fulfillment_info_dict(row, 1)
     return jsonify(Fulfillment_Info=result)
 def getTotalFulfillments(self):
     dao = FulfilledRequestDAO()
     amount = dao.getTotalFulfillments()
     return jsonify(Total_Fulfillments=amount)