-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.py
More file actions
198 lines (145 loc) · 5 KB
/
api.py
File metadata and controls
198 lines (145 loc) · 5 KB
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
import flask
from flask import Flask, jsonify, request
from flask_restful import reqparse
from datetime import datetime
import flask_restful
import pymysql
import json
import uuid
app = Flask(__name__)
api = flask_restful.Api(app)
config = {
'host': '172.20.0.5',
'port': 3306,
'user': 'root',
'database': 'mydb'
}
# 카테고리 확인하기
@app.route('/category-ms')
def index_1():
return "Get Category Data!"
@app.route('/festival-ms')
def index_2():
return "Get Festival Data!"
@app.route('/festival2-ms')
def index_3():
return "Show Earnings!"
@app.route('/seller-ms')
def index_4():
return "Get seller's Information!"
@app.route('/storelist-ms')
def index_5():
return "Get Store list!"
@app.route('/storemenulist-ms')
def index_6():
return "What do they sell?"
class Category(flask_restful.Resource):
def __init__(self):
self.conn = pymysql.connect(**config)
self.cursor = self.conn.cursor()
def get(self):
sql ="select category, count(category) as '가게 수' \
from store group by category"
self.cursor.execute(sql)
# data_list = self.cursor.fetchone()
data_list = self.cursor.fetchall()
row_headers = [x[0] for x in self.cursor.description]
json_data = []
for result in data_list:
json_data.append(dict(zip(row_headers, result)))
return jsonify(json_data)
# 페스티벌 조회하기
class Festival(flask_restful.Resource):
def __init__(self):
self.conn = pymysql.connect(**config)
self.cursor = self.conn.cursor()
def get(self):
sql ="SELECT * from festival"
self.cursor.execute(sql)
data_list = self.cursor.fetchall()
row_headers = [x[0] for x in self.cursor.description]
json_data = []
for result in data_list:
json_data.append(dict(zip(row_headers, result)))
return jsonify(json_data)
# 페스티벌 실적 조회
class Festival2(flask_restful.Resource):
def __init__(self):
self.conn = pymysql.connect(**config)
self.cursor = self.conn.cursor()
def get(self):
db = pymysql.connect(**config)
cur = db.cursor()
sql = "SELECT A.order_id,A.total_price,B.festival_id,B.festival_name from orders A INNER JOIN festival B ON A.user_no=B.user_no"
cur.execute(sql)
data_list = cur.fetchall()
return jsonify(data_list)
# 판매자 정보 조회
class Seller(flask_restful.Resource):
def __init__(self):
self.conn = pymysql.connect(**config)
self.cursor = self.conn.cursor()
def get(self):
db = pymysql.connect(**config)
cur = db.cursor()
sql = '''SELECT A.menu_name, B.total_price ,B.total_qty \
from menu A INNER JOIN orders B ON A.store_id=B.store_id'''
cur.execute(sql)
data_list_two = ()
data_list_two += cur.fetchall() #1~4
price = {}
qty = {}
menu_list = []
for i in data_list_two:
if (price.get(i[0]) == None):
price[i[0]] = i[1]
menu_list.append(i[0])
else:
price[i[0]] += i[1]
for i in data_list_two:
if (qty.get(i[0]) == None):
qty[i[0]] = i[2]
else:
qty[i[0]] += i[2]
data_list = []
for i in menu_list:
data_list.append([i, price[i], qty[i]])
return jsonify(data_list)
# 가게 리스트 조회
class Storelist(flask_restful.Resource):
def __init__(self):
self.conn = pymysql.connect(**config)
self.cursor = self.conn.cursor()
def get(self, f_id):
sql ="select store_name, store_id, store_description, location_number \
from store where festival_id=%s"
self.cursor.execute(sql, [f_id])
data_list = self.cursor.fetchall()
row_headers = [x[0] for x in self.cursor.description]
json_data = []
for result in data_list:
json_data.append(dict(zip(row_headers, result)))
return jsonify(json_data)
# 가게별 판매목록 조회
class Storemenulist(flask_restful.Resource):
def __init__(self):
self.conn = pymysql.connect(**config)
self.cursor = self.conn.cursor()
def get(self, id):
db = pymysql.connect(**config)
cur = db.cursor()
sql = '''select menu_id, menu_name, menu_price from menu where store_id=%s'''
cur.execute(sql, id)
data_list = cur.fetchall()
a = list(data_list)
a.insert(len(a), id)
a = tuple(a)
return jsonify(a)
api.add_resource(Storemenulist, '/storemenulist-ms/storemenulist/<int:id>')
api.add_resource(Storelist, '/storelist-ms/storelist/<int:f_id>')
api.add_resource(Seller, '/seller-ms/seller')
api.add_resource(Festival2, '/festival2-ms/festival2')
api.add_resource(Festival, '/festival-ms/festival')
api.add_resource(Category, '/category-ms/category')
if __name__ == '__main__':
app.run(port=7000)