コード例 #1
0
def create_country():
    continent_id = request.form['continent_id']
    name = request.form["name"]
    continent = continent_repository.select(continent_id)
    visited = False
    new_country = Country(name, continent, visited)
    country_repository.save(new_country)
    country_id = new_country.id
    return redirect(f"/countries/{country_id}")
コード例 #2
0
def update_country(id):
    name = request.form['name']
    continent_id = request.form['continent_id']
    continent = continent_repository.select(continent_id)
    country = country_repository.select(id)
    visited = country.visited
    updated_country = Country(name, continent, visited, id)
    country_repository.update(updated_country)
    country_id = country.id
    return redirect(f"/countries/{country_id}")
コード例 #3
0
def select(id):
    country = None
    sql = "SELECT * FROM countries WHERE id = %s"
    values = [id]
    result = run_sql(sql, values)[0]

    if result is not None:
        continent = continent_repository.select(result['continent_id'])
        country = Country(result['name'], continent, result['visited'],
                          result['id'])
    return country
コード例 #4
0
def select_all():
    countries = []

    sql = "SELECT * FROM countries"
    results = run_sql(sql)

    for row in results:
        continent = continent_repository.select(row['continent_id'])
        country = Country(row['name'], continent, row['visited'], row['id'])
        countries.append(country)
    return countries
コード例 #5
0
def select(id):
    # create sql query without values
    sql = "SELECT * FROM countries WHERE id = %s"
    # create list with values required by sql query
    values = [id]
    # execute sql query
    result = run_sql(sql, values)[0]
    # convert return which is a single element list of dictionaries into a continent object
    country = Country(result["name"],
                      continent_repository.select(result['continent_id']),
                      result["id"])
    # return the result
    return country
コード例 #6
0
def select_all():
    # set return variable as empty list
    countries = []
    # create sql query without values
    sql = "SELECT * FROM countries ORDER BY	name ASC"
    # execute sql query
    results = run_sql(sql)
    # convert return which is a single element list of dictionaries into list of countries objects
    for result in results:
        country = Country(result["name"],
                          continent_repository.select(result['continent_id']),
                          result["id"])
        countries.append(country)
    # return the result
    return countries
コード例 #7
0
def countries_update():
    continent = continent_repository.select(request.form['continent_id'])
    updated_country = Country(request.form['country_name'], continent ,request.form['country_id'])
    country_repository.update(updated_country)
    return redirect('/countries/view')
コード例 #8
0
def countries_add():
    new_country = Country(request.form['name'], continent_repository.select(request.form['continent_id']))
    country_repository.save(new_country)
    return redirect('/countries/view')