Ejemplo n.º 1
0
def update_notebook(notebook_id):
    '''
    This endpoint updates a notebook
    '''
    try:
        # get notebook
        notebook = Notebook.query.filter(Notebook.id==notebook_id).first()

        # if notebook is not found
        if notebook is None:
            return jsonify({'message': 'Notebook could not be found.'}), 400
        else:

            # get json request data
            json_data =  request.get_json()

            notebook.name = json_data["name"]

            # update
            db.session.commit()

            # serialize sqlalchemy data
            serializer = NotebookSchema(many=False)
            result = serializer.dump(notebook)

            # return json result
            return jsonify({'notebook': result.data})

    except Exception:
        # return json result
        return jsonify({'status' : 'error while updating notebook'}), 400
Ejemplo n.º 2
0
def list_notebooks():
    '''
    This endpoint returns all notebooks
    '''
    try:
        # get all notebooks
        notebooks = Notebook.query.order_by(db.func.lower(Notebook.name)).all()

        # serialize sqlalchemy data
        serializer = NotebookSchema(many=True)
        result = serializer.dump(notebooks)

        # return json result
        return jsonify({'notebooks': result.data})
    except Exception:
        # return json result
        return jsonify({'status' : 'error while listing notebooks'}), 400
Ejemplo n.º 3
0
def create_new_notebook():
    '''
    This endpoint creates a new notebook
    '''
    try:
        # get json request data
        json_data =  request.get_json()

        # create new notebook
        new_notebook =  Notebook(json_data['name'])

        # add new notebook to database
        db.session.add(new_notebook)
        db.session.commit()

        # serialize sqlalchemy data
        serializer = NotebookSchema(many=False)
        result = serializer.dump(new_notebook)

        # return json result
        return jsonify({"notebook" : result.data})
    except Exception:
        # return json result
        return jsonify({'status' : 'error while creating a new notebook'})