def getAllReservedBatteries(self): dao = BatteryDAO() result = dao.getAllReservedBatteries() result_list = [] for row in result: result = self.build_battery_dict(row) result_list.append(result) return jsonify(Batteries = result_list)
def getBatteryByResourceId(self, resource_id): dao = BatteryDAO() row = dao.getBatteryByResourceId(resource_id) if not row: return jsonify(Error = "Battery Not Found"), 404 else: battery = self.build_battery_dict(row) return jsonify(Battery = battery)
def deleteBaterry(self, battery_id): battery_dao = BatteryDAO() if not battery_dao.getBatteryById(battery_id): return jsonify(Error = "Battery not found."), 404 else: resource_id = battery_dao.delete(battery_id) res_dao = ResourceDAO() res_dao.delete(resource_id) return jsonify(DeleteStatus = "OK"), 200
def getBatteriesBySupplierId(self, supplier_id): supplier_dao = SupplierDAO() if not supplier_dao.getSupplierById(supplier_id): return jsonify(Error = "Supplier Not Found"), 404 else: battery_dao = BatteryDAO() result_list = [] battery_list = battery_dao.getBatteriesBySupplierId(supplier_id) for row in battery_list: result = self.build_battery_dict(row) result_list.append(result) return jsonify(Batteries = result_list)
def getBatteryAddress(self, battery_id): battery_dao = BatteryDAO() try: supplier_id = battery_dao.getBatteryById(battery_id)[5] except Exception: return jsonify(Error = "Battery not found."), 404 supplier_dao = SupplierDAO() if not supplier_dao.getSupplierById(supplier_id): return jsonify(Error = "Supplier not found."), 404 else: row = battery_dao.getBatteryAddress(supplier_id) if not row: return jsonify(Error = "Address not found."), 404 else: address = self.build_address_dic(row) return jsonify(Address = address)
def searchBatteries(self, args): battery_power_capacity = args.get('power_capacity') battery_power_condition = args.get('power_condition') battery_type = args.get('battery_type') dao = BatteryDAO() battery_list = [] if (len(args) == 1) and battery_power_capacity: battery_list = dao.getBatteriesByPowerCapacity(battery_power_capacity) elif (len(args) == 1) and battery_power_condition: battery_list = dao.getBatteriesByPowerCondition(battery_power_condition) elif (len(args) == 1) and battery_type: battery_list = dao.getBatteriesByType(battery_type) else: return jsonify(Error = "Malformed query string"), 400 result_list = [] for row in battery_list: result = self.build_battery_dict(row) result_list.append(result) return jsonify(Batteries = result_list)
def insertBattery(self, json): supplier_id = json['supplier_id'] category_id = json['category_id'] battery_name = json['battery_name'] battery_brand = json['battery_brand'] battery_quantity = json['battery_quantity'] battery_price = json['battery_price'] power_capacity = json['power_capacity'] power_condition =json['power_condition'] battery_type =json['battery_type'] if supplier_id and category_id and battery_name and battery_brand and battery_quantity and (battery_price>=0) and power_capacity and power_condition and battery_type: res_dao = ResourceDAO() resource_id = res_dao.insert(supplier_id, category_id, battery_name, battery_brand, battery_quantity, battery_price) battery_dao = BatteryDAO() battery_id = battery_dao.insert(resource_id, power_capacity, power_condition, battery_type) result = self.build_battery_attributes(supplier_id, resource_id, battery_id, category_id, battery_name, battery_brand, battery_quantity, battery_price, power_capacity, power_condition, battery_type) return jsonify(Battery = result), 201 else: return jsonify(Error = "Unexpected attributes in post request"), 400
def updateBattery(self, battery_id, json): battery_dao = BatteryDAO() if not battery_dao.getBatteryById(battery_id): return jsonify(Error = "Battery not found."), 404 else: supplier_id = json['supplier_id'] category_id = json['category_id'] battery_name = json['battery_name'] battery_brand = json['battery_brand'] battery_quantity = json['battery_quantity'] battery_price = json['battery_price'] power_capacity = json['power_capacity'] power_condition = json['power_condition'] battery_type = json['battery_type'] if supplier_id and category_id and battery_name and battery_brand and battery_quantity and (battery_price>=0) and power_capacity and power_condition and battery_type: resource_id = battery_dao.update(battery_id, power_capacity, power_condition, battery_type) res_dao = ResourceDAO() res_dao.update(resource_id, supplier_id, category_id, battery_name, battery_brand, battery_quantity, battery_price) result = self.build_battery_attributes(supplier_id, resource_id, battery_id, category_id, battery_name, battery_brand, battery_quantity, battery_price, power_capacity, power_condition, battery_type) return jsonify(Battery = result), 200 else: return jsonify(Error = "Unexpected attributes in update request"), 400