Example #1
0
def index():
    try:
        current_user = get_jwt_identity()
        if current_user:
            if not request.args.get('filter'):
                notes = Notes.query.filter_by(user_id=current_user['user_id'])
                if not notes:
                    return response.NOT_FOUND([],"No notes found")

                data = transform(notes)

                return response.OK({
                    "notes" : data
                }, "All note")

            else:
                filters = request.args.get('filter').split(",")
                notes = Notes.query.filter(or_(Notes.notes.like("%"+_+"%") for _ in filters)).all()
                data = transform(notes)

                return response.OK({
                    "filter" : filters,
                    "notes" : data
                }, "filter notes")

        else:
            return response.UNAUTHORIZED([], "Your credential is invalid")
    except Exception as e:
        print(e)
        return response.INTERNAL_SERVER_ERROR([], "Failed to load note")
Example #2
0
def delete(id):
    try:
        current_user = get_jwt_identity()
        if current_user:
            note = Notes.query.filter_by(id=id).first()
            if not note:
                return response.NO_CONTENT([], "No data deleted")
            
            db.session.delete(note)
            db.session.commit()

            return response.OK([], "Successfully delete data")

        else:
            return response.UNAUTHORIZED([], "Your credential is invalid")
    except Exception as e:
        return response.INTERNAL_SERVER_ERROR([], "Failed to delete")
Example #3
0
def show(id):
    try:
        current_user = get_jwt_identity()
        if current_user:
            note = Notes.query.filter_by(user_id=current_user['user_id'],id=id).first()
            if not note:
                return response.NOT_FOUND([],"No notes found")

            data = singleTransform(note)

            return response.OK({
                "notes" : data
            }, "notes by id")

        else:
            return response.UNAUTHORIZED([], "Your credential is invalid")
    except Exception as e:
        return response.INTERNAL_SERVER_ERROR([], "Failed to load note")
Example #4
0
def edit(id):
    try:
        current_user = get_jwt_identity()
        if current_user and current_user['user_id'] == id:
            user = Users.query.filter_by(id=current_user['user_id']).first()
            if not user:
                return response.NOT_FOUND([], 'No user found')

            data = singleTransform(user)

            return response.OK({
                "users" : data
            }, "User data loaded")

        else:
            return response.UNAUTHORIZED([], "Your credential is invalid")

    except Exception as e:
        return response.INTERNAL_SERVER_ERROR([], "Failed to login")
Example #5
0
def login():
    try:
        user_name = request.json['userName'].strip()
        password = request.json['password']
        user = Users.query.filter_by(user_name=user_name).first()
        if not user:
            return response.NOT_FOUND([], 'No user found')
        if not user.checkPassword(password):
            return response.UNAUTHORIZED([], 'Your credentials is invalid')

        access_token, refresh_token = getToken(user.id)    

        return response.OK({
            "users" : singleTransform(user),
            "access_token": access_token,
            "refresh_token": refresh_token,
        }, "Login Succes")

    except Exception as e:
        return response.INTERNAL_SERVER_ERROR('', "Failed to login")
Example #6
0
def add():
    try:
        current_user = get_jwt_identity()
        if current_user:
            title = request.json['title'].strip()
            notes = request.json['notes'].strip()

            note = Notes(user_id=current_user['user_id'], title=title, notes=notes)
            db.session.add(note)
            db.session.commit()

            data = singleTransform(note)

            return response.CREATED({
                "notes" : data
            }, "success add note")
        else:
            return response.UNAUTHORIZED([], "Your credential is invalid")
    except Exception as e:
        print(e)
        return response.INTERNAL_SERVER_ERROR([], "Failed to load note")
Example #7
0
def edit(id):
    try:
        current_user = get_jwt_identity()
        if current_user:
            note = Notes.query.filter_by(user_id=current_user['user_id'],id=id).first()
            if not note:
                return response.NOT_FOUND([],"No notes found")

            note.title = request.json['title']
            note.notes = request.json['notes']
            db.session.add(note)
            db.session.commit()

            data = singleTransform(note)

            return response.CREATED({
                "notes" : data
            }, "Success edit note")
        else:
            return response.UNAUTHORIZED([], "Your credential is invalid")
    except Exception as e:
        return response.INTERNAL_SERVER_ERROR([], "Failed to edit note")