File size: 2,051 Bytes
560823b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f6b17c
560823b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f6b17c
 
 
 
 
 
 
5c97774
1f6b17c
 
 
 
 
 
 
 
d0acf6f
 
1f6b17c
 
 
 
 
5c97774
 
 
 
 
 
 
 
 
 
 
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
import os
import sqlite3

DATA_DIR = "/data"

DB_PATH = os.path.join(DATA_DIR, "data.db")


def get_db_connection():
    return sqlite3.connect(DB_PATH)


def initialize_data_storage():
    with get_db_connection() as con:
        cur = con.cursor()
        cur.execute("CREATE TABLE IF NOT EXISTS user_rosters( user_id INTEGER, position_id TEXT, player_id TEXT)")
        cur.execute("CREATE TABLE IF NOT EXISTS users( user_id INTEGER PRIMARY KEY ASC, email TEXT, name TEXT)")


def update_selection(user_id: str, position_id: str, player_id: str):
    with get_db_connection() as con:
        cur = con.cursor()
        cur.execute(
            f"""REPLACE 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):
    with get_db_connection() as con:
        cur = con.cursor()
        user_data = cur.execute(f"select * from users where user_id = {user_id}").fetchone()
    if not user_data:
        return {}
    return {
        "user_id": user_data[0],
        "email": user_data[1],
        "name": user_data[2],
    }


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