Bootstrap

【Python_Flask系列之_调试书籍管理增删改查接口】

1、书籍管理_新增接口

1、调试进入数据新增接口及数据库插入成功:
下面展示一些 内联代码片

@book.route("/addBook", methods=["POST"])
def add_book():
    if request.method == "POST":
        post_data = request.get_json()
        book_list = select_data_all()
        title_list = []
        for i in range(len(book_list)):
            title_list.append(book_list[i]["title"])
        if post_data.get("title") in title_list:
            return jsonify(msg="书名(title)重复", status="fail")
        if post_data.get("title") is None:
            return jsonify(msg="title是必传参数!", status="fail")
        if post_data.get('author') is None:
            return jsonify(msg="author是必传参数!", status="fail")
        if post_data.get('read_status') is None:
            return jsonify(msg="read_status是必传参数!", status="fail")
        title = str(post_data.get('title')).strip()
        author = str(post_data.get('author')).strip()
        read_status = post_data.get('read_status')
        insert_data(title=title, author=author, read_status=read_status)
        jsonify(msg="图书添加成功", status="success")
        return jsonify(msg="图书添加成功", status="success")

在这里插入图片描述
在这里插入图片描述

2、书籍管理_修改接口

1、修改书籍接口:

    if request.method == "PUT":
        post_data = request.get_json()
        if not post_data.get('id'):
            return jsonify(msg="id是必传参数!", status="fail")
        if not post_data.get('title'):
            return jsonify(msg="title是必传参数!", status="fail")
        if not post_data.get('author'):
            return jsonify(msg="author是必传参数!", status="fail")
        if post_data.get('read_status') is None:
            return jsonify(msg="read_status是必传参数!", status="fail")
        book_list = select_data_all()
        id_list = []
        for i in range(len(book_list)):
            id_list.append(book_list[i]["id"])
        if post_data.get("id") not in id_list and int(post_data.get('id')) not in id_list:
            return jsonify(msg="需要修改的书籍id不存在!", status="fail")
        book_id = post_data.get("id")
        title = post_data.get("title")
        author = post_data.get("author")
        read_status = post_data.get("read_status")
        if read_status != 0 and read_status != 1:
            return jsonify(msg="阅读状态只能为0和1!", status="fail")
        update_data(id=book_id, title=title, author=author, read_status=read_status)
        return jsonify(msg="图书已更新", status="success")

在这里插入图片描述

3、书籍管理_查询接口

1、查询到所有书籍接口:

@book.route("/queryBook", methods=["GET"])
def query_book():
    books = select_data_all()
    return jsonify(msg="查询所有书籍成功", book=books, status="success")

在这里插入图片描述

4、书籍管理_删除接口

1、删除书籍接口:

@book.route("/deleteBook", methods=["DELETE"])
def delete_book():
    if request.method == "DELETE":
        post_data = request.get_json()
        if not post_data.get('id'):
            return jsonify(msg="id是必传参数", status="fail")
        id = post_data.get('id')
        result = delete_data(id)
        if result is False:
            return jsonify(msg="需要删除的图书不存在", status="fail")
        else:
            return jsonify(msg="图书被删除", status="fail")

在这里插入图片描述

;