-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
302 lines (265 loc) · 10.1 KB
/
main.py
File metadata and controls
302 lines (265 loc) · 10.1 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
from __future__ import print_function
import mysql.connector
from mysql.connector import errorcode
from datetime import date, datetime, timedelta
def connectDB(cursor):
try:
cursor.execute("USE {}".format(DB_NAME))
print('\n\tConnection established!!!\n')
except mysql.connector.Error as err:
print("Database {} does not exist".format(DB_NAME))
exit(1)
def table_exists(cursor, table_name):
try:
cursor.execute(f"SHOW TABLES LIKE '{table_name}'")
if cursor.fetchone():
return True
else:
print('Table does not exist.')
except Error as e:
return False
def sign_up(username, password, table):
cursor = cnx.cursor()
try:
query = f"SELECT * FROM {table} WHERE {table[:-1]}_username = %s"
cursor.execute(query, (username,))
user = cursor.fetchone()
if user:
print("\tError: Username already exists.\n")
else:
insert_query = f"INSERT INTO {table} ( {table[:-1]}_username, {table[:-1]}_password) VALUES (%s, %s)"
cursor.execute(insert_query, (username, password))
cnx.commit()
print("\tSign-up successful!\n")
except Error as err:
print(f"Error: {err}")
def log_in(username, password, table):
try:
query = f"SELECT * FROM {table} WHERE {table[:-1]}_username = %s AND {table[:-1]}_password = %s"
cursor.execute(query, (username, password))
user = cursor.fetchone()
if user:
print("\tLog-in successful!\n")
session(table,username)
else:
print("\tLog-in failed. Invalid username or password.\n")
except Error as e:
print("Error")
def publish_request_as_author(table,username):
if table == 'authors':
try:
book_title = input("Enter the book title: ")
auth_username = username
pub_username = input("Enter the publisher's username: ")
# Insert values into the 'requests' table
query = "INSERT INTO requests (book_title, author_username, publisher_username) VALUES (%s, %s, %s)"
values = (book_title, auth_username, pub_username)
cursor.execute(query, values)
cnx.commit()
print("\tRequest pending...")
except mysql.connector.Error as err:
print(f"Error: {err}")
else:
print('You do not have access.')
def view_requests_as_author(table, username):
if table!='authors':
print('You do not have access.')
return
try:
query = f"SELECT * FROM requests WHERE author_username = '{username}';"
cursor.execute(query)
result = cursor.fetchall()
print("Requests as an author:")
for row in result:
print(row)
except Error as e:
print(f"Error: {e}")
def view_requests_as_publisher(table, username):
if table!='publishers':
print('You do not have access.')
return
try:
query = f"SELECT * FROM requests WHERE publisher_username = '{username}';"
cursor.execute(query)
result = cursor.fetchall()
print("Requests as a publisher:")
for row in result:
print(row)
except Error as e:
print(f"Error: {e}")
def approve_request_as_publisher(table, username):
if table!='publishers':
print('You do not have access.')
return
try:
author_name = input("Enter the author name: ")
book_title = input("Enter the book title: ")
query = f"UPDATE requests SET request_status = 'approved' WHERE author_username = '{author_name}' AND book_title = '{book_title}' AND publisher_username = '{username}';"
cursor.execute(query)
cnx.commit()
print("Request approved successfully as a publisher.")
except Error as e:
print(f"Error: {e}")
def insert_keyword_as_author(table, username):
if table!='authors':
print('You do not have access.')
return
book_title = input("Enter the book title: ")
get_book_id_query = "SELECT book_id FROM books WHERE book_title = %s LIMIT 1"
cursor.execute(get_book_id_query, (book_title,))
book_id_result = cursor.fetchone()
if book_id_result:
keyword = input("Enter a keyword: ")
book_id = book_id_result[0]
fill_keyword_query = "CALL fill_keyword(%s, %s)"
cursor.execute(fill_keyword_query, (book_id, keyword))
print("Keyword added successfully!")
cnx.commit()
else:
print("Book not found.")
def view_all_books():
view_books_query = "SELECT * FROM books"
cursor.execute(view_books_query)
books = cursor.fetchall()
if books:
for book in books:
print(book)
else:
print("No books found.")
def search_using_keyword():
keyword = input("Enter the keyword to search for: ")
# Retrieve books corresponding to the keyword from the books table
search_query = f"SELECT * FROM books WHERE book_id IN (SELECT book_id FROM keywords WHERE keyword = '{keyword}')"
cursor.execute(search_query)
books = cursor.fetchall()
if books:
for book in books:
print(book)
else:
print(f"No books found for the keyword '{keyword}'.")
def place_order_as_user(table,username):
if table!='users':
print('You do not have access.')
return
book_id = input("Enter the book ID to place an order: ")
# Check if the book exists
check_book_query = f"SELECT * FROM books WHERE book_id = {book_id}"
cursor.execute(check_book_query)
book = cursor.fetchone()
if book:
# order_date = date.today()
insert_order_query = f"INSERT INTO sales (user_username, book_id) VALUES ('{username}', {book_id})"
cursor.execute(insert_order_query)
cnx.commit()
print("Order placed successfully!")
else:
print(f"No book found with ID {book_id}.")
def view_orders_as_user(table, username):
if table!='users':
print('You do not have access.')
return
try:
query = f"SELECT * FROM sales WHERE user_username = '{username}';"
cursor.execute(query)
result = cursor.fetchall()
print("Orders:")
for row in result:
print(row)
except Error as e:
print(f"Error: {e}")
def view_orders_as_publisher(table, username):
if table!='publishers':
print('You do not have access.')
return
try:
query = f"SELECT * FROM books WHERE publisher_username = '{username}';"
cursor.execute(query)
result = cursor.fetchall()
print("Orders:")
for row in result:
query2 = f"SELECT * FROM sales WHERE book_id = '{row[0]}';"
cursor.execute(query2)
result2 = cursor.fetchall()
for row2 in result2:
print(row2)
except Error as e:
print(f"Error: {e}")
def deliver_order_as_publisher(table, username):
if table!='publishers':
print('You do not have access.')
return
try:
sale_id = input("Enter the sale_id: ")
query = f"UPDATE sales SET order_status = 'delivered' WHERE sale_id = '{sale_id}' AND order_status='pending';"
cursor.execute(query)
cnx.commit()
print("Delivered successfully.")
except Error as e:
print(f"Error: {e}")
def session(table, username):
query = 1
while query != 0:
query = int(input("\n\tWhat do you wanna do? (Enter 21 for a list of available queries): "))
if query == 1 :
publish_request_as_author(table,username)
elif query == 2:
view_requests_as_author(table,username)
elif query == 3:
view_requests_as_publisher(table,username)
elif query == 4:
approve_request_as_publisher(table,username)
elif query == 5:
insert_keyword_as_author(table,username)
elif query == 6:
view_all_books()
elif query == 7:
search_using_keyword()
elif query == 8:
place_order_as_user(table,username)
elif query == 9:
view_orders_as_user(table,username)
elif query == 10:
view_orders_as_publisher(table,username)
elif query == 11:
deliver_order_as_publisher(table,username)
elif query == 21:
print(
"0)quit\n"
"1)publish_request_as_author\n"
"2)view_requests_as_author\n"
"3)view_requests_as_publisher\n"
"4)approve_request_as_publisher\n"
"5)insert_keyword_as_author\n"
"6)view_all_books\n"
"7)search_using_keyword\n"
"8)place_order_as_user\n"
"9)view_orders_as_user\n"
"10)view_orders_as_publisher\n"
"11)deliver_order_as_publisher\n")
def home():
choice = 'A'
category = ['admins', 'publishers', 'authors', 'users']
while choice!='Q':
choice = input("\n\tSign-up (S) or Log-in (L) or Quit(Q): ")
if choice != 'S' and choice != 'L' :
continue
if choice!='Q':
table=input("Enter category (admin=0, publisher=1, author=2, user=3): ")
if not table_exists(cursor, category[int(table)]):
continue
if choice == 'S':
username_input = input("Enter your username: ")
password_input = input("Enter your password: ")
sign_up(username_input, password_input,category[int(table)])
elif choice == 'L':
username_input = input("Enter your username: ")
password_input = input("Enter your password: ")
log_in(username_input, password_input,category[int(table)])
DB_NAME = 'dbfb'
cnx = mysql.connector.connect(user='root',password='mysqlPass')
cursor = cnx.cursor()
connectDB(cursor)
home()
cnx.commit()
cursor.close()
cnx.close()