-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapp.py
50 lines (40 loc) · 1.36 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
from flask import Flask, jsonify, request
from flask_sqlalchemy import SQLAlchemy
import os
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///" + \
os.path.join(basedir, 'DB.db')
app.config['SQLALCHEMY_ DATABASE_URI'] = False
db = SQLAlchemy(app)
class Confessions(db.Model):
"""
Takes two parameters:
@name: String
@message: String
"""
__tablename__ = 'confessions'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(32), index=True)
message = db.Column(db.String(256))
@app.route('/api/get', methods=['GET'])
def get_confession():
messages = Confessions.query.all()
messages_tuple = tuple(map(lambda confession: {
'name': confession.name,
'title': confession.message
}, messages))
return jsonify(messages_tuple), 200
@app.route('/api/post', methods=['POST'])
def post_confession():
name = request.json.get('name')
message = request.json.get('message')
if name is None or message is None:
return jsonify({"status": "Name or message cannot be blank"}), 400
confess = Confessions(name=name, message=message)
db.session.add(confess)
db.session.commit()
return jsonify({"status": "Confession added successfully!"}), 201
if __name__ == '__main__':
db.create_all()
app.run()