-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
223 lines (177 loc) · 6.57 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
from flask import g
import psycopg2
import psycopg2.extras
def open_db():
g.connection = psycopg2.connect(database="postgres", user="postgres", password="postgres")
g.cursor = g.connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
def close_db():
g.cursor.close()
g.connection.close()
def all_listings():
g.cursor.execute('''SELECT * FROM listing WHERE available_quantity > 0 ORDER BY date_created DESC;''')
return g.cursor.fetchall()
def all_users():
g.cursor.execute('''SELECT email FROM public.user ''')
return g.cursor.fetchall()
def title_like_listings(search_query):
search_query = '%' + search_query + '%'
query = \
'''SELECT * FROM listing WHERE lower(title) LIKE %(search_query)s;'''
g.cursor.execute(query, {'search_query': search_query.lower()})
return g.cursor.fetchall()
def search_like_category(search_query):
search_query = '%' + search_query + '%'
query = \
'''
SELECT * FROM listing inner join category on listing.category_id = category.category_id
WHERE name LIKE %(search_query)s;
'''
g.cursor.execute(query, {'search_query': search_query.lower()})
return g.cursor.fetchall()
def search_like_users(search_query):
search_query = '%' + search_query + '%'
query = \
'''
select * from "user"
where lower(email) LIKE %(search_query)s
or lower(first_name) LIKE %(search_query)s
or lower(last_name) LIKE %(search_query)s;
'''
g.cursor.execute(query, {'search_query': search_query.lower()})
return g.cursor.fetchall()
def add_listing(new_product):
query = \
'''
insert into listing(seller_id, title, photo, description, original_quantity, available_quantity, unit_type,
total_price, price_per_unit, category_id, is_tradeable, is_active,
date_created, date_harvested, date_modified)
values (%(seller_id)s, %(title)s, %(photo)s, %(description)s, %(original_quantity)s,
%(available_quantity)s, %(unit_type)s, %(total_price)s, %(price_per_unit)s,
%(category_id)s, %(is_tradeable)s, true, now(), %(date_harvested)s, now());
'''
g.cursor.execute(query, new_product)
g.connection.commit()
return g.cursor.rowcount
def get_one_listing(listing_id):
g.cursor.execute('SELECT * FROM listing WHERE listing_id = %(id)s;'
, {'id': listing_id})
return g.cursor.fetchone()
def get_one_user(user_id):
g.cursor.execute('SELECT * FROM "user" WHERE user_id = %(id)s;',
{'id': user_id})
return g.cursor.fetchone()
def find_user(email):
g.cursor.execute('SELECT * FROM "user" WHERE email = %(email)s;',
{'email': email})
return g.cursor.fetchone()
def get_one_login(email):
g.cursor.execute('SELECT * FROM "user" WHERE email = %(email)s;',
{'email': email})
return g.cursor.fetchone()
def get_user_listings(user_id):
g.cursor.execute('SELECT * FROM listing where seller_id = %(id)s;',
{'id': user_id})
return g.cursor.fetchall()
def update_available_quantity(bought_amount, listing_id):
query = \
'''
UPDATE listing
SET available_quantity = available_quantity - %(bought_amount)s
WHERE listing_id = %(id)s;
'''
g.cursor.execute(query, {'id': listing_id,
'bought_amount': bought_amount})
g.connection.commit()
return g.cursor.rowcount
def get_user_address(user_id):
query = \
'''
SELECT street, city, state.name, abbrev, zipcode FROM ("user"
INNER JOIN address on "user".address_id = address.address_id
INNER JOIN state on address.state_id = state.state_id)
WHERE "user".user_id = %(id)s;
'''
g.cursor.execute(query, {'id': user_id})
return g.cursor.fetchone()
def get_user_address_via_listing(listing_id):
query = \
'''
SELECT street, city, state.name, abbrev, zipcode FROM (listing
INNER JOIN "user" on listing.seller_id = "user".user_id
INNER JOIN address on "user".address_id = address.address_id
INNER JOIN state on address.state_id = state.state_id)
WHERE listing.listing_id = %(id)s;
'''
g.cursor.execute(query, {'id': listing_id})
return g.cursor.fetchone()
def get_listing_details_for_confirmation_page(listing_id):
query = \
'''
SELECT listing.title, listing.photo, listing.unit_type,
"user".first_name, "user".last_name FROM (listing
INNER JOIN "user" on listing.seller_id = "user".user_id)
WHERE listing.listing_id = %(listing_id)s;
'''
g.cursor.execute(query, {'listing_id': listing_id})
return g.cursor.fetchone()
def add_new_order(
listing_id,
qty,
total_cost,
buyer_id,
):
query = \
'''
INSERT into orders(listing_id, quantity, total_cost, buyer_id, time_placed)
values(%(listing_id)s, %(qty)s, %(total_cost)s, %(buyer_id)s, now());
'''
g.cursor.execute(query, {
'listing_id': listing_id,
'qty': qty,
'total_cost': total_cost,
'buyer_id': buyer_id,
})
g.connection.commit()
return g.cursor.rowcount
def create_new_address(address):
query = \
'''
INSERT INTO public.address(address_id, street, city, state_id, zipcode)
VALUES (default ,%(street)s, %(city)s, %(state)s, %(zipcode)s);
'''
query_to_get_a_id = \
'''SELECT address_id FROM address ORDER BY address_id DESC limit 1;'''
g.cursor.execute(query, address)
g.connection.commit()
g.cursor.execute(query_to_get_a_id)
row_id = g.cursor.fetchone()
return (g.cursor.rowcount, row_id)
def get_all_states():
query = '''
SELECT state_id, state.name FROM public.state;
'''
g.cursor.execute(query)
return g.cursor.fetchall()
def create_user(new_user):
query = \
'''
INSERT INTO public.user(address_id, email, first_name, last_name, profile_pic, password, role, bio)
VALUES (%(address_id)s, %(email)s, %(first)s, %(last)s, %(photo)s, %(pass)s, 'user', %(bio)s);
'''
g.cursor.execute(query, new_user)
g.connection.commit()
return g.cursor.rowcount
def get_latest_user_id():
query = '''SELECT count(user_id) from "user";'''
g.cursor.execute(query)
return g.cursor.fetchone()[0] + 1
def get_user_orders(user_id):
query = \
'''SELECT listing.title, listing.listing_id, orders.total_cost,
orders.time_placed, orders.quantity, listing.photo, listing.unit_type FROM orders
INNER JOIN "user" on orders.buyer_id = "user".user_id
INNER JOIN listing on orders.listing_id = listing.listing_id
WHERE buyer_id = %(user_id)s
ORDER BY orders.time_placed DESC;'''
g.cursor.execute(query, {'user_id': user_id})
return g.cursor.fetchall()