def book_delete(id): #Delete a book book = Book.query.get(id) db.session.delete(book) db.session.commit() return jsonify(book_schema.dump(book))
def book_update(id): #Update a book books = Book.query.filter_by(id=id) book_fields = book_schema.load(request.json) books.update(book_fields) db.session.commit() return jsonify(book_schema.dump(books[0]))
def book_delete(id): book = Book.query.get(id) if not book: return abort(404) db.session.delete(book) db.session.commit() return jsonify(book_schema.dump(book))
def book_create(): #Create a new book book_fields = book_schema.load(request.json) new_book = Book() new_book.title = book_fields["title"] db.session.add(new_book) db.session.commit() return (jsonify(book_schema.dump(new_book)), 200)
def book_delete(user, id): #Delete a book book = Book.query.filter_by(id=id, user_id=user.id).first() if not book: return abort(400) db.session.delete(book) db.session.commit() return jsonify(book_schema.dump(book))
def book_create(user): #Create a new book book_fields = book_schema.load(request.json) new_book = Book() new_book.title = book_fields["title"] user.books.append(new_book) db.session.commit() return jsonify(book_schema.dump(new_book))
def book_update(user, id): #Update a book book_fields = book_schema.load(request.json) books = Book.query.filter_by(id=id, user_id=user.id) if books.count() != 1: return abort(401, description="Unauthorized to update this book") books.update(book_fields) db.session.commit() return jsonify(book_schema.dump(books[0]))
def book_create(user=None): # Define the create function. user=none to use the decorator book_fieds = book_schema.load(request.json) # Deserializing the json into something that can be used # user_id = get_jwt_identity() # Get identity returns the userid from the JWT # user = User.query.get(user_id) # Return the user from querying the DB with the DB # if not user: # If no user then return the id # return abort(401, description="Invalid user") new_book = Book() # Creating a new instance of book new_book.title = book_fieds["title"] # Update the title user.books.append(new_book) # Add this book to the the user who created it db.session.commit() # Commit the transaction return jsonify(book_schema.dump(new_book)) # Return the json format of the book
def book_create(): #Create a new book book_fields = book_schema.load(request.json) if "title" not in book_fields.keys(): return abort(400) new_book = Book() new_book.title = book_fields["title"] db.session.add(new_book) db.session.commit() return jsonify(book_schema.dump(new_book))
def book_delete(id, user=None): # Define the update function, takes the id as an argument. user=none to use the decorator # user_id = get_jwt_identity() # Get the user ID from the jwt # user = User.query.get(user_id) # Get the user object from the db by querying the db with the id # if not user: # Check if that user exisits # return abort(401, description="Invalid user") book = Book.query.filter_by(id=id, user_id=user.id).first() # Check if the user owns that book if not book: # If no book exisits then return abort(400) db.session.delete(book) # Delete the book db.session.commit() # Commit the transaction to the database return jsonify(book_schema.dump(book)) # Return the data in the form of json
def book_delete(id): user_id = get_jwt_identity() user = User.query.get(user_id) if not user: return abort(401, description="Invalid user") book = Book.query.filter_by(id=id, user_id=user.id).first() if not book: return abort(400) db.session.delete(book) db.session.commit() return jsonify(book_schema.dump(book))
def book_create(): #Create a new book book_fields = book_schema.load(request.json) user_id = get_jwt_identity() user = User.query.get(user_id) if not user: return abort(401, description="Invalid user") new_book = Book() new_book.title = book_fields["title"] user.books.append(new_book) db.session.commit() return jsonify(book_schema.dump(new_book))
def book_update(id): #Update a book book_fields = book_schema.load(request.json) user_id = get_jwt_identity() user = User.query.get(user_id) if not user: return abort(401, description="Invalid user") books = Book.query.filter_by(id=id, user_id=user.id) if books.count() != 1: return abort(401, description="Unauthorized to update this book") books.update(book_fields) db.session.commit() return jsonify(book_schema.dump(books[0]))
def book_update(id, user=None): # Define the update function, takes the id as an argument. user=none to use the decorator # books = Book.query.filter_by(id=id) # Using the Book model to fetch one book with a specific id using the query method book_fields = book_schema.load(request.json) # Deserializing the json into something that can be used # user_id = get_jwt_identity() # Get the user id from the jwt # user = User.query.get(user_id) # Get the user by querying the DB by user ID # if not user: # Check if that user exisits # return abort(401, description="Invalid user") books = Book.query.filter_by(id=id, user_id=user.id) # Check if the user owns that book if books.count() != 1: # Raise error if the user is not authorized return abort(401, description="Unauthorized to update this book") books.update(book_fields) # Update books with the new data db.session.commit() # Commit the transaction to the DB return jsonify(book_schema.dump(books[0])) # Return the data
def book_show(id): #Return a single book book = Book.query.get(id) return jsonify(book_schema.dump(book))
def book_show(id): # Define the show function, , takes the id as an argument book = Book.query.get(id) # Using the Book model to fetch one book with a specific id using the query method return jsonify(book_schema.dump(book)) # Return the book in the form of JSON