示例#1
0
def create_book():
    data = request.get_json() or {}
    if 'name' not in data or 'isbn' not in data or 'author_id' not in data:
        return bad_request('must include name')
    if Book.query.filter_by(name=data['name']).first():
        return bad_request('please use a different name')
    if Book.query.filter_by(isbn=data['isbn']).first():
        return bad_request('Two books can not have the same ISBN.')
    book = Book()
    book.from_dict(data)
    db.session.add(book)
    db.session.commit()
    response = jsonify(book.to_dict())
    response.status_code = 201
    response.headers['Location'] = url_for('api.get_book', id=book.id)
    return response
示例#2
0
    def post(self):
        """
        Add book
        """
        data = request.json

        if Book.search().query('match',
                               isbn=data['isbn']).execute().hits.total != 0:
            abort(400, error='ISBN number already exist')

        book = Book.from_dict(data)
        book.save()

        return book.to_dict(include_id=True)