def __getMatchingCars(self, searchQuery): matchingCars = [] for i in range(len(Car.attributesAsList()) - 1): matchingCars.extend( self.__getCarsByFilterParam(Car.attributesAsList()[i], searchQuery)) return matchingCars
def createCar(self, params): # construct a car object from the gathered data car = Car(params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8]) # update the database with the new car and respond with a confirmation of insertion db.insertObject('cars', Car.attributesAsList(), car.asTuple()) return jsonify({'car': car.asDict()}), 201
def getCars(self, params=None): cars = [] # with no optional params all cars can be fetched if not params: carTuples = db.getAllObjects('cars') # otherwise only cars matching the params will be fetched else: for param in params: if param not in Car.attributesAsList(): return jsonify({'cars': []}) # get all cars that match the filter carTuples = db.getObjectsByFilter('cars', list(params.keys()), list(params.values())) # contsruct the fetched cars as objects for i in range(len(carTuples)): cars.append( Car(carTuples[i][0], carTuples[i][1], carTuples[i][2], carTuples[i][3], carTuples[i][4], carTuples[i][5], carTuples[i][6], carTuples[i][7], carTuples[i][8])) # jsonify each car object's dict return jsonify(cars=[car.asDict() for car in cars])
return fieldsToUpdate, params # general routes @app.route(generateURI(), methods=['GET']) def index(): return 'This is the home page for the CSS API.' @app.errorhandler(404) def not_found(error): return make_response(jsonify({'error': 'Not found'}), 404) # api calls regarding the car object carParams = Car.attributesAsList() @app.route(generateURI('cars', True), methods=['GET']) def getCar(id): return carsController.getCar(id) @app.route(generateURI('cars'), methods=['GET']) def getCars(): # if this request contains parameters call a modified get all function if request.args: return carsController.getCars(request.args) return carsController.getCars()