-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_funcs.py
269 lines (245 loc) · 10.1 KB
/
db_funcs.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import psycopg2
import os
from dotenv import load_dotenv
load_dotenv()
class BotDB:
def __init__(self):
"""initialization of connection with db"""
self.connection = psycopg2.connect(
host=os.getenv("HOST"),
user=os.getenv("USER"),
password=os.getenv("PASSWORD"),
database=os.getenv("DB_NAME"),
)
self.create_table()
def create_table(self):
"""create table"""
with self.connection.cursor() as cursor:
cursor.execute(
"""CREATE TABLE IF NOT EXISTS train_table (
id serial PRIMARY KEY,
phonenumber TEXT,
name TEXT,
surname TEXT,
bonus INTEGER);"""
)
return "Таблиця готова до роботи"
def create_table_admins(self):
"""create table with users"""
with self.connection.cursor() as cursor:
cursor.execute(
"""CREATE TABLE IF NOT EXISTS admins (
id serial PRIMARY KEY,
admins TEXT);"""
)
return "Таблиця з користувачами готова"
def create_table_main_admins(self):
"""create table with main admins"""
with self.connection.cursor() as cursor:
cursor.execute(
"""CREATE TABLE IF NOT EXISTS main_admins (
id serial PRIMARY KEY,
admins TEXT);"""
)
return "Таблиця з адмінами готова"
def if_admin_exists(self, user_id: str) -> bool:
"""check if the user already in db"""
with self.connection.cursor() as cursor:
cursor.execute(
"""SELECT * FROM admins WHERE admins = %s;""",
(user_id,),
)
result = cursor.fetchall()
return bool(len(result))
def if_main_admin_exists(self, user_id: str) -> bool:
"""check if the main admin already in db"""
with self.connection.cursor() as cursor:
cursor.execute(
"""SELECT * FROM main_admins WHERE admins = %s;""",
(user_id,),
)
result = cursor.fetchall()
return bool(len(result))
def if_client_exists(self, phonenumber: str) -> bool:
"""check if the client already in db"""
with self.connection.cursor() as cursor:
cursor.execute(
"""SELECT phonenumber FROM train_table WHERE phonenumber = %s;""",
(phonenumber,),
)
result = cursor.fetchall()
return bool(len(result))
def all_admins(self) -> list:
"""returns the list of users in db"""
with self.connection.cursor() as cursor:
cursor.execute("""SELECT * FROM admins;""")
result = cursor.fetchall()
return [i[1] for i in result]
def all_main_admins(self) -> list:
"""returns the list of main admins in db"""
with self.connection.cursor() as cursor:
cursor.execute("""SELECT * FROM main_admins;""")
result = cursor.fetchall()
return [i[1] for i in result]
def add_new_main_admin(self, user_id: str) -> str:
"""add new main admin into db"""
if self.if_main_admin_exists(user_id):
return "Такий адмін вже існує"
else:
with self.connection.cursor() as cursor:
cursor.execute(
"""INSERT INTO main_admins (admins) VALUES (%s)""",
(user_id,),
)
self.connection.commit()
return "Адмін додан"
def add_new_admin(self, user_id: str) -> str:
"""add new user into db"""
if self.if_admin_exists(user_id):
return "Такий користувач вже існує"
else:
with self.connection.cursor() as cursor:
cursor.execute(
"""INSERT INTO admins (admins) VALUES (%s)""",
(user_id,),
)
self.connection.commit()
return "Користувач додан"
def add_new_client(
self, phonenumber: str, name: str, surname: str, bonus: str
) -> str:
"""add new client into db"""
if self.if_client_exists(phonenumber):
return "Такий клієнт вже існує"
else:
with self.connection.cursor() as cursor:
cursor.execute(
"""INSERT INTO train_table (phonenumber, name, surname, bonus) VALUES (%s, %s, %s, %s)""",
(
phonenumber,
name.capitalize(),
surname.capitalize(),
int(bonus),
),
)
self.connection.commit()
return "Інформацію додано"
def minus_all_bonus_from_exist_client(self, phonenumber: str) -> str:
"""minus bonuses by phonenumber"""
with self.connection.cursor() as cursor:
cursor.execute(
"""UPDATE train_table SET bonus = 0 WHERE phonenumber = %s;""",
(phonenumber,),
)
self.connection.commit()
if bool(cursor.rowcount):
return "Бонуси анульовані"
return "Такого клієнту не існує"
def delete_exist_client(self, phonenumber: str) -> str:
"""delete client from db"""
with self.connection.cursor() as cursor:
cursor.execute(
"""DELETE FROM train_table WHERE phonenumber = %s;""",
(phonenumber,),
)
self.connection.commit()
if bool(cursor.rowcount):
return "Клієнт видален з бази"
return "Такого клієнту не існує"
def delete_exist_admin(self, user_id: str) -> str:
"""delete admin from db"""
with self.connection.cursor() as cursor:
cursor.execute(
"""DELETE FROM admins WHERE admins = %s;""",
(user_id,),
)
self.connection.commit()
if bool(cursor.rowcount):
return "Користувач видален з бази"
return "Такого користувача не існує"
def delete_exist_main_admin(self, user_id: str) -> str:
"""delete user from db"""
with self.connection.cursor() as cursor:
cursor.execute(
"""DELETE FROM main_admins WHERE admins = %s;""",
(user_id,),
)
self.connection.commit()
if bool(cursor.rowcount):
return "Адмін видален з бази"
return "Такого адміну не існує"
def plus_bonus_to_exist_client(self, phonenumber: str, bonus: str) -> str:
"""add bonus to client"""
with self.connection.cursor() as cursor:
cursor.execute(
"""UPDATE train_table SET bonus = bonus + %s WHERE phonenumber = %s;""",
(
int(bonus),
phonenumber,
),
)
self.connection.commit()
if bool(cursor.rowcount):
return "Бонуси додані"
return "Такого клієнту не існує"
def minus_bonus_from_exist_client(self, phonenumber: str, bonus: str) -> str:
"""minus bonuses"""
with self.connection.cursor() as cursor:
cursor.execute(
"""UPDATE train_table SET bonus = bonus - %s WHERE phonenumber = %s;""",
(
int(bonus),
phonenumber,
),
)
self.connection.commit()
if bool(cursor.rowcount):
return "Бонуси списані"
return "Такого клієнту не існує"
def edit_client_name(self, phonenumber: str, new_name: str) -> str:
"""edit clients' name"""
with self.connection.cursor() as cursor:
cursor.execute(
"""UPDATE train_table SET name = %s WHERE phonenumber = %s;""",
(
new_name.capitalize(),
phonenumber,
),
)
self.connection.commit()
if bool(cursor.rowcount):
return "Ім'я клієнту успішно змінено"
return "Такого клієнту не існує"
def edit_client_surname(self, phonenumber: str, new_surname: str) -> str:
"""edit clients' surname"""
with self.connection.cursor() as cursor:
cursor.execute(
"""UPDATE train_table SET surname = %s WHERE phonenumber = %s;""",
(
new_surname.capitalize(),
phonenumber,
),
)
self.connection.commit()
if bool(cursor.rowcount):
return "Прізвище клієнту успішно змінено"
return "Такого клієнту не існує"
def total_amount_of_clients(self) -> str:
"""return anount of clients in db"""
with self.connection.cursor() as cursor:
cursor.execute("""SELECT COUNT(id) as max_value FROM train_table;""")
result = cursor.fetchall()
return f"Загальна кількість клієнтів в базі: {result[0][0]}"
def get_client_info(self, phonenumber: str) -> str:
"""return clients' info"""
with self.connection.cursor() as cursor:
cursor.execute(
"""SELECT * FROM train_table WHERE phonenumber = %s;""",
(phonenumber,),
)
result = cursor.fetchall()
return (
f"Прізвище та ім'я: {result[0][3]} {result[0][2]}\n"
f"Номер телефону: {result[0][1]}\n"
f"Кількість бонусів: {result[0][4]}"
)