-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
311 lines (248 loc) · 9.41 KB
/
db.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
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
303
304
305
306
307
308
309
310
311
from flask import g
import psycopg2
import psycopg2.extras
import config
data_source_name = config.data_source_name
# Open database connection
def open_db_connection():
g.connection = psycopg2.connect(data_source_name)
g.cursor = g.connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
# Close database connection
def close_db_connection():
g.cursor.close()
g.connection.close()
# Create a user
def create_user(name, email, zip, password, bio, rating, active):
query = '''
INSERT INTO "user" (name, email, zip, password, bio, rating, active)
VALUES (%(name)s, %(email)s, %(zip)s, %(password)s, %(bio)s, %(rating)s, %(active)s)
'''
g.cursor.execute(query, {'name': name, 'email': email, 'zip': zip, 'password': password, 'bio': bio, 'rating': rating, 'active': active})
g.connection.commit()
return g.cursor.rowcount
# Find a user by their email
def find_user_by_email(email):
g.cursor.execute('SELECT * FROM "user" WHERE email = %(email)s', {'email': email})
return g.cursor.fetchone()
# Find a user by name
def find_user_by_id(id):
query = '''
SELECT u.*, file_path
FROM "user" u
LEFT JOIN user_photo ON u.id = user_photo.id
WHERE u.id = %(id)s;
'''
g.cursor.execute(query, {'id': id})
return g.cursor.fetchone()
# Returns a list of all users
def all_users():
g.cursor.execute('SELECT * FROM "user" ORDER BY id')
return g.cursor.fetchall()
# Allows a user to update their profile
def update_user(name, email, zip, password, bio, user_id):
query = '''
UPDATE "user"
SET name = %(name)s, email = %(email)s, zip = %(zip)s, password = %(password)s, bio = %(bio)s
WHERE id = %(id)s
'''
g.cursor.execute(query, {'id': user_id, 'name': name, 'email': email, 'zip': zip, 'password': password, 'bio': bio})
g.connection.commit()
return {'id': user_id, 'rowcount': g.cursor.rowcount}
# Allows the admin to update a user
def admin_update_user(name, email, password, bio, rating, user_id):
query = '''
UPDATE "user"
SET name = %(name)s, email = %(email)s, password = %(password)s, bio = %(bio)s, rating = %(rating)s
WHERE id = %(id)s
'''
g.cursor.execute(query, {'id': user_id, 'name': name, 'email': email, 'password': password, 'bio': bio, 'rating': rating})
g.connection.commit()
return g.cursor.rowcount
# Disable a user by their ID
def disable_user_by_id(user_id):
g.cursor.execute('UPDATE "user" SET active = FALSE WHERE id = %(user_id)s', {'user_id': user_id})
g.connection.commit()
return g.cursor.rowcount
# Enable a user by their ID
def enable_user_by_id(user_id):
g.cursor.execute('UPDATE "user" SET active = TRUE WHERE id = %(user_id)s', {'user_id': user_id})
g.connection.commit()
return g.cursor.rowcount
# Finds a post by it's ID
def find_post_by_id(id):
query = '''
SELECT *, p.id AS "post_id" FROM post p
INNER JOIN "user" u ON u.id = p.user_id
LEFT JOIN "post_photo" ON p.id = post_photo.id
WHERE p.id = %(id)s
'''
g.cursor.execute(query, {'id': id})
return g.cursor.fetchone()
# Finds all posts by a user
def posts_by_user(user_id):
query = '''
SELECT *, p.id AS "post_id" FROM post p
INNER JOIN "user" u ON u.id = p.user_id
LEFT JOIN "post_photo" ON p.id = post_photo.id
WHERE u.active = TRUE AND user_id = %(user_id)s
ORDER BY p.id;
'''
g.cursor.execute(query, {'user_id': user_id})
g.connection.commit()
return g.cursor.fetchall()
# Finds all favorites by a user
def favorites_by_user(user_id):
query = '''
SELECT * FROM favorite f
INNER JOIN post p ON p.id = f.post_id
INNER JOIN "user" u ON u.id = f.user_id
LEFT JOIN "post_photo" ON p.id = post_photo.id
WHERE u.id = %(user_id)s AND u.active = TRUE AND p.quantity > 0
'''
g.cursor.execute(query, {'user_id': user_id})
g.connection.commit()
return g.cursor.fetchall()
# Checks to see if the user has already added a post to favorites
def find_duplicate_in_favorites(user_id, post_id):
query = '''
SELECT * FROM favorite f
WHERE post_id = %(post_id)s AND user_id = %(user_id)s
'''
g.cursor.execute(query, {'user_id': user_id, 'post_id': post_id})
g.connection.commit()
return g.cursor.fetchall()
# Adds a post to favorites
def add_to_favorites(user_id, post_id):
query = '''
INSERT INTO favorite (user_id, post_id) VALUES (%(user_id)s, %(post_id)s);
'''
g.cursor.execute(query, {'post_id': post_id, 'user_id': user_id})
g.connection.commit()
return g.cursor.rowcount
# Deletes a favorite by the post_id
def delete_from_favorites(user_id, post_id):
query = '''
DELETE FROM favorite WHERE post_id = %(post_id)s AND user_id = %(user_id)s
'''
g.cursor.execute(query, {'user_id': user_id, 'post_id': post_id})
g.connection.commit()
return g.cursor.rowcount
# Creates a post
def create_post(user_id, price, quantity, unit, product, category, description):
query = '''
INSERT INTO post (user_id, price, quantity, unit, product, "category", description)
VALUES (%(user_id)s, %(price)s, %(quantity)s, %(unit)s, %(product)s, %(category)s, %(description)s)
RETURNING id
'''
g.cursor.execute(query, {'user_id': user_id, 'price': price, 'quantity': quantity, 'unit': unit, 'product': product, 'category': category, 'description': description})
g.connection.commit()
return {'id': g.cursor.fetchone()['id'], 'rowcount': g.cursor.rowcount}
def init_post_photo(id):
g.cursor.execute('INSERT INTO post_photo (id) VALUES (%(id)s)', {'id': id})
g.connection.commit()
g.cursor.execute('SELECT * FROM post_photo WHERE id = (%(id)s)', {'id': id})
return g.cursor.fetchone()
def set_post_photo(photo_id, file_path):
query = '''
UPDATE post_photo
SET file_path = %(file_path)s
WHERE id = %(id)s
'''
g.cursor.execute(query, {'file_path': file_path, 'id': photo_id})
g.connection.commit()
return g.cursor.rowcount
def init_user_photo(id):
g.cursor.execute('INSERT INTO user_photo (id) VALUES (%(id)s)', {'id': id})
g.connection.commit()
g.cursor.execute('SELECT * FROM user_photo WHERE id = (%(id)s)', {'id': id})
return g.cursor.fetchone()
def set_user_photo(photo_id, file_path):
query = '''
UPDATE user_photo
SET file_path = %(file_path)s
WHERE id = %(id)s
'''
g.cursor.execute(query, {'file_path': file_path, 'id': photo_id})
g.connection.commit()
return g.cursor.rowcount
#
# def delete_user_photo(photo_id):
# query = '''
# DELETE FROM user_photo WHERE id = %(photo_id)s;
# '''
# g.cursor.execute(query, {'photo_id': photo_id})
# g.connection.commit()
# return g.cursor.rowcount
# Returns the entire post table
def all_posts():
query = '''
SELECT *, p.id AS "post_id" FROM post p
INNER JOIN "user" u ON u.id = p.user_id
LEFT JOIN "post_photo" ON p.id = post_photo.id
WHERE u.active = TRUE AND p.quantity > 0
ORDER BY p.id;
'''
g.cursor.execute(query)
return g.cursor.fetchall()
# Updates/edits a post
def update_post(price, quantity, unit, product, description, post_id):
query = '''
UPDATE post
SET price = %(price)s, product = %(product)s, unit = %(unit)s, quantity = %(quantity)s, description = %(description)s
WHERE id = %(id)s
'''
g.cursor.execute(query, {'id': post_id, 'price': price, 'quantity': quantity, 'unit': unit, 'product': product, 'description': description})
g.connection.commit()
return g.cursor.rowcount
# Gets the quantity of a post
def get_quantity(post_id):
query = '''
SELECT quantity FROM post
WHERE id = %(id)s
'''
g.cursor.execute(query, {'id': post_id})
return g.cursor.fetchone()
# Updates a post with the new quantity
def update_quantity(post_id, quantity, amount):
if quantity < amount:
return 0
query = '''
UPDATE post
SET quantity = %(quantity)s - %(amount)s
WHERE id = %(id)s
'''
g.cursor.execute(query, {'id': post_id, 'quantity': quantity, 'amount': amount})
g.connection.commit()
return g.cursor.rowcount
# Finds products that match the search query
def search_products(item):
query = '''
SELECT *, p.id AS "post_id" FROM post p
INNER JOIN "user" u ON u.id = p.user_id
LEFT JOIN "post_photo" ON p.id = post_photo.id
WHERE u.active = TRUE AND product ~* %(item)s AND p.quantity > 0
ORDER BY p.id;
'''
g.cursor.execute(query, {'item': item})
return g.cursor.fetchall()
# Filters products by category
def filter_products(key_list):
pattern = '|'.join(key_list)
query = '''
SELECT *, p.id AS "post_id" FROM post p
INNER JOIN "user" u ON u.id = p.user_id
LEFT JOIN "post_photo" ON p.id = post_photo.id
WHERE u.active = TRUE AND %(pattern)s ~* category AND p.quantity > 0
'''
g.cursor.execute(query, {'pattern': pattern})
return g.cursor.fetchall()
# Deletes a single post by post ID
def delete_post_by_id(post_id):
query = '''
DELETE FROM post_photo WHERE id = %(post_id)s;
DELETE FROM favorite WHERE post_id = %(post_id)s;
DELETE FROM post WHERE id = %(post_id)s;
'''
g.cursor.execute(query, {'post_id': post_id})
g.connection.commit()
return g.cursor.rowcount