Esempio n. 1
0
def get_book(id):
    try:
        book = Books.get(id=id)
        return jsonify(bookSchema.dump(book).data), 200
    except Books.DoesNotExist:
        return jsonify(
            {"message": "Can't find book with id - `{id}`".format(id=id)}), 404
Esempio n. 2
0
def endpoint(id=None):
    if request.method == 'GET':
        if id:
            return jsonify(model_to_dict(Books.get(Books.id == id)))
        else:
            bookList = []
            for book in Books.select():
                bookList.append(model_to_dict(book))
            return jsonify(bookList)

    if request.method == 'POST':
        new_book = dict_to_model(Books, request.get_json())
        new_book.save()
        return jsonify({"success": True})
Esempio n. 3
0
def update_book(id):
    try:
        books = Books.get(id=id)
    except Books.DoesNotExist:
        return jsonify(
            {"message": "Can't find book with id - `{id}`".format(id=id)}), 404

    book, errors = bookSchema.load(request.json, instance=books)

    if errors:
        return jsonify(errors), 400

    book.save()

    return jsonify(bookSchema.dump(book).data), 200
Esempio n. 4
0
def update_book(current_user, book_id):
    try:
        book = Books().get_by_id(book_id)
        if not book or book["user_id"] != current_user["_id"]:
            return {
                "message": "Book not found for user",
                "data": None,
                "error": "Not found"
            }, 404
        book = request.form
        if book.get('cover_image'):
            book["image_url"] = request.host_url+"static/books/"+save_pic(request.files["cover_image"])
        book = Books().update(book_id, **book)
        return jsonify({
            "message": "successfully updated a book",
            "data": book
        }), 201
    except Exception as e:
        return jsonify({
            "message": "failed to update a book",
            "error": str(e),
            "data": None
        }), 400