-
Notifications
You must be signed in to change notification settings - Fork 5
/
database.py
233 lines (191 loc) · 7.08 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
import json
import pyrebase
from datetime import datetime
class DBhandler:
def __init__(self):
with open('./authentication/firebase_auth.json') as f:
config = json.load(f)
firebase = pyrebase.initialize_app(config)
self.db = firebase.database()
# User
def insert_user(self, data, password):
phone = data.get('phone', None)
user_info = {
"id": data['id'],
"password": password,
"email": data['email'],
"phone": phone # "phone" 키가 없으면 None으로 설정
}
if self.user_duplicate_check(str(data['id'])):
self.db.child("user").push(user_info)
print(data)
return True
else:
return False
def user_duplicate_check(self, id_string):
users = self.db.child("user").get()
print("users###", users.val())
if str(users.val()) == "None": # first registration
return True
else:
for res in users.each():
value = res.val()
if value['id'] == id_string:
return False
return True
def find_user(self, id_, pw_):
users = self.db.child("user").get()
target_value=[]
for res in users.each():
value = res.val()
if value['id'] == id_ and value['password'] == pw_:
return True
return False
def get_user_info(self, id_):
users = self.db.child("user").get()
for res in users.each():
value = res.val()
if value['id'] == id_:
user_info = {
"id": value['id'],
"email": value['email'],
"phone": value['phone']
}
print(user_info)
return user_info
return False
# Item
def insert_item(self, name, data):
item_info ={
"sellerId": data['seller-id'],
"productName": data['product-name'],
"productPrice": data['product-price'],
"product-status": data['product-status'],
"description": data['product-description'],
"product-image": data['img_path'],
"location": data['location'],
"timestamp": datetime.now().timestamp() # 등록 시간을 timestamp로 추가
}
self.db.child("item").child(name).set(item_info)
print(data)
return True
def get_all_items(self):
items = self.db.child("item").get()
result = []
if items.val():
for item in items.each():
if 'timestamp' in item.val():
timestamp = item.val()['timestamp']
dt = datetime.fromtimestamp(timestamp)
item.val()['datetime'] = dt.strftime("%Y-%m-%d %H:%M:%S")
result.append(item.val())
sorted_result = sorted(result, key=lambda x: x['datetime'], reverse=True)
return sorted_result
def get_item_byname(self, name):
items = self.db.child("item").get()
target_value=""
print("###########",name)
for res in items.each():
key_value = res.key()
if key_value == name:
target_value=res.val()
return target_value
def get_heart_byname(self, uid, name):
hearts = self.db.child("heart").child(uid).get()
target_value=""
if hearts.val() == None:
return target_value
for res in hearts.each():
key_value = res.key()
if key_value == name:
target_value=res.val()
return target_value
def update_heart(self, user_id, isHeart, item):
heart_info ={
"interested": isHeart
}
self.db.child("heart").child(user_id).child(item).set(heart_info)
return True
def buy_item(self, user_id, item):
purchase_info = {
"sold": "Y"
}
self.db.child("purchase").child(user_id).child(item).set(purchase_info)
# Review
def reg_review(self, data):
review_info = {
"productName": data['productName'],
"title": data['title'],
"point": data['point'],
"content": data['content'],
"img_path": data['img_path'],
"authorId": data['authorId']
}
self.db.child("review").child(data['productName']).set(review_info)
return True
def get_all_reviews(self ):
reviews = self.db.child("review").get().val()
return reviews
def get_review_byname(self, name):
reviews = self.db.child("review").get()
target_value=""
print("###########",name)
for res in reviews.each():
key_value = res.key()
if key_value == name:
target_value=res.val()
break
return target_value
# 등록내역 조회
def get_users_registered_item(self, seller):
items = self.db.child("item").get()
result = []
length = 0
print("###########", seller)
for res in items.each():
item = res.val()
if item['sellerId'] == seller:
result.append(item)
length += 1
if length >= 2:
break
return result
# 회원탈퇴
def withdraw_user(self, id_):
users = self.db.child("user").get()
for res in users.each():
key = res.key()
value = res.val()
if value['id'] == id_:
#print("user:", value)
self.db.child("user").child(key).remove()
return True
# heart
def get_top_2_hearts_byname(self, uid):
hearts = self.db.child("heart").child(uid).get()
target_product = []
if hearts.val() is None:
return target_product
# Create a list of tuples containing key-value pairs
heart_list = [(res.key(), res.val()) for res in hearts.each()]
# Sort the list based on the values in descending order
top_2_hearts = heart_list[:2]
print("########### target_heart : ", top_2_hearts)
for name, _ in top_2_hearts:
# Assuming that the name is the key for the product in "products" node
product_info = self.db.child("item").child(name).get()
if product_info.val() is not None:
target_product.append(product_info.val())
print("####### target product : ", target_product)
return target_product
# 구매 내역
def get_users_purchase(self, buyer):
result = []
purchases = self.db.child("purchase").child(buyer).get()
purchase_list = [res.key() for res in purchases.each()]
top_2_purchases = purchase_list[:2]
for name in top_2_purchases:
item = self.db.child("item").child(name).get()
if item.val() is not None:
result.append(item.val())
return result