def show_with_id(id): # PART 2 if request.method == 'GET': if db.getById('shows', int(id)) is None: return create_response(status=404, message="No show with this id exists") return create_response({"shows": db.get('shows')[int(id) - 1]}) if request.method == 'DELETE': if db.getById('shows', int(id)) is None: return create_response(status=404, message="No show with this id exists") db.deleteById('shows', int(id)) return create_response(message="Show deleted") # PART 4 if request.method == 'PUT': if db.getById('shows', int(id)) is None: return create_response(status=404, message="No show with this id exists") else: # store values recevied in corresponding variables data_json = request.get_data() data = json.loads(data_json) # if name and episodes are not provided, keep orignal values if data['name'] == "": data['name'] = db.getById('shows', int(id))['name'] if data['episodes_seen'] == "": data['episodes_seen'] = db.getById('shows', int(id))['episodes_seen'] # update show with corresponding ID db.updateById('shows', int(id), data) # return results return create_response({"shows": db.get('shows')[int(id) - 1]}, status=201)
def get_all_restaurants(): restaurants = db.get('restaurants') minRating = request.args.get('minRating') if args.get('rating') > minRating filtered_restaurants = [db.get('restaurants')] if filtered_restaurants is None: return create_response(status=404, message="No restaurant with this/above this rating exits") return create_response({"restaurants": filtered_restaurants}) @app.route("/restaurants/<id>", methods=['GET']) def get_restaurant(id): restaurant = db.getById('restaurants', int(id)) if restaurant is None: return create_response(status=404, message="No restaurant with this id exits") return create_response(restaurant) @app.route("/restaurants/<id>", methods=['DELETE']) def delete_restaurant(id): if db.getById('restaurants', int(id)) is None: return create_response(status=404, message="No restaurant with this id exists") db.deleteById('restaurants', int(id)) return create_response(message="Restaurant deleted") # TODO: Implement the rest of the API here! """ ~~~~~~~~~~~~ END API ~~~~~~~~~~~~ """ if __name__ == "__main__": app.run(host='0.0.0.0', port=8080, debug=True)
def shows(): # GET Request if request.method == 'GET': return create_response({"shows": db.get('shows')}) # POST Request if request.method == 'POST': # Access data you get from post request and store it in variables data_json = request.get_data() data = json.loads(data_json) data_name = data['name'] data_episodes_seen = data['episodes_seen'] if data_name != "" or data_episodes_seen >= 0: # add it to the database db.create('shows', data) # get the list of shows list_of_shows = db.get('shows') #find out size of list and get id of show just added size_of_list = len(list_of_shows) id_number_of_show_added = list_of_shows[size_of_list - 1]['id'] #display the show that has been added with status code 201 return create_response( {"shows": db.get('shows')[int(id_number_of_show_added) - 1]}, status=201) else: return create_response( status=422, message= "Error, make sure you include name and episodes seen of the TV show" )
def get_all_shows(): if "minEpisodes" in request.args: minEps = int(request.args["minEpisodes"]) filteredShows = [ x for x in db.get('shows') if int(x['episodes_seen']) >= minEps ] return create_response({"shows": filteredShows}) return create_response({"shows": db.get('shows')})
def get_all_shows(): minEpisodes = request.args.get('minEpisodes') if minEpisodes is not None: minEpisodes = int(minEpisodes) shows = db.get('shows') new_shows = list(filter(lambda x: minEpisodes <= x.get('episodes_seen'), shows)) if not new_shows: return create_response({"response":"there is no show that has that much episodes seen!"}) return create_response({"shows": new_shows}) return create_response({"shows": db.get('shows')})
def get_all_shows(): minEpisodes = request.args.get('minEpisodes') if minEpisodes is None: return create_response({"shows": db.get('shows')}) minEpisodes = int(minEpisodes) shows = {"shows": []} for i in db.get("shows"): if i["episodes_seen"] >= minEpisodes: shows["shows"].append(i) return create_response({"shows": shows["shows"]}, status=200)
def get_all_restaurants(): if "minRating" not in request.args: return create_response({"restaurants": db.get('restaurants')}) minRating = int(request.args["minRating"]) all_restaurants = db.get('restaurants') filtered_restaurants = [ restaurant for restaurant in all_restaurants if restaurant["rating"] >= minRating ] return create_response({"restaurants": filtered_restaurants})
def get_minEpisodes(): if request.args.get('minEpisodes'): minEpisodes = request.args.get('minEpisodes') temp = list() for i in db.get("shows"): if i['episodes_seen'] >= int(minEpisodes): temp.append(i) return create_response({"shows": temp}) else: return create_response({"shows": db.get('shows')})
def get_all_contacts(): hobby = request.args.get('hobby') if (id is None): return create_response({"contacts": db.get('contacts')}) else: relevant_contacts = [] for obj in db.get('contacts'): if (obj.get('hobby') == hobby): relevant_contacts.append(obj) return create_response({"contacts": relevant_contacts})
def get_all_restaurants(): restaurants = db.get('restaurants') minRating = request.args.get('minRating') if minRating is None: return create_response({"restaurants": db.get('restaurants')}) filtered_restaurants = [] for restaurant in restaurants: rating = restaurant.get('rating') if rating >= int(minRating): filtered_restaurants.append(restaurant) return create_response({"restaurants": filtered_restaurants})
def get_all_shows(): minEpisodes = request.args.get("minEpisodes") if minEpisodes is not None: minEpisodes = int(minEpisodes) data = list( filter(lambda i: int(i["episodes_seen"]) >= minEpisodes, db.get('shows'))) return create_response({"result": data}, status=200) return create_response({"shows": db.get('shows')})
def get_contacts(): hobby = request.args.get('hobby') if (hobby): contacts = [c for c in db.get('contacts') if c['hobby'] == hobby] if (contacts): return create_response({"contacts": contacts}) else: return create_response(status=404, message="No contact with this hobby exists") else: return create_response({"contacts": db.get('contacts')})
def get_all_shows(): if 'minEpisodes' in request.args: minEpisodes = int(request.args.get('minEpisodes')) else: return create_response({'shows': db.get('shows')}) newDict = {'shows' : []} for i in db.get('shows'): if (i["episodes_seen"] >= minEpisodes): newDict['shows'].append(i) if newDict['shows']: return create_response({"shows": list(newDict.values())}) return create_response(status=404, message="No show with at least this amount of episodes seen exists")
def get_all_shows(): min_episodes = request.args.get('minEpisodes') if min_episodes is None: return create_response({"shows": db.get('shows')}) else: display_shows = dict() display_shows['shows'] = [] for show in db.get('shows'): if show['episodes_seen'] >= int(min_episodes): display_shows['shows'].append(show) if len(display_shows['shows']) == 0: return create_response(status=200, message="No such shows found") return create_response(data=display_shows)
def get_all_shows(): minEpisodes = request.args.get('minEpisodes') if not minEpisodes: return create_response({"shows": db.get('shows')}) else: shows = db.get('shows') filtered_shows = [] for show in shows: if show["episodes_seen"] >= int(minEpisodes): filtered_shows.append(show) if len(filtered_shows) == 0: return create_response(message="No shows fit the requirement") return create_response({"shows": filtered_shows}, message="Shows found")
def get_all_restaurants(): min_rating = request.args.get('minRating') if min_rating is not None: restaurants = db.get('restaurants') filtered_restauraunts = [ restaurant for restaurant in db.get('restaurants') if restaurant['rating'] >= int(min_rating) ] if len(filtered_restauraunts) == 0: return create_response( status=404, message="No restaurants have a rating at or above " + min_rating) return create_response({"restaurants": filtered_restauraunts}) return create_response({"restaurants": db.get('restaurants')})
def get_contacts(): hobby = request.args.get('hobby') if (hobby is None): return create_response({"contacts": db.get('contacts')}) if db.getByHobby('contacts', hobby) is None: return create_response(status=404, message="No contact with this id exists") return create_response({"contacts": db.getByHobby('contacts', hobby)})
def get_all_shows(): minEpisodes = request.args.get("minEpisodes", None) try: minEpisodes = int(minEpisodes) except ValueError: return create_response( status=422, message="Invalid argument for minEpisodes. Expected type int") if minEpisodes is None or not isinstance(minEpisodes, int): return create_response({"shows": db.get('shows')}) return create_response({ "shows": [ show for show in db.get('shows') if show["episodes_seen"] >= int(minEpisodes) ] })
def get_all_shows(): show_list = [] minep = request.args.get('minEpisodes') for show in db.get('shows'): if show['episodes_seen'] >= int(minep): show_list.append(show) return create_response({"shows": show_list})
def get_all_shows(): minEpisodes = request.args.get('minEpisodes') data = db.get('shows') if minEpisodes == None: return create_response({"shows": data}) new_data = [x for x in data if x['episodes_seen'] >= int(minEpisodes)] return create_response({"shows": new_data})
def get_all_shows(): shows = db.get('shows') minEpisodes = request.args.get('minEpisodes') if minEpisodes != None: minEpisodes = int(minEpisodes) shows = [show for show in shows if show["episodes_seen"] >= minEpisodes] return create_response({"shows": shows})
def get_all_contacts(): hobby = request.args.get("hobby") if hobby and db.getByHobby(hobby) != None: return create_response(status = 201, result = {"contacts": db.getByHobby(hobby)}) elif db.getByHobby(hobby) == None: return create_response(status = 404, message = "No contact with this hobby exists") return create_response(result = {"contacts": db.get('contacts')})
def get_all_shows(): minEpisodes = request.args.get("minEpisodes") shows = db.get("shows") if minEpisodes is not None: minEpisodes = int(minEpisodes) shows = list(filter(lambda x: x["episodes_seen"] > minEpisodes, shows)) return create_response({"shows": shows})
def return_shows_with_min_episodes(): # obtain the query argument minEpisodes_string = request.args.get("minEpisodes") # if no query argument is provided, return all shows if minEpisodes_string == None: return create_response({"shows": db.get('shows')}) # convert the string to an integer minEpisodes_int = int(minEpisodes_string) shows = db.get("shows") #initialize an empty list and add relevant shows relevant_shows = [] for show in shows: if show["episodes_seen"] >= minEpisodes_int: relevant_shows.append(show) return create_response(data={"shows": relevant_shows}, message="Shows with more than {} episode(s) seen found!".format(minEpisodes_int))
def get_all_restaurants(): minRating = request.args.get('minRating') if minRating is None: return create_response({"restaurants": db.get('restaurants')}) minRating = int(minRating) filtered_restaurants = [] for check_restaurant in db.get('restaurants'): if check_restaurant.get("rating") >= minRating: filtered_restaurants.append(check_restaurant) if not filtered_restaurants: return create_response( status=404, message="No restaurants are found with this rating or above") return create_response({"restaurants": filtered_restaurants})
def get_all_restaurants(): if request.args: restaurants = db.get("restaurants") if "minRating" in request.args: filtered_res = [] min_rate = request.args["minRating"] min_rate = int(min_rate) for r in restaurants: if r["rating"] >= min_rate: filtered_res.append(r) if filtered_res: return create_response({"restaurants": filtered_res}) else: return create_response( status=404, message="No restaurant with satisfying the query exists") return create_response({"restaurants": db.get("restaurants")})
def get_all_shows(): min_episodes_query = request.args.get("minEpisodes", -1, int) shows = db.get("shows") # filter the list of shows filtered_shows = list( filter(lambda show: show["episodes_seen"] >= min_episodes_query, shows)) return create_response(data={"shows": filtered_shows}, message="Shows retrieved")
def get_all_shows(): shows = db.get('shows') if request.args.get("minEpisodes"): min_episodes = int(request.args.get("minEpisodes")) filtered_shows = [ show for show in shows if int(show["episodes_seen"]) > min_episodes ] return create_response({"shows": filtered_shows}) return create_response({"shows": shows})
def get_contacts(): hobby = request.args.get('hobby') # Retrieves hobby query param if hobby is not None: # Checks if a hobby param existed if db.getByHobby('contacts', hobby) is None: return create_response(status=404, message="No contact with this hobby exists") return create_response({"contacts": db.getByHobby('contacts', hobby)}) return create_response({"contacts": db.get('contacts') }) # If no hobby param existed, this runs
def get_all_contacts(): hobby = request.args.get('hobby') if (hobby != ""): return get_contacts_by_hobby(hobby) else: return create_response({"contacts": db.get('contacts')})