This repository has been archived by the owner on Apr 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdatabase.py
205 lines (158 loc) · 5.89 KB
/
database.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
import sqlite3
import logging
# Initializing logs
log = logging.getLogger(__name__)
log.debug("Trying to connect to database.sqlite or create new if not exist")
conn = sqlite3.connect("database.sqlite", check_same_thread=False)
with conn:
log.debug("Initializing cursor")
cursor = conn.cursor()
# Creating user table if not exists
cursor.executescript("""CREATE TABLE IF NOT EXISTS users
(
chat_id INTEGER not null
primary key,
first_name VARCHAR not null,
username VARCHAR,
phone_number VARCHAR,
cart TEXT,
is_making_order BOOLEAN,
is_operator BOOLEAN,
is_administrator BOOLEAN
);
""")
# Creating category table if not exists
cursor.executescript("""CREATE TABLE IF NOT EXISTS categories
(
category_id INTEGER not null
primary key autoincrement,
title VARCHAR
);
""")
# Creating products table if not exists
cursor.executescript("""CREATE TABLE IF NOT EXISTS products
(
id INTEGER not null
primary key autoincrement,
title VARCHAR,
description TEXT,
price INTEGER,
image BLOB,
category_id INTEGER,
bot_shows BOOLEAN
);
""")
# Creating orders table if not exists
cursor.executescript("""CREATE TABLE IF NOT EXISTS orders
(
order_id INTEGER not null
primary key autoincrement,
chat_id INTEGER,
contacts TEXT,
order_items TEXT,
order_date DATE,
status INTEGER,
note TEXT
);
""")
conn.commit()
def if_user_exists(chat_id):
try:
cursor.execute("SELECT * FROM users WHERE chat_id LIKE ?", [chat_id])
g_user = cursor.fetchall()
log.debug(g_user[0][1] + " was checked on existing")
return True
except IndexError:
return False
def new_user(chat_id, first_name, username, phone_number):
cursor.execute("""INSERT INTO users (chat_id, first_name, username, phone_number, is_making_order, is_operator,
is_administrator) VALUES (?, ?, ?, ?, ?, ?, ?)
""", (chat_id, first_name, username, phone_number, 0, 0, 0))
conn.commit()
def new_order(chat_id, contacts, order_items, order_date, status, note):
cursor.execute("""
INSERT INTO orders (chat_id, contacts, order_items, order_date, status, note)
VALUES (?, ?, ?, ?, ?, ?)
""", (chat_id, contacts, order_items, order_date, status, note))
conn.commit()
def add_category(title):
cursor.execute("INSERT INTO category (title) VALUES (?)",
[title])
conn.commit()
log.debug(f"Added category: {title}")
def add_product(title, description, price, image, category_id):
cursor.execute("""INSERT INTO products (title, description, price, image, category_id, bot_shows)
VALUES (?, ?, ?, ?, ?, ?)""", (title, description, price, image, category_id, 1))
conn.commit()
def get_categories():
cursor.execute("SELECT * FROM categories")
return cursor.fetchall()
def get_products():
cursor.execute("SELECT * FROM products")
return cursor.fetchall()
def get_product_ids():
cursor.execute("SELECT id FROM products")
return cursor.fetchall()
def get_product_by_id(prod_id):
cursor.execute("SELECT * FROM products WHERE id LIKE ?", [prod_id])
product = cursor.fetchall()
return product[0]
def get_user_by_id(user_id):
cursor.execute("SELECT * FROM users WHERE chat_id LIKE ?", [user_id])
g_user = cursor.fetchall()
try:
return g_user[0]
except IndexError:
return None
def get_cart_by_id(user_id):
cursor.execute("SELECT cart FROM users WHERE chat_id LIKE ?", [user_id])
user_cart = cursor.fetchall()
return user_cart[0][0]
def get_orders_by_id(user_id):
cursor.execute("SELECT * FROM orders WHERE chat_id LIKE ?", [user_id])
user_cart = cursor.fetchall()
return user_cart
def get_orders_ids_by_id(user_id):
cursor.execute("SELECT order_id FROM orders WHERE chat_id LIKE ?", [user_id])
order_ids = cursor.fetchall()
return order_ids
def get_making_order_by_id(user_id):
cursor.execute("SELECT is_making_order FROM users WHERE chat_id LIKE ?", [user_id])
making_orders = cursor.fetchall()
return making_orders[0][0]
def get_operators():
cursor.execute("SELECT chat_id FROM users WHERE is_operator LIKE ?", [1])
operators = cursor.fetchall()
try:
return operators[0]
except IndexError:
return None
def set_cart_to_user(user_id, cart):
cursor.executemany("""UPDATE users
SET cart = ? WHERE chat_id = ?""", ((cart, user_id), ))
conn.commit()
def set_making_order_status_to_user(user_id, status):
cursor.executemany("""UPDATE users
SET is_making_order = ? WHERE chat_id = ?""", ((status, user_id), ))
conn.commit()
def set_phone_number_to_user(user_id, phone):
cursor.executemany("""UPDATE users
SET phone_number = ? WHERE chat_id = ?""", ((phone, user_id), ))
conn.commit()
def search_product(search_text):
cursor.execute("SELECT * FROM products WHERE title LIKE ?", [search_text])
log.debug(f"Search: {search_text}")
products = cursor.fetchall()
return products
# add_product("cat2", "this is good cat item", 200, "images/item_1.png", 12)
# new_user("1111", "Max2", None, "None")
# pr = get_products()
# print(get_products())
# print(get_cart_by_id(778508362))
# new_order(778508362, "Bla Bla Bla", "items", "12.12.2020", 1, "None")
# print(get_orders_by_id(778508362))
# [(1, 778508362, 'Bla Bla Bla', 'Address', 'items', 300, '12.12.2020', 0, 'None'),
# (2, 778508362, 'Bla Bla Bla2', 'Address', 'items', 300, '12.12.2020', 0, 'None')]
# print(get_making_order_by_id(1111))
# print(get_operators())
# print(max(get_orders_ids_by_id(778508362))[0])