def create_book(valid): if valid: #todo ---- if request.method == 'GET': books = Books.query.all() if not books: abort(make_response(jsonify('No books added yet'))) return jsonify({ 'success': True, 'books': [Books.details(book) for book in books] }) else: try: new_book = request.get_json() except Exception as e: abort( make_response( jsonify({'error message': 'Incorrect data'}))) if all(k in new_book for k in ('title', 'author', 'user_id')): book = Books(title=new_book['title'], author=new_book['author'], created_by=new_book['user_id']) try: book.insert() except sqlalchemy.exc.IntegrityError as e: abort( make_response( jsonify({ 'message': 'provided user does not exist' }))) except Exception as e: db.session.rollback() abort(404, e) result = { "success": True, "message": 'book ' + new_book['title'] + ' has been added successfully' } return jsonify(result) else: abort( make_response( jsonify({ 'message': 'You seem to have missed some data: Provide title of the book, its author ' 'and user_id who created the book' })))
def post_books(payload): body = request.get_json() try: book = Books(body['book_name'], body['book_type'], body['book_rate']) book.insert() except BaseException: abort(401) return jsonify({"sucess": True, "book": book.id}), 200
def new_books(payload, id): try: data = request.get_json() add_name = data.get('name') add_author = data.get('author') add_category = data.get('category_id') new_addition = Books(name=add_name, author=add_author, category_id=add_category) new_addition.insert() return jsonify({ "name": add_name, "author": add_author, "category": add_category, "success": True, "message": "successfully added a new book" }), 200 except Exception: abort(422)