Jon Solow commited on
Commit
a62c7e7
·
1 Parent(s): e10d757

Replace all refs to old db with supabase client

Browse files
Files changed (1) hide show
  1. src/data_storage.py +37 -69
src/data_storage.py CHANGED
@@ -4,48 +4,41 @@ import streamlit as st
4
  from queries.supabase_db.client import supabase_client
5
 
6
 
 
 
 
 
 
7
  def update_selection(user_id: str | int, position_id: str, player_id: str):
8
- pass
9
- # with get_db_connection() as con:
10
- # cur = con.cursor()
11
- # cur.execute(
12
- # f"""DELETE FROM user_rosters where user_id = {user_id} and position_id = '{position_id}'
13
- # """
14
- # )
15
- # cur.execute(
16
- # f"""INSERT INTO user_rosters (user_id, position_id, player_id )
17
- # VALUES({user_id}, '{position_id}', '{player_id}')
18
- # """
19
- # )
20
 
21
 
22
  def get_user_team(user_id):
23
- with get_db_connection() as con:
24
- cur = con.cursor()
25
- team = cur.execute(f"select * from user_rosters where user_id = {user_id}").fetchall()
 
 
 
 
26
  if team:
27
- return {x[1]: x[2] for x in team}
28
  else:
29
  return {}
30
 
31
 
32
  def add_new_user(email: str, name: str):
33
- with get_db_connection() as con:
34
- cur = con.cursor()
35
- cur.execute(
36
- f"""INSERT INTO users (email, name )
37
- VALUES('{email.lower()}', '{name}')
38
- """
39
- )
40
 
41
 
42
  def get_user(user_id: int):
43
  user_data = (
44
- supabase_client.table("npcs_users")
45
- .select("user_id", "email", "name")
46
- .eq("user_id", user_id)
47
- .execute()
48
- .data
49
  )
50
  if not user_data:
51
  return {}
@@ -53,13 +46,11 @@ def get_user(user_id: int):
53
 
54
 
55
  def get_user_id_if_email_exists(email: str) -> int | None:
56
- with get_db_connection() as con:
57
- cur = con.cursor()
58
- query_result = cur.execute(f"select user_id from users where email = '{email.lower()}'").fetchone()
59
- if query_result:
60
- user_id = query_result[0]
61
- else:
62
- user_id = None
63
  return user_id
64
 
65
 
@@ -71,58 +62,35 @@ def is_admin(user_id: int):
71
 
72
  def login_by_token(token: str):
73
  # returns true if logged in successfully
74
- with get_db_connection() as con:
75
- cur = con.cursor()
76
- query_result = cur.execute(f"select user_id from tokens where token = '{token}'").fetchone()
77
- if query_result:
78
- user_id = query_result[0]
79
- st.session_state["logged_in_user"] = user_id
80
- else:
81
- user_id = None
82
  return user_id
83
 
84
 
85
  def create_new_token_for_user(user_id: int, existing_user: bool = False):
86
- return {}
87
  # returns true if logged in successfully
88
  token = token_urlsafe(32)
89
- with get_db_connection() as con:
90
- cur = con.cursor()
91
- if existing_user:
92
- cur.execute(
93
- f"""DELETE FROM tokens where user_id = {user_id}
94
- """
95
- )
96
-
97
- cur.execute(
98
- f"""INSERT INTO tokens (user_id, token )
99
- VALUES({user_id}, '{token}')
100
- """
101
- )
102
  return token
103
 
104
 
105
-
106
  def get_all_users(columns_included: list[str] = ["user_id", "name", "email"]):
107
- columns_as_str = ",".join(columns_included)
108
- with get_db_connection() as con:
109
- cur = con.cursor()
110
- all_users = cur.execute(f"select {columns_as_str} from users").fetchall()
111
  return all_users
112
 
113
 
114
  def get_all_rosters() -> list[tuple[int, str, str]]:
115
- with get_db_connection() as con:
116
- cur = con.cursor()
117
- all_rosters = cur.execute("select * from user_rosters").fetchall()
118
  return all_rosters
119
 
120
 
121
  def get_all_rosters_week(week: int) -> list[tuple[int, str, str]]:
122
- with get_db_connection() as con:
123
- cur = con.cursor()
124
- all_rosters = cur.execute(f"select * from user_rosters where position_id like '{week}%'").fetchall()
125
- return all_rosters
126
 
127
 
128
  def migrate_players_from_week(migrate_from_week: int):
 
4
  from queries.supabase_db.client import supabase_client
5
 
6
 
7
+ USERS_TABLE = "npcs_users"
8
+ USER_ROSTERS_TABLE = "npcs_user_rosters"
9
+ TOKENS_TABLE = "npcs_tokens"
10
+
11
+
12
  def update_selection(user_id: str | int, position_id: str, player_id: str):
13
+ (
14
+ supabase_client.table(USER_ROSTERS_TABLE)
15
+ .update({"user_id": user_id, "position_id": position_id, "player_id": player_id})
16
+ .match({"user_id": user_id, "position_id": position_id})
17
+ .execute()
18
+ )
 
 
 
 
 
 
19
 
20
 
21
  def get_user_team(user_id):
22
+ team = (
23
+ supabase_client.table(USER_ROSTERS_TABLE)
24
+ .select("position_id", "player_id")
25
+ .eq("user_id", user_id)
26
+ .execute()
27
+ .data
28
+ )
29
  if team:
30
+ return {x["position_id"]: x["player_id"] for x in team}
31
  else:
32
  return {}
33
 
34
 
35
  def add_new_user(email: str, name: str):
36
+ (supabase_client.table(USERS_TABLE).insert({"email": email.lower(), "name": name}).execute())
 
 
 
 
 
 
37
 
38
 
39
  def get_user(user_id: int):
40
  user_data = (
41
+ supabase_client.table("npcs_users").select("user_id", "email", "name").eq("user_id", user_id).execute().data
 
 
 
 
42
  )
43
  if not user_data:
44
  return {}
 
46
 
47
 
48
  def get_user_id_if_email_exists(email: str) -> int | None:
49
+ query_result = supabase_client.table(USERS_TABLE).select("user_id").eq("email", email.lower()).execute().data
50
+ if query_result:
51
+ user_id = query_result[0]["user_id"]
52
+ else:
53
+ user_id = None
 
 
54
  return user_id
55
 
56
 
 
62
 
63
  def login_by_token(token: str):
64
  # returns true if logged in successfully
65
+ query_result = supabase_client.table(TOKENS_TABLE).select("user_id").eq("token", token).execute().data
66
+ if query_result:
67
+ user_id = query_result[0]["user_id"]
68
+ st.session_state["logged_in_user"] = user_id
69
+ else:
70
+ user_id = None
 
 
71
  return user_id
72
 
73
 
74
  def create_new_token_for_user(user_id: int, existing_user: bool = False):
 
75
  # returns true if logged in successfully
76
  token = token_urlsafe(32)
77
+ (supabase_client.table(TOKENS_TABLE).upsert({"user_id": user_id, "token": token}))
 
 
 
 
 
 
 
 
 
 
 
 
78
  return token
79
 
80
 
 
81
  def get_all_users(columns_included: list[str] = ["user_id", "name", "email"]):
82
+ all_users = supabase_client.table(USERS_TABLE).select(*columns_included).execute().data
 
 
 
83
  return all_users
84
 
85
 
86
  def get_all_rosters() -> list[tuple[int, str, str]]:
87
+ all_rosters = supabase_client.table(USER_ROSTERS_TABLE).select("*").execute().data
 
 
88
  return all_rosters
89
 
90
 
91
  def get_all_rosters_week(week: int) -> list[tuple[int, str, str]]:
92
+ week_rosters = supabase_client.table(USER_ROSTERS_TABLE).select("*").like("position_id", f"{week}%").execute().data
93
+ return week_rosters
 
 
94
 
95
 
96
  def migrate_players_from_week(migrate_from_week: int):