-
Notifications
You must be signed in to change notification settings - Fork 1
/
furniture.py
178 lines (160 loc) · 5.52 KB
/
furniture.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
import jsonschema
import json
import sqlite3
def create_furniture(furniture, owner):
""" used by user to post a new furniture
:param furniture: a json object representing a furniture
:return: (response body, status code)
"""
# integrity check
schema = {
"type": "object",
"required": ["title", "labels", "image_url", "description"],
"properties": {
"title": {"type": "string"},
"labels": {"type": "string"},
"image_url": {"type": "string"},
"description": {"type": "string"},
}
}
try:
jsonschema.validate(instance=furniture, schema=schema)
except (ValueError, jsonschema.exceptions.ValidationError):
return json.dumps({"error": "input invalid."}), 400
furniture["owner"] = owner
# write into db
conn = None
try:
conn = sqlite3.connect("sqlite_db")
stmt = (
"INSERT INTO Furniture "
"(owner, title, labels, image_url, description) "
"VALUES (?, ?, ?, ?, ?)"
)
conn.execute(
stmt, [furniture[k] for k in ["owner", "title",
"labels", "image_url",
"description"]]
)
conn.commit()
except sqlite3.Error as e:
return json.dumps({"error": f"db error: {str(e)}"}), 500
finally:
if conn:
conn.close()
return json.dumps({"error": ""}), 201
def rate_owner(fid, buyer_email, rating):
if not (0 <= rating <= 5):
return json.dumps(
{"error": "rating score should be in the range of [0, 5]"}), 400
conn = None
try:
conn = sqlite3.connect("sqlite_db")
record = conn.execute(
"SELECT buyer, owner, status FROM Furniture WHERE fid = ?",
[fid]
).fetchone()
if record is None:
return json.dumps({"error": "fid not existed"}), 400
real_buyer_email, owner, status = record
if buyer_email != real_buyer_email:
return json.dumps(
{"error": "rating can only be triggered by the buyer"}), 400
if status == "rated":
return json.dumps(
{"error": "this transaction has been rated"}), 400
if status != "completed":
return json.dumps(
{"error": "please rate after the transaction is completed"}
), 400
conn.execute(
"UPDATE user SET transaction_count = "
"transaction_count+1, rating = rating+?"
"WHERE email = ?",
[rating, owner]
)
conn.execute(
"UPDATE furniture SET status = 'rated' WHERE fid=?",
[fid]
)
conn.commit()
except sqlite3.Error as e:
return json.dumps({"error": f"db error: {str(e)}"}), 500
finally:
if conn:
conn.close()
return json.dumps({"error": ""}), 200
def buy_furniture(fid, buyer_email):
if not fid:
return json.dumps({"error": "invalid input"}), 400
conn = None
try:
conn = sqlite3.connect("sqlite_db")
record = conn.execute(
"SELECT buyer, status FROM Furniture WHERE fid = ?",
[fid]
).fetchone()
if record is None:
return json.dumps({"error": "furniture not existed"}), 400
existed_buyer_email, status = record
if status != "init":
return json.dumps(
{"error": "The item is already sold or in progress"}), 400
conn.execute(
"UPDATE Furniture SET buyer = "
"?, status = ?"
"WHERE fid = ?",
[buyer_email, "pending", fid]
)
conn.commit()
except sqlite3.Error as e:
return json.dumps({"error": f"db error: {str(e)}"}), 500
finally:
if conn:
conn.close()
return json.dumps({"error": ""}), 200
def owner_confirm(fid, curr_user_email, is_confirm):
if not fid:
return json.dumps({"error": "invalid input"}), 400
conn = None
try:
conn = sqlite3.connect("sqlite_db")
record = conn.execute(
"SELECT buyer, owner, status FROM Furniture WHERE fid = ?",
[fid]
).fetchone()
if record is None:
return json.dumps({"error": "fid not existed"}), 400
buyer_email, owner_email, status = record
if curr_user_email != owner_email:
return json.dumps(
{"error": "Only owner can confirm the transaction."}), 400
if status != "pending":
return json.dumps(
{"error": "the owner can only confirm "
"the pending transaction"}), 400
new_status = "completed"
if is_confirm == 'False':
new_status = 'init'
buyer_email = None
conn.execute(
"UPDATE furniture SET buyer =?, status = ? "
"WHERE fid = ?",
[buyer_email, new_status, fid]
)
curr_trans_count = conn.execute(
"SELECT COUNT(*) FROM Furniture "
"WHERE status = 'completed' "
"and owner = ?", [curr_user_email]
).fetchone()
conn.execute(
"UPDATE user SET transaction_count = ? WHERE email = ?",
[curr_trans_count[0], curr_user_email]
)
conn.commit()
except sqlite3.Error as e:
return json.dumps({"error": f"db error: {str(e)}"}), 500
finally:
if conn:
conn.close()
return json.dumps({"error": ""}), 200