def getList():
    try:
        events = Events.query.all()
        data = transform(events)
        return response.ok(data, "")
    except Exception as e:
        print(e)
        return response.internalServerError()
def getDetail(event_id):
    try:
        event = Events.query.filter_by(id=event_id).first()
        if not event:
            return response.badRequest([], Message.CONST_EVENT_NOT_FOUND)
        
        data = singleTransform(event)
        return response.ok(data, "")
    except Exception as e:
        print(e)
        return response.internalServerError()
def getDetail(user_id):
    try:
        user = Users.query.filter_by(id=user_id).first()
        if not user:
            return response.badRequest([], Message.CONST_USER_NOT_FOUND)

        data = singleTransform(user)
        return response.ok(data, "")
    except Exception as e:
        print(e)
        return response.internalServerError()
def delete(event_id):
    try:
        event = Events.query.filter_by(id=event_id).first()
        if not event:
            return response.badRequest([], Message.CONST_EVENT_NOT_FOUND)
        
        db.session.delete(event)
        db.session.commit()

        return response.ok("", Message.CONST_SUCCESS_DELETE_EVENT_MESSAGE)
    except Exception as e:
        print(e)
        return response.internalServerError()
def delete(user_id):
    try:
        user = Users.query.filter_by(id=user_id).first()
        if not user:
            return response.badRequest([], Message.CONST_USER_NOT_FOUND)

        db.session.delete(user)
        db.session.commit()

        return response.ok("", Message.CONST_SUCCESS_DELETE_USER_MESSAGE)
    except Exception as e:
        print(e)
        return response.internalServerError()
def update(event_id):
    try:
        input_description = request.json['description']
    except Exception as e:
        return response.badRequest("", Message.CONST_REQUIRED_UPDATE_EVENT_VALIDATION)

    try:
        event = Events.query.filter_by(id=event_id).first()
        if not event:
            return response.badRequest([], Message.CONST_EVENT_NOT_FOUND)
        
        event.description = input_description
        db.session.commit()

        data = singleTransform(event)
        return response.ok(data, Message.CONST_SUCCESS_UPDATE_EVENT_MESSAGE)
    except Exception as e:
        print(e)
        return response.internalServerError()
def update(user_id):
    try:
        # To-DO: Validate Input
        input_email = request.json['email']
    except Exception as e:
        print(e)
        return response.badRequest(
            "", Message.CONST_REQUIRED_UPDATE_USER_VALIDATION)

    try:
        user = Users.query.filter_by(id=user_id).first()
        if not user:
            return response.badRequest([], Message.CONST_USER_NOT_FOUND)

        user.email = input_email
        db.session.commit()

        data = singleTransform(user)
        return response.ok(data, Message.CONST_SUCCESS_UPDATE_USER_MESSAGE)
    except Exception as e:
        print(e)
        return response.internalServerError()
def insert():
    try:
        # To-DO: Validate Input
        input_name = request.json['name']
        input_email = request.json['email']
        input_password = request.json['password']
    except Exception as e:
        print(e)
        return response.badRequest(
            "", Message.CONST_REQUIRED_INSERT_USER_VALIDATION)

    try:
        user = Users(name=input_name, email=input_email)
        user.setPassword(input_password)
        db.session.add(user)
        db.session.commit()

        data = singleTransform(user)
        return response.ok(data, Message.CONST_SUCCESS_INSERT_USER_MESSAGE)
    except Exception as e:
        print(e)
        return response.internalServerError()
def insert():
    try:
        input_name = request.json['name']
        input_description = request.json['description']
        input_user_id = request.json['user_id']
    except Exception as e:
        return response.badRequest("", Message.CONST_REQUIRED_INSERT_EVENT_VALIDATION)

    try:
        # Check user is exists
        user = Users.query.filter_by(id=input_user_id).first()
        if not user:
            return response.badRequest("", Message.CONST_USER_NOT_FOUND)

        event = Events(event=input_name, description=input_description, user_id=input_user_id)
        db.session.add(event)
        db.session.commit()
    
        data = singleTransform(event)
        return response.ok(data, Message.CONST_SUCCESS_INSERT_EVENT_MESSAGE)
    except Exception as e:
        print(e)
        return response.internalServerError()