def get_contacts_for_id(id): print("I am ashwin" + id) if db.getById('contacts', int(id)) is None: return create_response(status=404, message="No contact with this id exists") # Handle 404. Do db.getById first, check if it's null, if it is do a 404. return create_response({"contacts": db.getById('contacts', int(id))})
def put_show(id): #If requested id is not found if db.getById('shows', int(id)) is None: return create_response(status=404, message="The requested URL was not found.") #Get data from Postman payload = request.json #Store original show data if only 1 variable is to be updated originalShow = db.getById("shows", int(id)) originalName = originalShow["name"] originalEp = str(originalShow["episodes_seen"]) #Check if name or episodes_seen is to be updated if "name" in payload: name = payload["name"] else: name = originalName if "episodes_seen" in payload: episodes_seen = payload["episodes_seen"] else: episodes_seen = originalEp #Format data into dict updateShow = {"id": int(id), "name": name, "episodes_seen": episodes_seen} #Update show updatedShow = db.updateById('shows', int(id), updateShow) #Return updated show return jsonify(updatedShow)
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 part256(id): if request.method == 'GET': if db.getbyId('users', int(id)) is None: return create_response(status=400, message="Not found") else: json = { 'users': db.getId('users') } return create_response(json) if request.method == 'PUT': json = {'name': request.form['name'], 'age': request.form['age'], 'team': request.form['team']} if db.updateById('users', int(id), json) is None: return create_response(status=400, message="not found") else: return create_response(db.updateById('users', id, json)) if request.method == 'DELETE': if db.getById('users', int(id)) is None: return create_response(status=400, message="Not found") if db.getById('users', int(id)) is None: return create_response(None, 404, "User cannot be found") else: json = { 'user': db.getById('users', int(id)) } return create_response(json)
def get_single_contact_from_id(id): if db.getById('contacts', int(id)) is None: return create_response(status=404, message="The contact with id " + str(id) + " was not found.") return create_response({'contact': db.getById('contacts', int(id))})
def get_show(id): if not id.isdigit(): return create_response(status=404, message="No show with this id exists") if db.getById('shows', int(id)) is None: return create_response(status=404, message="No show with this id exists") return create_response(result=db.getById('shows', int(id)))
def update_show(id): updateShow = request.get_json(force=True) if updateShow is None: return create_response(status=422, message="Please enter JSON with 'name' and/or 'episodes_seen' fields") elif db.getById('shows', int(id)) is None: return create_response(status=404, message="Please enter JSON with 'id' that exists in the database") else: db.updateById('shows', int(id), updateShow) return create_response(status=201, data=db.getById('shows', int(id)))
def get_show(id): #If requested id is not found if db.getById('shows', int(id)) is None: return create_response(status=404, message="The requested URL was not found.") #Return requested show return create_response(data=db.getById("shows", int(id)))
def updateContact(id): if db.getById('contacts', int(id)) is None: return create_response(status=404, message="No contact with this id exists") name = request.json.get("name") hobby = request.json.get("hobby") if name is not None: db.updateById('contacts', int(id), {"name": name}) if hobby is not None: db.updateById('contacts', int(id), {"hobby": hobby}) return create_response(status = 201, data = {"contacts": db.getById('contacts', int(id))})
def updateShow(id): if db.getById('shows', int(id)) is None: return create_response(status=404, message="No show with this id exists") else: data = db.getById('shows',2) if request.args["param1"] != "": data['name']=request.args["param1"] if request.args["param2"] != "": data['episodes_seen']=request.args["param2"] return create_response(data)
def put_show(id): int_id = int(id) updated_show = request.json old_show = db.getById('shows', int_id) if old_show is None: return create_response(status=404, message="No show found with that id") print(updated_show) db.updateById('shows', int_id, updated_show) return create_response(db.getById('shows', int_id), 201)
def modify_show(id): if db.getById('shows', int(id)) is None: return create_response(status=404, message="No show with this id exists") info = dict() if "name" in request.json: info["name"] = request.json["name"] if "episodes_seen" in request.json: info["episodes_seen"] = request.json["episodes_seen"] db.updateById('shows', int(id), info) return create_response(data=db.getById('shows', int(id)))
def get_contact_from_id(id): if db.getById('contacts',int(id)) is None: return create_response(status = 404, message = "No contact with this id exists") elif request.method == 'GET': return create_response(result = {"contact":db.getById(contacts, int(id))}) elif request.method == 'PUT': data = request.json() cont = db.getById(contacs,int(id)) cont["name"] = data["name"] cont["hobby"] = data["hobby"] return create_response(status = 201,result = {"contact":cont})
def update_contact(id): if db.getById('contacts', int(id)) is None: return create_response(status=404, message="No contact with this id exists") data = request.json if check_if_key_exists(data, "name") or check_if_key_exists(data, "hobby"): return create_response( status=201, data={"contacts": db.updateById('contacts', int(id), data)}) return create_response({"contacts": db.getById('contacts', int(id))})
def update_contact(id): if db.getById('contacts', int(id)) is None: return create_response(status=404, message="No contact with this id exists") data = request.json keys = list(data.keys()) for key in keys: if (key != "name") and (key != "hobby"): del data[key] db.updateById('contacts', int(id), data) return create_response(db.getById('contacts', int(id)), status=201)
def put_show(id): if db.getById('shows', int(id)) is None: return create_response(status=404, message="No show with this id exists") name = request.form.get('name') episodes_seen = request.form.get('episodes_seen') if ((name is None) and (episodes_seen is None)): return create_response(db.getById('shows', int(id)), message="Show returned unchanged") if name is None: return create_response(db.updateById('shows', int(id), {"episodes_seen" : episodes_seen})) if episodes_seen is None: return create_response(db.updateById('shows', int(id), {"name" : name})) return create_response(db.updateById('shows', int(id), {"name" : name, "episodes_seen" : episodes_seen}))
def update_show(id): if db.getById('shows', int(id)) is None: return create_response(status=404, message="No show with this id exists") na = request.form['name'] if na is not "": db.updateById("shows", int(id), {"name": na}) ep = request.form['episodes_seen'] if ep is not "": db.updateById("shows", int(id), {"episodes_seen": ep}) sh = db.getById('shows', int(id)) return create_response(sh)
def get_contacts_id(id): if db.getById('contacts', int(id)) is None: return create_response(status=404, message="No contact with this id exists") if request.method == 'GET': return create_response(db.getById('contacts', int(id))) if request.method == 'PUT': body = request.json valid = ['name', 'hobby'] update = {key: body[key] for key in valid} print(update) return create_response(db.updateById('contacts', int(id), update))
def post_single_contact(id): name = request.args.get('name') nickname = request.args.get('nickname') hobyy = request.args.get('hobby') contact_data = { 'name': name, 'age': age, 'id': len(db.get('contacts')) + 1, 'team': team } if db.getById('contacts', int(id)) is None: return create_reponse(status=201,contact_data); else: db.getById(id).append(contact_data); return create_reponse(status=404,message = "There are no users with the id");
def update_show(id): # if the show with the provided id does not exist if db.getById("shows", int(id)) is None: return create_response(status=404, message="No show with this id exists!") show = db.getById("shows", int(id)) # received request.json file data = request.json # obtain the updated name and episodes_seen name = data.get("name", "") episodes_seen = data.get("episodes_seen", "") #update the values from the corresponding show in our database show["name"] = name show["episodes_seen"] = episodes_seen return create_response(data=show, status=201, message="Show updated!")
def get_restaurant(id): restaurant = db.getById('restaurants', id) if restaurant is None: return create_response(status=404, message="No restaurant with this id exists") return create_response(restaurant)
def get_single_show(id): show = db.getById("shows", int(id)) if show is None: return create_response( status=404, message="There doesn't exist a show with the provided id!") return create_response(status=200, message="", data=show)
def update_show(id): body = request.get_json() if db.getById('shows', int(id)) is None: return create_response(status=404, message="No show with this id exists") updated_show = db.updateById('shows', int(id), body) return create_response(updated_show)
def update_restaurant(id): if db.getById('restaurants', int(id)) is None: return create_response(status=404, message="No restaurant with this id exists") update_restaurant_data = request.get_json() if not update_restaurant_data.get("name") is None: new_name_payload = {"name": update_restaurant_data.get("name")} db.updateById('restaurants', int(id), new_name_payload) if not update_restaurant_data.get("rating") is None: new_rating_payload = { "rating": int(update_restaurant_data.get("rating")) } db.updateById('restaurants', int(id), new_rating_payload) return create_response(status=201, data=db.getById('restaurants', int(id)))
def update_show(id): if db.getById('shows', int(id)) is None: return create_response(status=404, message="No show with this id exists") data = request.json name = db.getById('shows', int(id))["name"] episodes_seen = db.getById('shows', int(id))["episodes_seen"] if name is not None: name = data["name"] if episodes_seen is not None: episodes_seen = int(data["episodes_seen"]) item = db.updateById('shows', int(id), { "name": name, "episodes_seen": int(episodes_seen) }) return create_response({"result": item}, status=201)
def get_contact_by_id(id): contact = db.getById('contacts', int(id)) if (contact): return create_response(contact) else: return create_response(status=404, message="No contact with this id exists")
def edit_show(id): if db.getById('shows', int(id)) is None: return create_response(status=404, message="No show with this id exists") req_data = request.get_json() if not 'name' in req_data and not 'episodes_seen' in req_data: return create_response(status=201, message="Nothing was changed", data=db.getById('shows', int(id))) my_dict = {} if 'name' in req_data: my_dict['name'] = req_data['name'] if 'episodes_seen' in req_data: my_dict['episodes_seen'] = req_data['episodes_seen'] updated_show = db.updateById('shows', int(id), my_dict) return create_response(status=201, data=updated_show)
def update_show(id): if db.getById('shows', int(id)) is None: return create_response(status=422, message="There is no tv show with that id that exists") input_data = request.get_json() if input_data is None: return create_response(status=422, message="Incorrect input format") db.updateById('shows', int(id), input_data) return create_response(input_data, status=201)
def update_show(id): req = request.get_json() item = db.updateById('shows', int(id), req) if item is None: return create_response(status=404, message="No show with this id exists") return create_response(status=201, data={"shows": db.getById('shows', int(id))})
def get_show_by_id(id): show = db.getById('shows', int(id)) if show is None: return create_response(status=404, message="No show with this id exists") else: return create_response({"shows": show}, message="Show successfully displayed")