-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
296 lines (236 loc) · 9.95 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
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
import psycopg2 as dbapi2
import os
from item import Item
from user import User
from flask import current_app
from datetime import date
dsn = """user='postgres' password='docker' host='localhost' port=5432 dbname='postgres'"""
#dsn = os.getenv('DATABASE_URL')
#import models
class Database:
def __init__(self):
self.categories = {}
### USER QUERIES
#okey
def read_user(self, id):
#retrive user from id
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
query = "SELECT * FROM users WHERE (id = %(id)s)"
cursor.execute(query, {'id' : id})
user = cursor.fetchone()
temp = User(username=user[1], password=user[2], email=user[3], phone=user[4], profile_pic=user[5])
temp.profile_pic = user[5]
temp.key = user[0]
return temp
#okey
def get_user_id(self,username):
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
query = "SELECT * FROM users WHERE (username = %(username)s)"
cursor.execute(query, {'username' : username})
user = cursor.fetchone()
if user is not None:
return user[0]
else:
pass
#okey
def add_user(self,user):
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
query = "INSERT INTO users (username, password, email, phone_number, profile_pic ) VALUES (%s, %s, %s, %s, %s)"
#takes "" for the phone number should change
cursor.execute(query, (user.username, user.password, user.email, "", "default.jpg",))
connection.commit()
##gerek var mı bilmiyorum
def is_email_taken(self,email):
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
query = "SELECT * FROM users WHERE (email = %(email)s)"
cursor.execute(query, {'email' : email})
user = cursor.fetchone()
return user
def update_user(self,user, id):
#takes userr object as param
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
query ="""UPDATE users SET
username = %s,
password = %s,
email = %s,
phone_number = %s,
profile_pic = %s
WHERE (id = %s)"""
cursor.execute(query, (user.username, user.password, user.email, user.phone, user.profile_pic, id))
#user.key calısmayabilir
connection.commit()
def delete_user(self,username):
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
query = "DELETE FROM users WHERE (username = %(username)s)"
cursor.execute(query, {'username' : username})
connection.commit()
################category
def add_category(self,category_name):
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
query = "INSERT INTO categories category_name VALUES (%s)"
cursor.execute(query, {'category_name' : category_name})
connection.commit()
def get_category_names(self):
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
query = "select category_name from categories"
cursor.execute(query)
rows = cursor.fetchall()
return rows
def get_category(self,id):
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
query = "SELECT * FROM users WHERE (id = %(id)s)"
query = "select category_name from categories where (id = %(id)s)"
cursor.execute(query, {'id' : id})
rows = cursor.fetchone()
return rows
def get_category_id(self,category_name):
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
query = "SELECT * FROM categories WHERE (category_name = %(category_name)s)"
cursor.execute(query, {'category_name' : category_name})
id = cursor.fetchone()[0]
return id
###############item
#buna gerek kalmadı
def add_item(self,item):
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
query = "INSERT INTO items (category_id,name,description) VALUES (%s, %s, %s)"
db = current_app.config["db"]
cat_id = db.get_category_id(item.category) ####im not sure
cursor.execute(query, (cat_id, item.title, item.description,))
connection.commit()
def add_item_image(self,item,file_name):
####let us consider only jpg files available
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
query = "INSERT INTO items (category_id,name,description,image,color,situation) VALUES (%s, %s, %s, %s, %s, %s)"
db = current_app.config["db"]
cat_id = db.get_category_id(item.category)
data = (cat_id, item.title, item.description, file_name, item.color, item.situation)
cursor.execute(query, data )
connection.commit()
def get_item_id(self,name):
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
query = "SELECT * FROM items WHERE (name = %(name)s)"
cursor.execute(query, {'name' : name})
id = cursor.fetchone()[0]
return id
def get_item_info(self,id):
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
query = "SELECT * FROM items WHERE (id = %(id)s)"
cursor.execute(query, {'id' : id})
info = cursor.fetchone()
return info
def update_item(self, id, name, description, category_id, color, situation, file_name):
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
query ="""UPDATE items SET
name = %s,
description = %s,
category_id = %s,
image = %s,
color = %s,
situation = %s
WHERE (id = %s)"""
cursor.execute(query, (name, description,category_id, file_name, color, situation, id))
connection.commit()
def get_items_from_category_name(self, category_name):
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
db = current_app.config["db"]
category_id = db.get_category_id(category_name)
query = "SELECT * FROM items WHERE (category_id = %(category_id)s)"
cursor.execute(query, {'category_id' : category_id})
rows = cursor.fetchall()
return rows
def delete_item(self, id):
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
query = "DELETE FROM items WHERE (id = %(id)s)"
cursor.execute(query, {'id' : id})
connection.commit()
###############posts
def create_post(self, username, item_name, tag1, tag2):
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
today = date.today()
today = today.strftime("%m/%d/%Y")
db = current_app.config["db"]
user_id = db.get_user_id(username)
item_id = db.get_item_id(item_name)
if tag1 is None and tag2 is None:
query = "INSERT INTO posts (user_id, item_id, post_date) VALUES (%s, %s, %s)"
cursor.execute(query, (user_id, item_id, today,))
connection.commit()
if tag1 is not None and tag2 is None:
query = "INSERT INTO posts (user_id, item_id, post_date, tag1) VALUES (%s, %s, %s, %s)"
cursor.execute(query, (user_id, item_id, today, tag1))
connection.commit()
if tag2 is not None and tag1 is None:
query = "INSERT INTO posts (user_id, item_id, post_date, tag1) VALUES (%s, %s, %s, %s)"
cursor.execute(query, (user_id, item_id, today, tag2))
connection.commit()
else:
query = "INSERT INTO posts (user_id, item_id, post_date, tag1, tag2) VALUES (%s, %s, %s, %s, %s)"
cursor.execute(query, (user_id, item_id, today, tag1, tag2,))
connection.commit()
def get_all_posts(self):
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
query = "SELECT * FROM posts"
cursor.execute(query)
rows = cursor.fetchall()
return rows
def get_post(self,id):
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
query = "SELECT * FROM posts WHERE (id = %(id)s)"
cursor.execute(query, {'id' : id})
post = cursor.fetchone()
return post
def bring_users_post(self, username):
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
db = current_app.config["db"]
user_id = db.get_user_id(username)
query = "SELECT * FROM posts WHERE (user_id = %(user_id)s)"
cursor.execute(query, {'user_id' : user_id})
rows = cursor.fetchall()
return rows
def bring_items_post(self, item_id):
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
query = "SELECT * FROM posts WHERE (item_id = %(item_id)s)"
cursor.execute(query, {'item_id' : item_id})
rows = cursor.fetchone()
return rows
def delete_post(self, id):
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
query = "DELETE FROM posts WHERE (id = %(id)s)"
cursor.execute(query, {'id' : id})
connection.commit()
def update_post(self,id,is_available,is_sold,tag1,tag2):
connection = dbapi2.connect(dsn)
cursor = connection.cursor()
query ="""UPDATE posts SET
is_active = %s,
is_sold = %s,
tag1 = %s,
tag2 = %s
WHERE (id = %s)"""
cursor.execute(query, (is_available, is_sold, tag1, tag2, id,))
connection.commit()
#dsn = """user='postgres' password='docker' host='localhost' port=5432 dbname='postgres'"""