def api(isbn): """ Give all the details about the book""" if request.method == "GET": res = db.execute( "SELECT title, author, year, isbn, reviews_count, average_rating FROM books WHERE isbn = :isbn", { "isbn": isbn }).fetchone() book = Book() if res is None: return render_template("error.html", message="404 book not found"), 404 book.title, book.author, book.year, book.isbn, book.reviews_count, book.average_rating = res if res.reviews_count == 0 or res.average_rating == 0: book_aux = api_intern(isbn) book.average_rating = book_aux["books"][0]["average_rating"] book.reviews_count = book_aux["books"][0]["reviews_count"] response = { "title": book.title, "author": book.author, "year": book.year, "isbn": book.isbn, "review_count": book.reviews_count, "average_score": book.average_rating } return jsonify(response)
def details(isbn): if session["user"] is None: return login() """ Give all the details about the book""" book = Book() book.isbn, book.title, book.author, book.year, book.reviews_count, book.average_rating = db.execute( "SELECT isbn, title, author, year, reviews_count, average_rating FROM books WHERE isbn = :isbn", { "isbn": isbn }).fetchone() if book.average_rating == 0 or book.reviews_count == 0: book_aux = api_intern(isbn) book.average_rating = book_aux["books"][0]["average_rating"] book.reviews_count = book_aux["books"][0]["reviews_count"] db.execute( "UPDATE books SET average_rating = :average_rating, reviews_count = :reviews_count WHERE isbn=:isbn", { "isbn": isbn, "average_rating": float(book.average_rating), "reviews_count": int(book.reviews_count) }) db.commit() if request.method == "GET": return render_template("details.html", book=book) else: return "POST DETAILS"