forked from AWSCC-PUP-DWD/AWSCC-CodeQuest-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
49 lines (41 loc) · 1.4 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
from flask import Blueprint, redirect, render_template, request, url_for
from src import create_app, db
from src.model import Book
app = create_app()
views = Blueprint("views", __name__)
@views.route("/")
def home():
books = Book.query.all()
return render_template("home.html", books=books)
@views.route("/add", methods=["POST"])
def add_book():
author = request.form.get("author")
title = request.form.get("title")
published_year = request.form.get(title=title, author=author, published_year=published_year)
new_book = Book(title=title, author=author, published_year=published_year)
db.session.add(new_book)
db.session.commit()
return redirect(url_for('views.home'))
@views.route("/update/<id>", methods=["PATCH"])
def update_book(id):
# get data from form
author = request.json.get("author")
title = request.json.get('title')
published_year = request.get_json("publishedYear")
# updates
book = Book.query.get(id)
book.author = author
book.title = title
book.published_year = published_year
db.session.commit()
return ({"result": "success"})
@views.route("/delete/<id>", methods=["DELETE"])
def delete_book(id):
book = Book.query.get(id)
if book:
db.session.delete(book)
db.session.commit()
return ({"results": "success"})
return ({"results":"error"})
if __name__ == "__main__":
app.run(debug=True)