-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
56 lines (44 loc) · 1.4 KB
/
app.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
import logging
from flask import Flask, jsonify, request
from flask_pymongo import PyMongo
app = Flask(__name__)
# app.config['MONGO_DBNAME'] = 'restdb'
app.config['MONGO_URI'] = 'mongodb://localhost:27017/flask_mongo'
mongo = PyMongo(app)
# 테이블은 아래에 insert 때 생성됨
restapi = mongo.db.stars
star_data = {
"name": "test10",
"distance": "1234"
}
# restapi.insert_one(star_data)
@app.route('/star', methods=['GET'])
def get_all_stars():
# 테이블을 가져온다.
star = mongo.db.stars
output = []
for s in star.find():
output.append({'name': s['name'], 'distance': s['distance']})
return jsonify({'result': output})
@app.route('/star/', methods=['GET'])
def get_one_star(name):
print('Come on')
star = mongo.db.stars
s = star.find_one({'name': name})
print('dddd')
if s:
output = {'name': s['name'], 'distance': s['distance']}
else:
output = "No such name"
return jsonify({'result': output})
@app.route('/star', methods=['POST'])
def add_star():
star = mongo.db.stars
name = request.json['name']
distance = request.json['distance']
star_id = star.insert({'name': name, 'distance': distance})
new_star = star.find_one({'_id': star_id})
output = {'name': new_star['name'], 'distance': new_star['distance']}
return jsonify({'result': output})
if __name__ == '__main__':
app.run(debug=True)