-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
115 lines (80 loc) · 3.43 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
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
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask import Response
import json
app = Flask(__name__)
DATABASE = 'mydb_assignment1'
PASSWORD = 'bhavika'
USER = 'root'
HOSTNAME = 'db'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://%s:%s@%s/%s'%(USER, PASSWORD, HOSTNAME, DATABASE)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)
class mytable(db.Model):
id=db.Column(db.Integer, primary_key=True)
name=db.Column(db.String(200))
email=db.Column(db.String(200))
category=db.Column(db.String(200))
description=db.Column(db.String(200))
link=db.Column(db.String(200))
estimated_costs=db.Column(db.String(200))
submit_date=db.Column(db.String(200))
def __init__(self,name,email,category,description,link,estimated_costs,submit_date):
self.name=name
self.email=email
self.category=category
self.description=description
self.link=link
self.estimated_costs=estimated_costs
self.submit_date=submit_date
class CreateDB():
def __init__(self):
import sqlalchemy
engine = sqlalchemy.create_engine('mysql://%s:%s@%s'%(USER, PASSWORD, HOSTNAME))
engine.execute("CREATE DATABASE IF NOT EXISTS %s "%(DATABASE))
@app.route("/v1/expenses", methods=['POST'])
def index():
CreateDB()
db.create_all()
my_data = str(request.data)
data = json.loads(my_data, 'utf-8')
name = data['name']
email = data['email']
category = data['category']
description = data['description']
link = data['link']
estimated_costs = data['estimated_costs']
submit_date = data['submit_date']
user = mytable(name,email,category,description,link,estimated_costs,submit_date)
db.session.add(user)
db.session.commit()
store_data={'id' : user.id, 'name' : name, 'email' : email, 'category' : category, 'description' : description, 'link' : link, 'estimated_costs' : estimated_costs, 'submit_date' : submit_date, 'status' : 'pending' , 'decision_date' : '' }
js=json.dumps(store_data)
resp=Response(js,status=201, mimetype='application/json')
return resp
@app.route("/v1/expenses/<int:id>", methods=['GET'])
def index1(id):
get_data = mytable.query.filter_by(id=id).first_or_404()
#print(get_data.name)
show_data={'id' : get_data.id, 'name' : get_data.name, 'email' : get_data.email, 'category' : get_data.category, 'description' : get_data.description, 'link' : get_data.link, 'estimated_costs' : get_data.estimated_costs, 'submit_date' : get_data.submit_date, 'status' : 'pending', 'decision_date' : '' }
js1=json.dumps(show_data)
resp1=Response(js1, status=200, mimetype='application/json')
return resp1
@app.route("/v1/expenses/<int:id>", methods=['PUT'])
def index2(id):
updated_cost=str(request.data)
data=json.loads(updated_cost, 'utf-8')
up_cost=data['estimated_costs']
update_data = mytable.query.filter_by(id=id).first()
update_data.estimated_costs=up_cost
db.session.commit()
updated_data={'estimated_costs' : update_data.estimated_costs}
return Response(status=202)
@app.route("/v1/expenses/<int:id>", methods=['DELETE'])
def index3(id):
delete_data=mytable.query.filter_by(id=id).first()
db.session.delete(delete_data)
db.session.commit()
return Response(status=204)
if __name__== "__main__":
app.run(debug=True, host='0.0.0.0',port=5000)