def get_book(book_id: int): book = Book.query.get_or_404( book_id, description=f"Book with id {book_id} not found!") return jsonify({ 'success': True, 'data': book_schema.dump(book), })
def update_book(user_id: int, args: dict, book_id: int): book = Book.query.get_or_404( book_id, description=f'Book with id {book_id} not found.') book_with_existing_isbn = Book.query.filter( Book.isbn == args['isbn']).first() if book_with_existing_isbn is not None: if book_with_existing_isbn.id != book_id: abort(409, description=f'Book with ISBN {args["isbn"]} already exists.') book.title = args['title'] book.isbn = args['isbn'] book.number_of_pages = args['number_of_pages'] description = args.get('description') if description is not None: book.description = description author_id = args.get('author_id') if author_id is not None: Author.query.get_or_404( author_id, description=f'Author with id {author_id} not found.') book.author_id = author_id db.session.commit() return jsonify({'success': True, 'data': book_schema.dump(book)})
def create_book(user_id: int, args: dict, author_id: int): Author.query.get_or_404( author_id, description=f'Author with id {author_id} not found') if Book.query.filter(Book.isbn == args['isbn']).first(): abort(409, description=f'Bok with ISBN {args["isbn"]} already exists') book = Book(author_id=author_id, **args) db.session.add(book) db.session.commit() return jsonify({'success': True, 'data': book_schema.dump(book)}), 201