def delete_book(isbn): result = Book.delete_book(isbn) if result > 0: response = Response('%s books have been deleted' % result, status=201) else: response = Response("The book with such ISBN wasn't found", status=404) return response
def add_book(): received = request.get_json() if validBookObject(received): new_book = { "name": received['name'], "price": received['price'], "isbn": received['isbn'] } if Book.add_book(**received): response = Response("The book is added", 201, mimetype='application/json') response.headers["Location"] = "books/" + str(new_book["isbn"]) else: response = Response("Something went wrong! Check the logs!", status=400) else: invalidBookObjectErrorMsg = { "error": "Invalid book object passed in request", "helpstring": "Data passed in similar to this {'name': 'bookname', 'price': 7.89, 'isbn': 23424}" } response = Response(json.dumps(invalidBookObjectErrorMsg), status=400, mimetype='application/json') return response
def book(): bookTime = request.form["bookTime"] bookDate = request.form["bookDate"] countPeople = request.form["countPeople"] bookTime = 'NULL' if bookTime == '' else bookTime countPeople = countPeople.split(' ')[0] if countPeople != 'More' else -1 t = bookDate.split('/') y, m, d = t[2], t[0], t[1] bookDate = '{}-{}-{}'.format(y, m, d) # print('-----------In func book------------') # print(bookTime) # print(bookDate) # print(countPeople) book = Book(bookTime, bookDate, int(countPeople)) try: db.session.add(book) db.session.commit() except: return redirect(url_for('index')) return render_template('thank.html'), 200
def update_book(isbn): received = request.get_json() # Validate the requested data received['isbn'] = isbn if Book.edit_book(**received): response = Response("The book has been updated", status=201) else: response = Response("Something went wrong! Check the logs!", status=400) return response
def replace_book(isbn): received = request.get_json() # Validate the requested data received['isbn'] = isbn result = Book.replace_book(**received) if result == 1: response = Response("The book has been replaced", status=201) elif result == -1: response = Response("Added new book to the end of the list", status=201) else: response = Response("Something went wrong! Check the logs!", status=400) return response
def get_book(isbn): obj = Book.get_book_isbn(isbn) if obj: return jsonify(obj.serialize) else: return Response("The book with such ISBN wasn't found", status=404)
def get_books(): obj = Book.get_all_books() return jsonify(books=[i.serialize for i in obj])