54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
from app import app, db
|
|
from flask import render_template, request, jsonify
|
|
from app.models import Author, Book
|
|
|
|
@app.route("/", methods=["GET"])
|
|
def home():
|
|
books = db.session.query(Book, Author).filter(Book.author_id == Author.author_id).all()
|
|
return render_template("index.html", books=books)
|
|
@app.route("/submit", methods=["POST"])
|
|
|
|
def submit():
|
|
global_book_object = Book()
|
|
|
|
title = request.form["title"]
|
|
author_name = request.form["author"]
|
|
|
|
author_exists = db.session.query(Author).filter(Author.name == author_name).first()
|
|
print(author_exists)
|
|
# check if author already exists in db
|
|
if author_exists:
|
|
author_id = author_exists.author_id
|
|
book = Book(author_id=author_id, title=title)
|
|
db.session.add(book)
|
|
db.session.commit()
|
|
global_book_object = book
|
|
else:
|
|
author = Author(name=author_name)
|
|
db.session.add(author)
|
|
db.session.commit()
|
|
|
|
book = Book(author_id=author.author_id, title=title)
|
|
db.session.add(book)
|
|
db.session.commit()
|
|
global_book_object = book
|
|
|
|
response = f"""
|
|
<tr>
|
|
<td>{title}</td>
|
|
<td>{author_name}</td>
|
|
<td>
|
|
<button class="btn btn-primary"
|
|
hx-get="/get-edit-form/{global_book_object.book_id}">
|
|
Edit Title
|
|
</button>
|
|
</td>
|
|
<td>
|
|
<button hx-delete="/delete/{global_book_object.book_id}"
|
|
class="btn btn-primary">
|
|
Delete
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
"""
|
|
return response |