File size: 4,102 Bytes
f9247be
d3662ef
560823b
98ed27b
560823b
 
e11a10a
98ed27b
 
 
 
 
 
 
 
 
 
 
 
560823b
ea0c0c1
560823b
 
 
 
 
 
 
 
1f6b17c
 
 
 
 
 
 
5c97774
1f6b17c
 
 
 
 
98ed27b
 
 
 
 
 
 
d0acf6f
 
98ed27b
5c97774
 
 
 
 
 
 
 
 
 
 
a3c0746
 
 
98ed27b
a3c0746
d6188d7
f9247be
 
 
 
 
 
a7739f9
f9247be
 
d3662ef
f9247be
 
 
 
 
 
98ed27b
f9247be
 
 
 
7804478
 
 
 
 
 
f9247be
7804478
f9247be
 
 
86b69b9
0b5b559
 
9db9dc4
9d8e6a4
 
9db9dc4
 
9d8e6a4
9db9dc4
9d8e6a4
 
4bfd7b8
9d8e6a4
 
 
 
e11a10a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from secrets import token_urlsafe
import streamlit as st

from queries.supabase_db.client import supabase_client


def update_selection(user_id: str | int, position_id: str, player_id: str):
    pass
    # with get_db_connection() as con:
    #     cur = con.cursor()
    #     cur.execute(
    #         f"""DELETE FROM user_rosters where user_id = {user_id} and position_id = '{position_id}'
    #                 """
    #     )
    #     cur.execute(
    #         f"""INSERT INTO user_rosters (user_id, position_id, player_id )
    #                 VALUES({user_id}, '{position_id}', '{player_id}')
    #                 """
    #     )


def get_user_team(user_id):
    with get_db_connection() as con:
        cur = con.cursor()
        team = cur.execute(f"select * from user_rosters where user_id = {user_id}").fetchall()
    if team:
        return {x[1]: x[2] for x in team}
    else:
        return {}


def add_new_user(email: str, name: str):
    with get_db_connection() as con:
        cur = con.cursor()
        cur.execute(
            f"""INSERT INTO users (email, name )
                    VALUES('{email.lower()}', '{name}')
                    """
        )


def get_user(user_id: int):
    user_data = (
        supabase_client.table("npcs_users")
        .select("user_id", "email", "name")
        .eq("user_id", user_id)
        .execute()
        .data
    )
    if not user_data:
        return {}
    return user_data[0]


def get_user_id_if_email_exists(email: str) -> int | None:
    with get_db_connection() as con:
        cur = con.cursor()
        query_result = cur.execute(f"select user_id from users where email = '{email.lower()}'").fetchone()
        if query_result:
            user_id = query_result[0]
        else:
            user_id = None
    return user_id


def is_admin(user_id: int):
    return True
    # Replace with db data field later
    return user_id == 1


def login_by_token(token: str):
    # returns true if logged in successfully
    with get_db_connection() as con:
        cur = con.cursor()
        query_result = cur.execute(f"select user_id from tokens where token = '{token}'").fetchone()
        if query_result:
            user_id = query_result[0]
            st.session_state["logged_in_user"] = user_id
        else:
            user_id = None
    return user_id


def create_new_token_for_user(user_id: int, existing_user: bool = False):
    return {}
    # returns true if logged in successfully
    token = token_urlsafe(32)
    with get_db_connection() as con:
        cur = con.cursor()
        if existing_user:
            cur.execute(
                f"""DELETE FROM tokens where user_id = {user_id}
                        """
            )

        cur.execute(
            f"""INSERT INTO tokens (user_id, token )
                    VALUES({user_id}, '{token}')
                    """
        )
    return token



def get_all_users(columns_included: list[str] = ["user_id", "name", "email"]):
    columns_as_str = ",".join(columns_included)
    with get_db_connection() as con:
        cur = con.cursor()
        all_users = cur.execute(f"select {columns_as_str} from users").fetchall()
    return all_users


def get_all_rosters() -> list[tuple[int, str, str]]:
    with get_db_connection() as con:
        cur = con.cursor()
        all_rosters = cur.execute("select * from user_rosters").fetchall()
    return all_rosters


def get_all_rosters_week(week: int) -> list[tuple[int, str, str]]:
    with get_db_connection() as con:
        cur = con.cursor()
        all_rosters = cur.execute(f"select * from user_rosters where position_id like '{week}%'").fetchall()
    return all_rosters


def migrate_players_from_week(migrate_from_week: int):
    """
    Migrate players from the week = migrate_from_week to the week = migrate_from_week + 1
    """
    rosters = get_all_rosters_week(migrate_from_week)
    for user_id, position_id, player_id in rosters:
        new_position_id = f"""{migrate_from_week + 1}-{position_id.split("-", 1)[1]}"""
        update_selection(user_id, new_position_id, player_id)