示例#1
0
def show(book_id):
    schema = BookSchema()
    book = Book.get(id=book_id)

    if not book:
        abort(404)

    return schema.dumps(book)
示例#2
0
def delete_review(book_id, review_id):

    schema = BookSchema()
    book = Book.get(id=book_id)
    review = Review.get(id=review_id)
    review.delete()
    db.commit()

    return schema.dumps(book)
示例#3
0
def delete(book_id):
    book = Book.get(id=book_id)

    if not book:
        abort(404)

    book.delete()
    db.commit()

    return '', 204
示例#4
0
def show(book_id):
    # This will serialize our data
    schema = BookSchema()
    # This gets a sandwich by ID
    book = Book.get(id=book_id)

    # If we can't find a sandwich, send a 404 response
    if not book:
        abort(404)

    # otherwise, send back the sandwich data as JSON
    return schema.dumps(book)
示例#5
0
def update(book_id):
    schema = BookSchema()
    book = Book.get(id=book_id)

    if not book:
        abort(404)

    try:
        data = schema.load(request.get_json())
        book.set(**data)
        db.commit()
    except ValidationError as err:
        return jsonify({'message': 'Validation failed', 'errors': err.messages}), 422

    return schema.dumps(book)
示例#6
0
def create_review(book_id):
    book_schema = BookSchema()
    review_schema = ReviewSchema()
    book = Book.get(id=book_id)

    try:
        data = review_schema.load(request.get_json())
        Review(**data, book=book, user=g.current_user)
        db.commit()
    except ValidationError as err:
        return jsonify({
            'message': 'Validation failed',
            'errors': err.messages
        }), 422

    return book_schema.dumps(book), 201