def update_recipe_into_db(data): db.execute( "UPDATE recipes SET name=%s, pre_time=%s, difficulty=%s, vegetarian=%s, created_at=%s WHERE id=%s", (data['name'], data['pre_time'], data['difficulty'], data['vegetarian'], datetime.datetime.now(), data['id'])) db_connection.commit() status_code = 200 return RECIPE_UPDATED, status_code
def delete_partido(data): try: db.execute("DELETE FROM partido where id=%s" % data['id']) db_connection.commit() status_code = 200 return PARTIDO_DELETED, status_code except Exception as e: db_connection.rollback() return {'errors': e.args}, 400
def create_recipe_into_db(data): db.execute( "INSERT INTO recipes (name, pre_time, difficulty, vegetarian, created_at) VALUES (%s, %s, %s, %s, %s)", (data['name'], data['pre_time'], data['difficulty'], data['vegetarian'], datetime.datetime.now())) db_connection.commit() # db_connection.close() # db.close() status_code = 201 return RECIPE_CREATED, status_code
def delete_recipe_from_db(data): recipes = fetch_all_recipes() for recipe in recipes: if recipe['id'] == int(data['id']): db.execute("DELETE FROM recipes where id=%s" % data['id']) db_connection.commit() status_code = 200 return RECIPE_DELETED, status_code status_code = 400 return DATA_NOT_FOUND, status_code
def recipe_rating_into_db(data): recipes = fetch_all_recipes() for recipe in recipes: if recipe['id'] == int(data['id']): db.execute( "INSERT INTO recipe_rating (recipe_id, rated) VALUES (%s, %s)", (data['id'], data['rated'])) db_connection.commit() status_code = 201 return RECIPE_RATING, status_code status_code = 400 return DATA_NOT_FOUND, status_code
def update_partido(data): try: db.execute( "UPDATE partido SET nome=%s, sigla=%s, fundo=%s, numero=%s WHERE id=%s", (data['nome'], data['sigla'], data['fundo'], data['numero'], data['id'])) db_connection.commit() status_code = 200 return PARTIDO_UPDATED, status_code except Exception as e: db_connection.rollback() return {'errors': e.args}, 400
def add_partido(data): try: db.execute( "INSERT INTO partido (nome, sigla, fundo, numero, created_at) VALUES (%s, %s, %s, %s, %s)", (data['nome'], data['sigla'], data['fundo'], data['numero'], datetime.datetime.now())) db_connection.commit() status_code = 201 return PARTIDO_CREATED, status_code except Exception as e: db_connection.rollback() return {'errors': e.args}, 400
from db.database_connection import db_connection, db recipe_table = "CREATE TABLE recipes (id serial PRIMARY KEY , name VARCHAR(100), " \ "pre_time INTEGER CHECK ( pre_time > 0), difficulty INTEGER check ( difficulty > 0)," \ " vegetarian BOOLEAN, created_at TIMESTAMP )" # db.execute("CREATE TABLE vubon (id serial PRIMARY KEY, num integer, data varchar);") recipe_rating = "CREATE TABLE recipe_rating (recipe_id INTEGER REFERENCES recipes, rated INTEGER CHECK( rated > 0))" db.execute("select exists(select * from information_schema.tables where table_name=%s)", ('recipes',)) # checking if table already exists then pass or create that table if db.fetchone()[0]: pass else: db.execute(recipe_table) db.execute("select exists(select * from information_schema.tables where table_name=%s)", ('recipe_rating',)) if db.fetchone()[0]: pass else: db.execute(recipe_rating) db_connection.commit() # db_connection.close() # db.close()