Ejemplo n.º 1
0
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]))
Ejemplo n.º 2
0
def book_create(user=None):
    # Create a new book
    data = book_schema.load(request.json)
    new_book = Book()
    new_book.title = data["title"]

    user.books.append(new_book)
    db.session.commit()

    return jsonify(books_schema.dump(Book.query.all()))
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
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))
Ejemplo n.º 5
0
def book_update(id, user=None):
    # Update a book

    data = book_schema.load(request.json)
    book = Book.query.filter_by(id=id, user_id=user.id).first()

    if not book:
        return abort(401, description="Unauthorized request")

    book.title = data["title"]
    db.session.commit()
    return jsonify(books_schema.dump(Book.query.all()))
Ejemplo n.º 6
0
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]))
Ejemplo n.º 7
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
Ejemplo n.º 8
0
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_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))
Ejemplo n.º 10
0
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]))
Ejemplo n.º 11
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