def update_book(isbn): request_data = request.get_json() if 'name' in request_data: Book.update_book_name(isbn, request_data['name']) if 'price' in request_data: Book.update_book_price(isbn, request_data['price']) return Response(status=204)
def add_book(): request_data = request.get_json() if (validBookObject(request_data)): try: Book.add_book(request_data['name'], request_data['price'], request_data['isbn']) except: print('Unexpected error: ', sys.exc_info()[0]) print('more error', sys.exc_info()[1]) response = Response("", status=201, mimetype='application/json') response.headers['Location'] = "/books/" + str(request_data['isbn']) return response else: invalidBookObjectErrorMsg = { "error": "Invalid book object passed in request", "helpString": "Data passed in similar to this {'name': 'bookname', 'price': 7.99, 'isbn': 792321354533 }" } response = Response(json.dumps(invalidBookObjectErrorMsg), status=400, mimetype='application/json') return response
def add_book(): new_book = { "isbn": request.form['isbn'], "title": request.form['title'], "author": request.form['author'], "quote": request.form['quote'] } if (validBookObject(new_book)): Book.add_book(new_book['title'], new_book['author'], new_book['isbn'], new_book['quote']) # Response constructor bookInsertedMsg = "New book successfully inserted as follow: {0}".format( new_book) response = Response(json.dumps(bookInsertedMsg), status=200, mimetype='application/json') return response else: invalidBookObjectErrorMsg = { "error": "Invalid book object", "help": "{'isbn': 1, title': '2', 'author': '3', 'quote': '4'}" } response = Response(invalidBookObjectErrorMsg, status=400, mimetype='application/json') return response
def replace_book(): changed_book = { "isbn": request.form['isbn'], "title": request.form['title'], "author": request.form['author'], "quote": request.form['quote'] } if (not validBookObject(changed_book)): invalidBookObjectErrorMsg = { "error": "Invalid book object", "help": "{'title': '1', 'author': '2', 'quote': '3'}" } response = Response(json.dumps(invalidBookObjectErrorMsg), status=400, mimetype='application/json') return response Book.replace_book(oldIsbn, changed_book["isbn"], changed_book["title"], changed_book["author"], changed_book["quote"]) response = Response("Book changed!", status=204) replacementCompletedMsg = { 'success': 'Book replaced successfully:{0}'.format(changed_book) } response = Response(json.dumps(replacementCompletedMsg), status=200, mimetype='application/json') return response
def patch_book(isbn): request_data = request.get_json() if 'name' in request_data: Book.update_name(isbn, request_data['name']) if 'price' in request_data: Book.update_price(isbn, request_data['price']) response = Response("", status=204) response.headers['Location'] = 'books' + str(isbn) return response
def replace_book(isbn): request_data = request.get_json() if valid_book_object_put(request_data): Book.replace_book(isbn, request_data['name'], request_data['price']) return Response(status=204) else: invalid_error_msg = {'error': "Invalid book passed in request"} return Response(response=json.dumps(invalid_error_msg), status=400, mimetype='application/json')
def add_book(): request_data = request.get_json() if validate_book(request_data): Book.add_book(request_data['isbn'], request_data['name'], request_data['price']) response = Response("", 201, mimetype='application/json') response.headers['Location'] = '/books/' + str(request_data['isbn']) return response else: return Response('Invalid book object. Must include "name", "price", and "isbn"', status=400, mimetype='application/json')
def update_book(isbn): request_data = request.get_json() if ("name" in request_data): Book.update_book_name(isbn, request_data['name']) if ("price" in request_data): Book.update_book_price(isbn, request_data['price']) response = Response("", status=204) response.headers['Location'] = '/books' + str(isbn) return response
def update_book(isbn): request_data = request.get_json() # Do the updates if "name" in request_data: Book.update_book_name(isbn, request_data['name']) if "price" in request_data: Book.update_book_price(isbn, request_data['price']) response = Response("", status=204) response.headers['Location'] = "/books/" + str(isbn) return response
def add_book(): request_data = request.get_json() if validBookObject(request_data): Book.add_book(request_data['name'], request_data['price'], request_data['isbn'], ) response = Response('', 201, mimetype='application/json') response.headers['Location'] = '/books/' + str(request_data['isbn']) return response else: invalidBookObjectErrorMsg = { "error": "Invalid book object passed in request", "helpString": "Data passed in similar to this { 'name' : 'bookName' , 'price' : 2.99, 'isbn' : 1234232435 }" } response = Response(json.dumps(invalidBookObjectErrorMsg), status=400, mimetype='application/json') return response
def add_book(): request_data = request.get_json() if valid_book_object_post(request_data): Book.add_book(request_data['name'], request_data['price'], request_data['isbn']) response = Response(status=201, mimetype='application/json') response.headers['Location'] = "/books/" + str(request_data['isbn']) return response else: invalid_error_msg = {'error': "Invalid book passed in request"} response = Response(response=json.dumps(invalid_error_msg), status=400, mimetype='application/json') return response
def replace_book(isbn): request_data = request.get_json() if not valid_put_request_data(request_data): invalidBookObjectErrorMsg = { "error": "Invalid book object passed in request", "helpString": "Data passed in similar to this { 'name' : 'bookName' , 'price' : 2.99 }" } response = Response(json.dumps(invalidBookObjectErrorMsg), status=400, mimetype='application/json') return response Book.replace_book(isbn, request_data['name'], request_data['price']) response = Response('', 201, mimetype='application/json') response.headers['Location'] = '/books/' + str(isbn) return response
def add_book_form(): # this block is only entered when the form is submitted if request.method == 'POST': isbn = request.form.get('isbn') name = request.form['name'] price = request.form['price'] Book.add_book(isbn, name, price) return '''<h1>You added a new book!</h1> <div>{}, {}, ${}</div> <a href="/">View</a>'''.format(isbn, name, price) return '''<form method="POST">
def delete_book(isbn): if Book.delete_book(isbn): response = Response('', status=204) return response else: response = Response('ISBN not found. Unable to delete.', status=404, mimetype='application/json') return response
def update_book(isbn): request_data = request.get_json() if not valid_patch_request_data(request_data): invalidBookObjectErrorMsg = { "error": "Invalid book object passed in request" } response = Response(json.dumps(invalidBookObjectErrorMsg), status=400, mimetype='application/json') return response if 'name' in request_data: Book.update_book_name(isbn, request_data['name']) if 'price' in request_data: Book.update_book_price(isbn, request_data['price']) response = Response('', 201, mimetype='application/json') response.headers['Location'] = '/books/' + str(isbn) return response
def replace_book(isbn): request_data = request.get_json() if (not valid_put_request_data(request_data)): invalidBookObjectErrorMsg = { "error": "Invalid book object passed in request", "helpString": "Data passed in similar to this {'name': 'bookname', 'price': 7.99 }" } response = Response(json.dumps(invalidBookObjectErrorMsg), status=400, mimetype='application/json') return response Book.replace_book(isbn, request_data['name'], request_data['price']) response = Response("", status=204, mimetype='application/json') return response
def delete_book(isbn): if Book.delete_book(isbn): response = Response("", 204) return response errorMsg = {"error": "Book with ISBN " + str(isbn) + " not found."} response = Response(json.dumps(errorMsg), 404, mimetype='application/json') return response
def get_paginated_books(page_number): print(type(request.args.get('limit'))) LIMIT = request.args.get('limit', DEFAULT_PAGE_LIMIT, int) start_index = page_number * LIMIT end_index = page_number * LIMIT print(start_index) print(end_index) return jsonify({'books': Book.get_all_books()[start_index:end_index]})
def replace_book(isbn): request_data = request.get_json() if (not (validBookObject(request_data))): invalidBookObjectErrorMsg = { "error": "Valid book object passed must be passed in the request", "helpString": "Data passed in should look similar to this {'name':'bookname','price':1.01}" } response = Response(json.dumps(invalidBookObjectErrorMsg), status=400, mimetype='application/json') return response Book.replace_book(isbn, request_data['name'], request_data['price']) response = Response("", status=204) return response
def add_book(): request_data = request.get_json() if (valid_post_request_data(request_data)): Book.add_book(request_data['name'], request_data['price'], request_data['isbn']) response = Response("", 201, mimetype='application/json') response.headers['Location'] = "/books/" + str(request_data['isbn']) return response errorMsg = { "error": "Invalid book object passed in request", "helpString": "Data passed in similar to this {'name': 'bookName', 'price': 8.99, 'isbn': 1234567890}" } response = Response(json.dumps(errorMsg), status=400, mimetype='application/json') return response
def delete_book(isbn): if Book.delete_book(isbn): return Response('', status=204) invalidIsbnErrorMsg = { "error": "Book with Isbn number is not found, please try with valid isbn" } response = Response(json.dumps(invalidIsbnErrorMsg), status=404, mimetype='application/json') return response
def addbook(): if request.method == 'POST': book_name = request.form['book_name'] book_description = request.form['book_description'] book_price = request.form['book_price'] book = Book(title=book_name, description=book_description, price=book_price) db.session.add(book) db.session.commit() return render_template('addbook.html')
def replace_book(isbn): request_data = request.get_json() # Invalid object data in request if not valid_put_request_data(request_data): errorMsg = { 'error': 'Valid book object must be passed in the request', 'helpString': 'Data passed in similar to this {"name": "bookName", "price": 7.99}' } response = Response(json.dumps(errorMsg), status=400, mimetype='application/json') return response Book.replace_book(isbn, request_data['name'], request_data['price']) response = Response("", status=204) return response
def delete_book(isbn): if (Book.delete_book(isbn)): response = Response("", status=204) return response invalidBookObjectErrorMsg = { "error": "Book with the ISBN number that was provided was not found, so therefore unable to delete" } return Response(json.dumps(invalidBookObjectErrorMsg), status=404, mimetype='application/json')
def add_book(): request_data = request.get_json() #f # ## (valid_post_request_data(request_data)): if (valid_post_request_data(request_data)): Book.add_book(request_data['name'], request_data['price'], request_data['isbn']) response = Response("", 201, mimetype='application/json') response.headers['Location'] = "/books/" + str(request_data['isbn']) return response errorMsg = { "error": "Invalid book object passed in request", "helpString": "" } # response =Response(json.dumps(errorMsg), status=400, mimetype='ablication/json') response = Response(json.dumps(errorMsg), status=400, mimetype='application/json') return response
def update_book(isbn): request_data = request.get_json() if (not valid_patch_request_data(request_data)): invalidBookObjectErrorMsg = { "error": "Invalid book object passed in request", "helpString": "Data passed in similar to this {'name': 'bookname', 'price': 7.99 }" } response = Response(json.dumps(invalidBookObjectErrorMsg), status=400, mimetype='application/json') return response if ("name" in request_data): Book.update_book_name(isbn, request_data['name']) if ("price" in request_data): Book.update_book_price(isbn, request_data['price']) response = Response("", status=204, mimetype='application/json') response.headers['Location'] = "/books/" + str(isbn) return response
def get_paginated_books(page_number): books = Book.get_all_books() LIMIT = request.args.get('limit', DEFAULT_PAGE_LIMIT, int) startIndex = (page_number - 1) * LIMIT endIndex = len(books) if (LIMIT < endIndex): endIndex = LIMIT * page_number if (endIndex > len(books)): endIndex = len(books) print(startIndex) print(endIndex) return jsonify({'books': books[startIndex:endIndex]})
def delete_book(isbn): if (Book.delete_book(isbn)): deletionCompletedMsg = { 'success': 'Book with ISBN {0} was deleted from collection.'.format(isbn) } response = Response(json.dumps(deletionCompletedMsg), status=200, mimetype='application/json') return response invalidBookObjectErrorMsg = {'error': 'Book with this ISBN was not found.'} response = Response(json.dumps(invalidBookObjectErrorMsg), status=404, mimetype='application/json') return response
def get_book_by_isbn(isbn): return_value = Book.get_book(isbn) return jsonify(return_value)
def books_list(): return jsonify({'books': Book.get_all_books()})