Exemple #1
0
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 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)
Exemple #3
0
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)
Exemple #4
0
def update_show_by_id(id):
    print(id)
    content = request.get_json()
    if db.getById('shows', int(id)) is None:
        return create_response(status=404,
                               message="No show with this id exists")
    db.updateById('shows', int(id), content)
    return create_response(status=201, message="Successfully updated show")
Exemple #5
0
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)))
Exemple #6
0
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))})
Exemple #7
0
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)
Exemple #8
0
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 update_show(id):
    if request.method == 'PUT':
        req_data = request.get_json()
        update_data = {
            "name": req_data['name'],
            "episodes_seen":req_data['episodes_seen']
        }
        if db.getById('shows', int(id)) is None:
            return create_response(status=404, message="The provided Id does not exists!")
        db.updateById('shows',int(id), update_data)
        return create_response({"shows": db.get('shows')})
Exemple #10
0
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)))
Exemple #11
0
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}))
Exemple #12
0
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)
Exemple #13
0
def update_contact(id):
    updated_contact = db.getById('contacts', int(id))
    if updated_contact is None:
        return create_response(status=404, message="No contact with this id exists")
    body = request.get_json()
    if 'name' in body:
        new_name = body["name"]
        updated_contact = db.updateById('contacts', int(id), {"name": new_name})
    if 'hobby' in body:
        new_hobby = body["hobby"]
        updated_contact = db.updateById('contacts', int(id), {"hobby": new_hobby})
    return create_response(updated_contact)
Exemple #14
0
def change_restaurant(id):
    try:
        name = request.form['name']
        rating = request.form['rating']
    except Exception as e:
        return create_response(status=422,
                               message="Missing parameters name or rating")

    # Assumption that we are only rating with integers
    # Also we could try to modularize this since it is the same in add_restaurant
    try:
        id = int(id)
        rating = int(rating)
    except Exception as e:
        return create_response(status=422,
                               message="Rating or Id is not an integer")

    if rating < 0 or rating > 10:
        return create_response(status=422,
                               message="Rating is not between 0 and 10")

    payload = {'name': name, 'rating': rating}
    restaurant = db.updateById('restaurants', id, payload)

    if restaurant is None:
        return create_response(status=404,
                               message="No restaurant with this id exists")
    return create_response({"restaurant": restaurant})
Exemple #15
0
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_show(id):
    update_request = request.json
    update_response = db.updateById("shows", id, update_request)
    if (update_response is None):
        return create_response(
            status=404, message="Show could not be updated: does not exist")
    return create_response(data=update_response, message="Show updated")
Exemple #17
0
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)
Exemple #18
0
def update_restaurant(id):
    updated_restaurant = db.updateById('restaurants', int(id), request.json)
    print(updated_restaurant)
    if updated_restaurant is None:
        return create_response(status=404,
                               message="No restaurant with this id exists")
    return create_response(updated_restaurant)
Exemple #19
0
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)))
Exemple #20
0
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.get_json()
    updatedEntry = db.updateById("shows", int(id), data)
    return create_response(updatedEntry, message="Your show has been updated")
Exemple #21
0
def update_contact(id, name, hobby):
    if db.getById('contacts', int(id)) is None:
        return create_response(status=404, message="No contact with this id exists")

    if name is None and hobby is None:
        return create_response(status=201)
    elif name is not None and hobby is not None:
        data = {"name": name, "hobby": hobby}
    elif name is not None:
        data = {"name": name}

    #hobby is not None      
    else
        data = {"hobby": hobby}

    db.updateById('contacts', int(id), data)
    return create_response(status-201)
Exemple #22
0
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))})
Exemple #23
0
def update_contact_with_info(id):

    input_info = request.json

    check_name = (('name' in input_info) and (input_info['name'] != ''))
    check_hobby = (('hobby' in input_info) and (input_info['hobby'] != ''))

    if db.getById('contacts', int(id)) is None:
        return create_response(status=404,
                               message="The contact with id " + str(id) +
                               " was not found.")

    if (check_name):
        db.updateById('contacts', int(id), {'name': input_info['name']})

    if (check_hobby):
        db.updateById('contacts', int(id), {'hobby': input_info['hobby']})

    return create_response({'contact': db.getById('contacts', 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")
    update_values = request.get_json()

    updated = db.updateById("shows", int(id), update_values)
    if updated is None:
        return create_response(status=404,
                               message="No show with this id exists")
    return create_response(updated, status=201)
Exemple #25
0
def update_contact(id):
    if db.getById('contacts', int(id)) is None:
        return create_response(
            status=404, message='No contact with id "{0}" exists'.format(id))

    data = request.json

    name = data.get("name")
    if name is None:
        return create_response(
            status=422, message='Could not create contact, no "name" info')

    hobby = data.get("hobby")
    if hobby is None:
        return create_response(
            status=422, message='Could not create contact, no "hobby" info')

    db.updateById('contacts', int(id), {"name": name, "hobby": hobby})

    return create_response({"contacts": db.getById('contacts', int(id))})
Exemple #26
0
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))})
Exemple #27
0
def update_show(id):
    if db.getById('shows', int(id)) is None:
        return create_response(status=404,
                               message="No show with this id exists")
    payload = request.get_json()
    update_values = {
        key: payload[key]
        for key in payload if key in ["name", "episodes_seen"]
    }
    return create_response(
        status=201,
        data={"show": db.updateById('shows', int(id), update_values)})
Exemple #28
0
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))
Exemple #29
0
def put_show(id):
    update_values = request.get_json()

    valid_parameters = ["episodes_seen", "name"]
    same_parameters = set(valid_parameters).intersection(update_values.keys())
    update_values = { parameter: update_values[parameter] for parameter in same_parameters }

    show = db.updateById('shows', int(id), update_values)
    
    if show is None:
        return create_response(status=404, message="No show with this id exists")
    return create_response({"shows": show}, status=201)
Exemple #30
0
def update_restaurant(id):
    body, error = validate_args(request.json, [], [('name', str),
                                                   ('rating', int)])
    if error:
        return error

    restaurant = db.updateById('restaurants', id, body)
    if restaurant is None:
        return create_response(status=404,
                               message="No restaurant with this id exists")

    return create_response(restaurant)