11import psycopg2
22from config import DB_HOST , DB_PORT , DB_USER , DB_PASS , DB_NAME
3+ from collections import namedtuple
4+
5+ Sponsor = namedtuple ('Sponsor' , [
6+ 'gh_id' , 'gh_username' , 'gh_url' , 'discord_name' , 'discord_id' ,
7+ 'discord_code' , 'contributed_to_repos' , 'is_contributor' , 'is_currently_sponsoring'
8+ ])
39
410class PostgresDB :
511 def __init__ (self ):
@@ -12,7 +18,7 @@ def __init__(self):
1218 )
1319 self .cursor = self .conn .cursor ()
1420
15- def create_sponsor (self , gh_id , gh_username , discord_id = 0 , discord_name = '' , discord_code = '' , contributed_to_repos = None , is_currently_sponsoring = False ):
21+ def create_sponsor (self , gh_id , gh_username , discord_id = None , discord_name = None , discord_code = None , contributed_to_repos = None , is_currently_sponsoring = False ):
1622 if self .get_sponsor_by_gh_id (gh_id ):
1723 return
1824 if contributed_to_repos is None :
@@ -29,19 +35,28 @@ def get_sponsor_by_gh_id(self, gh_id):
2935 self .cursor .execute ('''
3036 SELECT * FROM Sponsor WHERE gh_id = %s LIMIT 1
3137 ''' , (gh_id ,))
32- return self .cursor .fetchone ()
38+ result = self .cursor .fetchone ()
39+ if result :
40+ return Sponsor (* result )
41+ return None
3342
3443 def get_sponsor_by_gh_username (self , gh_username ):
3544 self .cursor .execute ('''
3645 SELECT * FROM Sponsor WHERE LOWER(gh_username) = LOWER(%s) LIMIT 1
3746 ''' , (gh_username ,))
38- return self .cursor .fetchone ()
47+ result = self .cursor .fetchone ()
48+ if result :
49+ return Sponsor (* result )
50+ return None
3951
4052 def get_sponsor_by_discord_id (self , discord_id ):
4153 self .cursor .execute ('''
4254 SELECT * FROM Sponsor WHERE discord_id = %s LIMIT 1
4355 ''' , (discord_id ,))
44- return self .cursor .fetchone ()
56+ result = self .cursor .fetchone ()
57+ if result :
58+ return Sponsor (* result )
59+ return None
4560
4661 def update_sponsor_gh_username (self , gh_id , gh_username ):
4762 if not self .get_sponsor_by_gh_id (gh_id ):
@@ -93,4 +108,5 @@ def update_sponsor_is_currently_sponsoring(self, gh_id, is_currently_sponsoring)
93108
94109 def get_sponsors (self ):
95110 self .cursor .execute ('SELECT * FROM Sponsor' )
96- return self .cursor .fetchall ()
111+ results = self .cursor .fetchall ()
112+ return [Sponsor (* result ) for result in results ]
0 commit comments