def deleteBuyer(self, b_id): dao = BuyerDAO() if not dao.getBuyerById(b_id): return jsonify(Error = "Buyer not found."), 404 else: dao.delete(b_id) return jsonify(DeleteStatus = "OK"), 200
def getBuyerById(self, b_id): dao = BuyerDAO() buyer_list = dao.getBuyerById(b_id) if not buyer_list: return jsonify(Error="Buyer Not Found"), 404 else: result = self.build_buyer_dict(buyer_list) return jsonify(Buyer=result)
def searchBuyers(self, args): name = args.get("name") lastname = args.get("lastname") region = args.get("region") dao = BuyerDAO() buyer_list = [] if (len(args) == 3) and name and lastname and region: buyer_list = dao.getBuyerByRegionNameAndLastName(region, name,lastname) elif (len(args) == 2) and name and lastname: buyer_list = dao.getBuyerByNameandLastName(name, lastname) elif (len(args) == 2) and name and region: buyer_list = dao.getBuyerByNameandRegion(name, region) elif (len(args) == 2) and lastname and region: buyer_list = dao.getBuyerByLastNameandRegion(lastname, region) elif (len(args) == 1) and name: buyer_list = dao.getBuyerByName(name) elif (len(args) == 1) and lastname: buyer_list = dao.getBuyerByLastName(lastname) elif (len(args) == 1) and region: buyer_list = dao.getBuyerByRegion(region) else: return jsonify(Error = "Malformed query string"), 400 if not buyer_list: return jsonify(Error="Buyer Not Found"), 404 else: result_list = [] for row in buyer_list: result = self.build_buyer_dict(row) result_list.append(result) return jsonify(Buyers=result_list)
def getTransactionsByBuyerId(self, b_id): dao = BuyerDAO() transaction_list = dao.getTransactionsByBuyerId(b_id) if not transaction_list: return jsonify(Error="Transaction Not Found"), 404 else: result_list = [] for row in transaction_list: result = self.build_transaction_dict(row) result_list.append(result) return jsonify(Transactions=result_list)
def getResourcesByBuyerId(self, b_id): dao = BuyerDAO() buyer_list = dao.getResourcesByBuyerId(b_id) if not buyer_list: return jsonify(Error="Buyer Not Found"), 404 else: result_list = [] for row in buyer_list: result = self.build_resource_dict(row) result_list.append(result) return jsonify(Resources=result_list)
def getAllBuyers(self): dao = BuyerDAO() buyer_list = dao.getAllBuyers() if not buyer_list: return jsonify(Error="Buyer Not Found"), 404 else: result_list = [] for row in buyer_list: result = self.build_buyer_dict(row) result_list.append(result) return jsonify(Buyers=result_list)
def insertBuyer(self, form): if len(form) != 9: return jsonify(Error = "Malformed post request"), 400 else: u_name = form['u_name'] u_lastname = form['u_lastname'] u_email = form['u_email'] u_password = form['u_password'] u_address = form['u_address'] u_city = form['u_city'] u_region = form['u_region'] u_phone = form['u_phone'] u_age = form['u_age'] if u_name and u_lastname and u_email and u_password and u_address and u_city and u_region and u_phone and u_age: dao = BuyerDAO() b_id = dao.insert(u_name, u_lastname, u_email, u_password, u_address, u_city, u_region, u_phone, u_age) result = self.build_buyer_attributes(b_id, u_name, u_lastname, u_email, u_password, u_address, u_city, u_region, u_phone, u_age) return jsonify(Buyer=result), 201 else: return jsonify(Error="Unexpected attributes in post request"), 400
def updateBuyer(self, b_id, form): dao = BuyerDAO() if not dao.getBuyerById(b_id): return jsonify(Error = "Buyer not found."), 404 else: if len(form) != 9: return jsonify(Error="Malformed update request"), 400 else: u_name = form['u_name'] u_lastname = form['u_lastname'] u_email = form['u_email'] u_password = form['u_password'] u_address = form['u_address'] u_city = form['u_city'] u_region = form['u_region'] u_phone = form['u_phone'] u_age = form['u_age'] if u_name and u_lastname and u_email and u_password and u_address and u_city and u_region and u_phone and u_age: dao = BuyerDAO() dao.update(b_id, u_name, u_lastname, u_email, u_password, u_address, u_city, u_region, u_phone, u_age) result = self.build_buyer_attributes(b_id, u_name, u_lastname, u_email, u_password, u_address, u_city, u_region, u_phone, u_age) return jsonify(Buyer=result), 201 else: return jsonify(Error="Unexpected attributes in update request"), 400