44
55import psycopg
66
7- from library . database import Database
7+ import slackbot_settings as conf
88
99
10- class VocabularyDatabase (Database ):
11- """パワーワードを扱うDBを操作するためのクラス"""
12-
13- def get_word_list (self ):
14- """パワーワードの一覧をDBから取得する"""
15- with self .conn .cursor () as cursor :
10+ def get_word_list ():
11+ """パワーワードの一覧をDBから取得する"""
12+ with psycopg .connect (conf .DB_URL ) as conn :
13+ with conn .cursor () as cursor :
1614 try :
1715 cursor .execute ("SELECT no, word FROM vocabulary ORDER BY no;" )
1816 results = cursor .fetchall ()
1917 except psycopg .Error :
2018 print ("Can not execute sql(select_list)." )
2119
22- return results
20+ return results
21+
2322
24- def get_random_word (self ):
25- """パワーワードをDBからランダムで取得する"""
23+ def get_random_word ():
24+ """パワーワードをDBからランダムで取得する"""
2625
27- with self .conn .cursor () as cursor :
26+ with psycopg .connect (conf .DB_URL ) as conn :
27+ with conn .cursor () as cursor :
2828 try :
2929 cursor .execute ("SELECT word FROM vocabulary ORDER BY random() LIMIT 1;" )
3030 results = cursor .fetchone ()
3131 except psycopg .Error :
3232 print ("Can not execute sql(select_random)." )
3333
34- return results
34+ return results
35+
3536
36- def add_word (self , word : str ) -> None :
37- """パワーワードをDBに登録する"""
37+ def add_word (word : str ) -> None :
38+ """パワーワードをDBに登録する"""
3839
39- with self .conn .cursor () as cursor :
40+ with psycopg .connect (conf .DB_URL ) as conn :
41+ with conn .cursor () as cursor :
4042 try :
4143 cursor .execute ("INSERT INTO vocabulary(word) VALUES(%s);" , (word ,))
42- self . conn .commit ()
44+ conn .commit ()
4345 except psycopg .Error :
4446 print ("Can not execute sql(add)." )
4547
46- def delete_word (self , word_id : int ) -> None :
47- """指定したidのパワーワードをDBから削除する"""
4848
49- with self .conn .cursor () as cursor :
49+ def delete_word (word_id : int ) -> None :
50+ """指定したidのパワーワードをDBから削除する"""
51+
52+ with psycopg .connect (conf .DB_URL ) as conn :
53+ with conn .cursor () as cursor :
5054 try :
5155 cursor .execute ("DELETE FROM vocabulary WHERE no = %s;" , (word_id ,))
52- self . conn .commit ()
56+ conn .commit ()
5357 except psycopg .Error :
5458 print ("Can not execute sql(delete)." )
5559
5660
5761def get_vocabularys ():
5862 """一覧を表示する"""
5963
60- with VocabularyDatabase () as v_d :
61- result = v_d .get_word_list ()
64+ result = get_word_list ()
6265
6366 if len (result ) > 0 :
6467 slack_msg = "```"
@@ -79,17 +82,15 @@ def get_vocabularys():
7982def add_vocabulary (msg : str ) -> None :
8083 """追加する"""
8184
82- with VocabularyDatabase () as v_d :
83- v_d .add_word (msg )
85+ add_word (msg )
8486
8587
8688def show_vocabulary (word_id : int ) -> str :
8789 """指定したものを表示する"""
8890
8991 slack_msg = "該当する番号は見つからなかったっぽ!"
9092
91- with VocabularyDatabase () as v_d :
92- result = v_d .get_word_list ()
93+ result = get_word_list ()
9394
9495 cnt = 1
9596 for row in result :
@@ -106,8 +107,7 @@ def show_random_vocabulary() -> str:
106107
107108 slack_msg = "鳩は唐揚げ!!"
108109
109- with VocabularyDatabase () as v_d :
110- result = v_d .get_random_word ()
110+ result = get_random_word ()
111111
112112 if result is not None and len (result ) > 0 :
113113 slack_msg = result [0 ]
@@ -120,16 +120,15 @@ def delete_vocabulary(word_id: int) -> str:
120120
121121 slack_msg = "該当する番号は見つからなかったっぽ!"
122122
123- with VocabularyDatabase () as v_d :
124- result = v_d .get_word_list ()
125- cnt = 1
126- for row in result :
127- row_id , _ = row
128- if cnt == word_id :
129- delete_id = row_id
130- v_d .delete_word (delete_id )
131- slack_msg = "忘れたっぽ!"
132- break
133- cnt += 1
123+ result = get_word_list ()
124+ cnt = 1
125+ for row in result :
126+ row_id , _ = row
127+ if cnt == word_id :
128+ delete_id = row_id
129+ delete_word (delete_id )
130+ slack_msg = "忘れたっぽ!"
131+ break
132+ cnt += 1
134133
135134 return slack_msg
0 commit comments