import json import psycopg2.extras from psycopg2._psycopg import connection from api.models import Account # account create and delete def create_account( conn: connection, groupname: str, author: str, platform: str, login: str, password: str, metadata: dict ): with conn.cursor() as cur: cur.execute( """ insert into picrinth.accounts (groupname, author, platform, login, password, metadata, created_at) values (%s, %s, %s, %s, now()) returning id """, (groupname, author, platform, login, password, json.dumps(metadata)), ) result = cur.fetchone() conn.commit() if result is None: return None return result[0] def delete_account( conn: connection, account_id: str ): with conn.cursor() as cur: cur.execute( """ delete from picrinth.accounts where account_id = %s """, (account_id), ) conn.commit() return cur.rowcount > 0 # account checks def check_account_existence( conn: connection, groupname: str, platform: str ): with conn.cursor() as cur: cur.execute( """ select exists( select 1 from picrinth.accounts where groupname = %s and platform = %s ); """, (groupname, platform), ) return cur.fetchone()[0] # type: ignore # account update def update_account( conn: connection, groupname: str, author: str, platform: str, login: str, password: str, metadata: dict ): with conn.cursor() as cur: cur.execute( """ update picrinth.accounts SET author = %s, login = %s, password = %s, metadata = %s where groupname = %s and platform = %s """, (author, login, password, json.dumps(metadata), groupname, platform), ) conn.commit() return cur.rowcount > 0 def update_account_metadata( conn: connection, groupname: str, platform: str, metadata: dict ): with conn.cursor() as cur: cur.execute( """ update picrinth.accounts SET metadata = %s where groupname = %s and platform = %s """, (json.dumps(metadata), groupname, platform), ) conn.commit() return cur.rowcount > 0 # account receiving def get_accounts_by_group( conn: connection, groupname: str ) -> list[Account]: with conn.cursor() as cur: cur.execute( """ select * from picrinth.accounts where groupname = %s """, (groupname,), ) return [Account().fill(account_data) for (account_data,) in cur.fetchall()] def get_accounts_by_group_platform( conn: connection, groupname: str, platform: str ): with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur: cur.execute( """ select groupname, author, platform, login, password, metadata, created_at from picrinth.accounts where groupname = %s and platform = %s """, (groupname, platform), ) return cur.fetchone()