-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBase.py
More file actions
177 lines (153 loc) · 6.53 KB
/
DataBase.py
File metadata and controls
177 lines (153 loc) · 6.53 KB
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
import sqlite3
import time
import math
import datetime
class DataBase:
def __init__(self, db):
self.__db = db
self.__cur = db.cursor()
def addUser(self, username, email, password_hash):
try:
self.__cur.execute('SELECT COUNT() as "count" FROM users WHERE email = ?', (email,))
res = self.__cur.fetchone()
if res['count'] > 0:
return False
tm = math.floor(time.time())
self.__cur.execute('INSERT INTO users VALUES (NULL, ?, ?, ?, ?, ?)', (username, email, password_hash, 0, tm))
self.__db.commit()
return True
except sqlite3.Error as e:
print(f'Ошибка добавления пользователя: {str(e)}')
return False
def getUser(self, user_id):
try:
self.__cur.execute('SELECT * FROM users WHERE id = ? LIMIT 1', (user_id,))
return self.__cur.fetchone()
except sqlite3.Error as e:
print(f'Ошибка получения пользователя: {str(e)}')
return None
def getUserByEmail(self, email):
try:
self.__cur.execute('SELECT * FROM users WHERE email = ? LIMIT 1', (email,))
return self.__cur.fetchone()
except sqlite3.Error as e:
print(f'Ошибка поиска пользователя по email: {str(e)}')
return None
# Обновление данных пользователя
def updateUser(self, user_id, username, email):
try:
self.__cur.execute('UPDATE users SET username = ?, email = ? WHERE id = ?', (username, email, user_id))
self.__db.commit()
return True
except sqlite3.Error as e:
print('Ошибка обновления пользователя: ' + str(e))
return False
# Обновление пароля
def updatePassword(self, user_id, password_hash):
try:
self.__cur.execute('UPDATE users SET password = ? WHERE id = ?', (password_hash, user_id))
self.__db.commit()
return True
except sqlite3.Error as e:
print('Ошибка обновления пароля: ' + str(e))
return False
def addNews(self, title, image_url, short_description, category, text):
try:
tm = math.floor(time.time())
current_time = datetime.datetime.fromtimestamp(tm)
date = current_time.strftime("%d.%m.%Y %H:%M")
category = category.lower()
self.__cur.execute('INSERT INTO news VALUES(NULL, ?, ?, ?, ?, ?, ?, ?)', (title, image_url, short_description, category, date, text, tm))
self.__db.commit()
return True
except sqlite3.Error as e:
print(f'Ошибка добавления новости: {str(e)}')
return False
def getAllNews(self):
try:
self.__cur.execute('SELECT * FROM news ORDER BY date DESC')
res = self.__cur.fetchall()
if not res:
return []
return res
except sqlite3.Error as e:
print('Ошибка получения всех новостей: ' + str(e))
return False
def getNewsById(self, news_id):
try:
self.__cur.execute('SELECT * FROM news WHERE id = ?', (news_id,))
res = self.__cur.fetchone()
if not res:
return []
return res
except sqlite3.Error as e:
print('Ошибка получения новости по id: ' + str(e))
return False
def getNewsByCategory(self, category):
try:
self.__cur.execute('SELECT * FROM news WHERE category = ? ORDER BY date DESC', (category,))
res = self.__cur.fetchall()
if not res:
return []
return res
except sqlite3.Error as e:
print('Ошибка получения новостей по категории: ' + str(e))
return False
def deleteNews(self, news_id):
try:
self.__cur.execute('DELETE FROM news WHERE id = ?', (news_id,))
self.__db.commit()
return True
except sqlite3.Error as e:
print(f'Ошибка удаления новости: {str(e)}')
return False
def addProduct(self, title, image_url, info, category):
try:
tm = math.floor(time.time())
current_time = datetime.datetime.fromtimestamp(tm)
date = current_time.strftime("%d.%m.%Y %H:%M")
category = category.lower()
self.__cur.execute('INSERT INTO shop VALUES(NULL, ?, ?, ?, ?, ?, ?)', (title, image_url, info, category, date, tm))
self.__db.commit()
return True
except sqlite3.Error as e:
print('Ошибка в создании товара: ' + str(e))
return False
def getAllProducts(self):
try:
self.__cur.execute('SELECT * FROM shop ORDER BY date DESC')
res = self.__cur.fetchall()
if not res:
return []
return res
except sqlite3.Error as e:
print('Ошибка получения всех продуктов: ' + str(e))
return False
def getProductsByCategory(self, category):
try:
self.__cur.execute('SELECT * FROM shop WHERE category = ? ORDER BY date DESC', (category,))
res = self.__cur.fetchall()
if not res:
return []
return res
except sqlite3.Error as e:
print('Ошибка получения продуктов по категории: ' + str(e))
return False
def getProductsById(self, product_id):
try:
self.__cur.execute('SELECT * FROM shop WHERE id = ?', (product_id,))
res = self.__cur.fetchone()
if not res:
return []
return res
except sqlite3.Error as e:
print('Ошибка получения новости по id: ' + str(e))
return False
def deleteProduct(self, product_id):
try:
self.__cur.execute('DELETE FROM shop WHERE id = ?', (product_id,))
self.__db.commit()
return True
except sqlite3.Error as e:
print(f'Ошибка удаления новости: {str(e)}')
return False