Implemented all endpoints
This commit is contained in:
@ -3,15 +3,6 @@ import config
|
||||
from loguru import logger
|
||||
|
||||
|
||||
logging_level = config.logging_level
|
||||
logger.add(
|
||||
"sys.stdout",
|
||||
format="{time:YYYY-MM-DD HH:mm:ss.SSS} | {level} | {file}:{line} - {message}",
|
||||
colorize=True,
|
||||
level=logging_level
|
||||
)
|
||||
|
||||
|
||||
#connection stuff
|
||||
def set_connection():
|
||||
try:
|
||||
@ -22,14 +13,14 @@ def set_connection():
|
||||
host = config.host_name,
|
||||
port = config.port
|
||||
)
|
||||
cursor = connection.cursor()
|
||||
return connection, cursor
|
||||
return connection
|
||||
except psycopg2.Error as e:
|
||||
logger.error(f'Failed to set connection to the PostgreSQL DB: {e.pgerror}')
|
||||
|
||||
|
||||
def close_connection(connection, cursor):
|
||||
def close_connection(connection):
|
||||
try:
|
||||
cursor = connection.cursor()
|
||||
cursor.close()
|
||||
connection.close()
|
||||
except psycopg2.Error as e:
|
||||
@ -37,57 +28,51 @@ def close_connection(connection, cursor):
|
||||
|
||||
|
||||
#actual DB alters
|
||||
def add_entry(article_url, rating):
|
||||
connection, cursor = set_connection()
|
||||
def add_entry(article_url, rating, connection):
|
||||
try:
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("INSERT INTO harticle.articles (article_url, rating) VALUES (%s, %s);", (article_url, rating,))
|
||||
connection.commit()
|
||||
logger.info('An entry has been written to the PGSQL DB successfully')
|
||||
except psycopg2.Error as e:
|
||||
logger.error(f'Failed to write an entry for article \'{article_url}\': {e.pgerror}')
|
||||
finally:
|
||||
close_connection(connection, cursor)
|
||||
|
||||
|
||||
def delete_entry(article_url, connection, cursor):
|
||||
connection, cursor = set_connection()
|
||||
def delete_entry(article_url, connection):
|
||||
try:
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("DELETE FROM harticle.articles WHERE article_url = %s;", (article_url,))
|
||||
connection.commit()
|
||||
logger.info(f'Rating for article \'{article_url}\' was cleared successfully')
|
||||
except psycopg2.Error as e:
|
||||
logger.error(f'Failed to clear a rating entry for article \'{article_url}\': {e.pgerror}')
|
||||
finally:
|
||||
close_connection(connection, cursor)
|
||||
|
||||
|
||||
# def delete_rating(article_url, connection, cursor):
|
||||
# close_connection(connection, cursor)
|
||||
# def delete_rating(article_url, connection):
|
||||
# try:
|
||||
# cursor = connection.cursor()
|
||||
# cursor.execute("UPDATE harticle.articles SET rating = NULL WHERE article_url = %s;", (article_url,))
|
||||
# connection.commit()
|
||||
# logger.info(f'Rating for article \'{article_url}\' was cleared successfully')
|
||||
# close_connection(connection, cursor)
|
||||
# except psycopg2.Error as e:
|
||||
# logger.error(f'Failed to clear a rating entry for article \'{article_url}\': {e.pgerror}')
|
||||
|
||||
|
||||
def get_all_entries():
|
||||
connection, cursor = set_connection()
|
||||
def get_all_entries(connection):
|
||||
try:
|
||||
cursor = connection.cursor()
|
||||
cursor.execute('SELECT article_url, rating FROM harticle.articles;')
|
||||
entries = cursor.fetchall()
|
||||
logger.info('All entry pairs have been retrieved successfully')
|
||||
return entries
|
||||
except psycopg2.Error as e:
|
||||
logger.error(f'Failed to fetch DB entries: {e.pgerror}')
|
||||
finally:
|
||||
close_connection(connection, cursor)
|
||||
|
||||
|
||||
#'create if no any' type functions for schema and table
|
||||
def schema_creator(schema_name):
|
||||
conn, cur = set_connection()
|
||||
conn = set_connection()
|
||||
cur = conn.cursor()
|
||||
try:
|
||||
cur.execute(f'CREATE SCHEMA IF NOT EXISTS {schema_name};')
|
||||
conn.commit()
|
||||
@ -95,28 +80,29 @@ def schema_creator(schema_name):
|
||||
except psycopg2.Error as e:
|
||||
logger.error(f'Error during schema creation: {e}')
|
||||
finally:
|
||||
close_connection(conn, cur)
|
||||
close_connection(conn)
|
||||
|
||||
|
||||
def table_creator(schema_name, table_name):
|
||||
conn, cur = set_connection()
|
||||
conn = set_connection()
|
||||
cur = conn.cursor()
|
||||
try:
|
||||
cur.execute(f'''
|
||||
CREATE TABLE IF NOT EXISTS {schema_name}.{table_name}
|
||||
(
|
||||
id SERIAL PRIMARY KEY,
|
||||
article_url VARCHAR(3000) UNIQUE NOT NULL,
|
||||
rating INT CHECK (rating < 2)
|
||||
)
|
||||
CREATE TABLE IF NOT EXISTS {schema_name}.{table_name}
|
||||
(
|
||||
id SERIAL PRIMARY KEY,
|
||||
article_url VARCHAR(3000) UNIQUE NOT NULL,
|
||||
rating INT CHECK (rating < 2)
|
||||
)
|
||||
|
||||
TABLESPACE pg_default;
|
||||
TABLESPACE pg_default;
|
||||
|
||||
ALTER TABLE IF EXISTS {schema_name}.{table_name}
|
||||
OWNER to {config.postgres_user};
|
||||
''')
|
||||
ALTER TABLE IF EXISTS {schema_name}.{table_name}
|
||||
OWNER to {config.postgres_user};
|
||||
''')
|
||||
conn.commit()
|
||||
logger.info(f'Successfully created table {table_name} in schema {schema_name} if it didn\'t exist yet')
|
||||
except psycopg2.Error as e:
|
||||
logger.error(f'Error during table creation: {e}')
|
||||
finally:
|
||||
close_connection(conn, cur)
|
||||
close_connection(conn)
|
||||
|
||||
Reference in New Issue
Block a user