Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
MothScientist committed Dec 12, 2024
1 parent 11d7deb commit 6c0a78e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 36 deletions.
42 changes: 6 additions & 36 deletions budget_graph/db_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,8 @@ def get_username_by_telegram_id(self, telegram_id: int) -> str:
# TypeError occurs in this block if the database connection returned null
with conn.cursor() as cur:
# Cursors can be used as context managers: leaving the context will close the cursor

# AttributeError occurs in this block if the database connection returned null
cur.execute("""SELECT
"username"
FROM
"budget_graph"."users"
WHERE
"telegram_id" = %s::bigint""", (telegram_id,)) # DO NOT REMOVE commas
cur.execute(read_sql_file('get_username_by_telegram_id'), {'telegram_id': telegram_id})
return res[0] if (res := cur.fetchone()) else ''

except (DatabaseError, TypeError) as err:
Expand All @@ -142,12 +136,7 @@ def get_telegram_id_by_username(self, username: str) -> int:
try:
with self.__conn as conn:
with conn.cursor() as cur:
cur.execute("""SELECT
"telegram_id"
FROM
"budget_graph"."users"
WHERE
"username" = %s::text""", (username,))
cur.execute(read_sql_file('get_telegram_id_by_username'), {'username': username})
return res[0] if (res := cur.fetchone()) else 0

except (DatabaseError, TypeError) as err:
Expand All @@ -162,12 +151,7 @@ def get_group_id_by_token(self, token: str) -> int:
try:
with self.__conn as conn:
with conn.cursor() as cur:
cur.execute("""SELECT
"id"
FROM
"budget_graph"."groups"
WHERE
"token" = %s::text""", (token,))
cur.execute(read_sql_file('get_group_id_by_token'), {'token': token})
return res[0] if (res := cur.fetchone()) else 0

except (DatabaseError, TypeError) as err:
Expand All @@ -182,12 +166,7 @@ def get_group_id_by_telegram_id(self, telegram_id: int) -> int:
try:
with self.__conn as conn:
with conn.cursor() as cur:
cur.execute("""SELECT
"group_id"
FROM
"budget_graph"."users_groups"
WHERE
"telegram_id" = %s::bigint""", (telegram_id,))
cur.execute(read_sql_file('get_group_id_by_telegram_id'), {'telegram_id': telegram_id})
return res[0] if (res := cur.fetchone()) else 0

except (DatabaseError, TypeError) as err:
Expand All @@ -214,16 +193,7 @@ def get_token_by_telegram_id(self, telegram_id: int) -> str:
try:
with self.__conn as conn:
with conn.cursor() as cur:
cur.execute("""SELECT
g."token"
FROM
"budget_graph"."groups" g
JOIN
"budget_graph"."users_groups" u_g
ON
g."id" = u_g."group_id"
WHERE
u_g."telegram_id" = %s::bigint""", (telegram_id,))
cur.execute(read_sql_file('get_token_by_telegram_id'), {'telegram_id': telegram_id})
return res[0] if (res := cur.fetchone()) else ''

except (DatabaseError, TypeError) as err:
Expand All @@ -239,7 +209,7 @@ def get_salt_by_username(self, username: str) -> str:
with self.__conn as conn:
with conn.cursor() as cur:
cur.execute(read_sql_file('get_salt_by_username'), {'username': username})
return str(res[0]) if (res := cur.fetchone()) else ''
return res[0] if (res := cur.fetchone()) else ''

except (DatabaseError, TypeError) as err:
logger_database.error(f"[DB_QUERY] {str(err)}, "
Expand Down
6 changes: 6 additions & 0 deletions budget_graph/sql/get_group_id_by_telegram_id.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SELECT
"group_id"
FROM
"budget_graph"."users_groups"
WHERE
"telegram_id" = %(telegram_id)s::bigint
6 changes: 6 additions & 0 deletions budget_graph/sql/get_group_id_by_token.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SELECT
"id"
FROM
"budget_graph"."groups"
WHERE
"token" = %(token)s::text
6 changes: 6 additions & 0 deletions budget_graph/sql/get_telegram_id_by_username.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SELECT
"telegram_id"
FROM
"budget_graph"."users"
WHERE
"username" = %(username)s::text
7 changes: 7 additions & 0 deletions budget_graph/sql/get_token_by_telegram_id.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
g."token"
FROM
"budget_graph"."groups" g
JOIN "budget_graph"."users_groups" u_g ON g."id" = u_g."group_id"
WHERE
u_g."telegram_id" = %(telegram_id)s::bigint
6 changes: 6 additions & 0 deletions budget_graph/sql/get_username_by_telegram_id.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SELECT
"username"
FROM
"budget_graph"."users"
WHERE
"telegram_id" = %(telegram_id)s::bigint

0 comments on commit 6c0a78e

Please sign in to comment.