-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
72 lines (61 loc) · 1.79 KB
/
app.py
File metadata and controls
72 lines (61 loc) · 1.79 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
import os
from flask import Flask, request, jsonify, render_template
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object(os.environ['APP_SETTINGS'])
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
from models import Book
@app.route("/")
def hello():
return "hello world"
@app.route("/add")
def add_book():
name = request.args.get('name')
author = request.args.get('author')
published = request.args.get('published')
try:
book = Book(
name = name,
author = author,
published = published
)
db.session.add(book)
db.session.commit()
return "Book added, bookId = {}".format(book.id)
except Exception as e:
return str(e)
@app.route("/add/form",methods=['GET','POST'])
def add_book_form():
if request.method == 'POST':
name = request.form.get('name')
author = request.form.get('author')
published = request.form.get('published')
try:
book = Book(
name = name,
author = author,
published = published
)
db.session.add(book)
db.session.commit()
return "Book added, bookId = {}".format(book.id)
except Exception as e:
return str(e)
return render_template("getdata.html")
@app.route("/getAllBooks")
def get_all_book():
try:
books = Book.query.all()
return jsonify([i.serialize() for i in books])
except Exception as e:
return str(e)
@app.route("/get/<ids>")
def get_by_id():
try:
book.Book.query.filter_by(id = ids).first()
return jsnoify(book.serialize)
except Exception as e:
return str(e)
if __name__ == '__main__':
app.run()