-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.py
136 lines (104 loc) · 3.91 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Base, Book
# Connect to Database and create database session
engine = create_engine('sqlite:///books-collection.db?check_same_thread=False')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
# landing page that will display all the books in our database
# This function will operate on the Read operation.
@app.route('/')
@app.route('/books')
def showBooks():
books = session.query(Book).all()
return render_template('books.html', books=books)
# This will let us Create a new book and save it in our database
@app.route('/books/new/', methods=['GET', 'POST'])
def newBook():
if request.method == 'POST':
newBook = Book(title=request.form['name'],
author=request.form['author'],
genre=request.form['genre'])
session.add(newBook)
session.commit()
return redirect(url_for('showBooks'))
else:
return render_template('newBook.html')
# This will let us Update our books and save it in our database
@app.route("/books/<int:book_id>/edit/", methods=['GET', 'POST'])
def editBook(book_id):
editedBook = session.query(Book).filter_by(id=book_id).one()
if request.method == 'POST':
if request.form['name']:
editedBook.title = request.form['name']
return redirect(url_for('showBooks'))
else:
return render_template('editBook.html', book=editedBook)
# This will let us Delete our book
@app.route('/books/<int:book_id>/delete/', methods=['GET', 'POST'])
def deleteBook(book_id):
bookToDelete = session.query(Book).filter_by(id=book_id).one()
if request.method == 'POST':
session.delete(bookToDelete)
session.commit()
return redirect(url_for('showBooks', book_id=book_id))
else:
return render_template('deleteBook.html', book=bookToDelete)
"""
api functions
"""
from flask import jsonify
def get_books():
books = session.query(Book).all()
return jsonify(books=[b.serialize for b in books])
def get_book(book_id):
books = session.query(Book).filter_by(id=book_id).one()
return jsonify(books=books.serialize)
def makeANewBook(title, author, genre):
addedbook = Book(title=title, author=author, genre=genre)
session.add(addedbook)
session.commit()
return jsonify(Book=addedbook.serialize)
def updateBook(id, title, author, genre):
updatedBook = session.query(Book).filter_by(id=id).one()
if not title:
updatedBook.title = title
if not author:
updatedBook.author = author
if not genre:
updatedBook.genre = genre
session.add(updatedBook)
session.commit()
return 'Updated a Book with id %s' % id
def deleteABook(id):
bookToDelete = session.query(Book).filter_by(id=id).one()
session.delete(bookToDelete)
session.commit()
return 'Removed Book with id %s' % id
@app.route('/')
@app.route('/booksApi', methods=['GET', 'POST'])
def booksFunction():
if request.method == 'GET':
return get_books()
elif request.method == 'POST':
title = request.args.get('title', '')
author = request.args.get('author', '')
genre = request.args.get('genre', '')
return makeANewBook(title, author, genre)
@app.route('/booksApi/<int:id>', methods=['GET', 'PUT', 'DELETE'])
def bookFunctionId(id):
if request.method == 'GET':
return get_book(id)
elif request.method == 'PUT':
title = request.args.get('title', '')
author = request.args.get('author', '')
genre = request.args.get('genre', '')
return updateBook(id, title, author, genre)
elif request.method == 'DELETE':
return deleteABook(id)
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=4996)