-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
109 lines (92 loc) · 3.13 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
from flask import Flask, jsonify, request
from flask_cors import CORS
from flask_mysqldb import MySQL
app = Flask(__name__)
#db = yaml.load(open('database.yaml'))
app.config['MYSQL_HOST'] = 'us-cdbr-east-02.cleardb.com'
app.config['MYSQL_USER'] = 'bf04dd86d27fef'
app.config['MYSQL_PASSWORD'] = '22494d04'
app.config['MYSQL_DB'] = 'heroku_b9505fd00ad0726'
mysql = MySQL(app)
CORS(app)
@app.route('/')
def index():
return jsonify({
'status': 'Welcome to BlogsBook!',
})
@app.route('/blogs', methods=['POST', 'GET'])
def blogs():
# POST a blog to database
if request.method == 'POST':
body = request.json
#blog = body['blog']
cursor = mysql.connection.cursor()
print(body['title'])
cursor.execute('INSERT INTO blogs (title,publisher,text) VALUES(%s,%s,%s)', (body['title'],body['publisher'],body['text']))
mysql.connection.commit()
cursor.close()
return jsonify({
'status': 'Blog is posted to MySQL!',
'blog': 'done',
})
# GET all blog from database
if request.method == 'GET':
cursor = mysql.connection.cursor()
cursor.execute('SELECT * FROM blogs')
blogs = cursor.fetchall()
allBlog = []
for i in range(len(blogs)):
id = blogs[i][0]
title = blogs[i][1]
text = blogs[i][2]
publisher = blogs[i][3]
likes = blogs[i][4]
dislikes = blogs[i][5]
dataDict = {
"id": id,
"title": title,
"text": text,
"publisher": publisher,
"likes": likes,
"dislikes": dislikes
}
allBlog.append(dataDict)
return jsonify(allBlog)
@app.route('/like/<string:id>', methods=['GET'])
def like(id):
body = request.json
cursor = mysql.connection.cursor()
query = "UPDATE blogs SET likes=likes+1 WHERE id={id}".format(id=id)
cursor.execute(query)
mysql.connection.commit()
cursor.close()
return jsonify({'status': 'Blog is liked!'})
@app.route('/dislike/<string:id>', methods=['GET'])
def dislike(id):
body = request.json
cursor = mysql.connection.cursor()
query = "UPDATE blogs SET dislikes=dislikes+1 WHERE id={id}".format(id=id)
cursor.execute(query)
mysql.connection.commit()
cursor.close()
return jsonify({'status': 'Blog is disliked!'})
@app.route('/stats/<string:id>', methods=['GET'])
def stats(id):
body = request.json
cursor = mysql.connection.cursor()
query = "SELECT id, likes, dislikes FROM blogs WHERE id = {id}".format(id=id)
cursor.execute(query)
blogs = cursor.fetchall()
print(blogs)
id = blogs[0][0]
like = blogs[0][1]
dislike = blogs[0][2]
dataDict = {
"id": id,
"like": like,
"dislike": dislike,
}
cursor.close()
return jsonify(dataDict)
if __name__ == '__main__':
app.run(debug = True)