Exemplo n.º 1
0
    def post(self):
        data_dict = request.json  # 获取发送的 json 数据
        name = data_dict.get('name')
        category = data_dict.get('category')
        price = data_dict.get('price')
        user_id = g.user.id

        # 将用户信息存入数据库
        book = Books()
        book.name = name
        book.category = category
        book.price = price
        # 将用户 id 直接赋值给 user_id
        book.user_id = user_id

        try:
            db.session.add(book)
            db.session.commit()
        except Exception as e:
            return jsonify(code=400, msg='数据跟新失败')

        return jsonify(code=200, msg='添加书籍成功', data=book.to_dict())
Exemplo n.º 2
0
def async_new_book_add(title,isbn,description,pages,author,category,language,num_copies):
    new_book = Books()
    new_book.title = title.strip()
    new_book.isbn = isbn.strip()
    new_book.description = description.strip()
    new_book.pages = pages
    authors = [author.strip() for author in author.split(",")]
    categories = [category.strip() for category in category.split(",")]
    new_book.author = authors
    new_book.category = categories
    new_book.language = language.strip()
    new_book.num_copies = num_copies

    db.session.add(new_book)
    db.session.commit()

    for _ in range(int(new_book.num_copies)):
        new_copy = BookCopies()
        new_copy.title = new_book.title
        new_copy.book_id = new_book.id
        new_book.copies.append(new_copy)
        db.session.add(new_copy)
        db.session.commit()