def create(self, api_request):

        new_uuid = str(uuid.uuid4())
        request = self.validate_request(api_request)
        data = request['where']

        if data is None:
            api_ex = ApiException(MessagesEnum.CREATE_ERROR)
            api_ex.set_params(data)
            raise api_ex

        try:
            result = self._repository.create(new_uuid, data)

            if result is None:
                api_ex = ApiException(MessagesEnum.CREATE_ERROR)
                api_ex.set_params(data)
                raise api_ex

        except Exception as err:
            self.logger.error(err)
            if data is None:
                api_ex = ApiException(MessagesEnum.CREATE_ERROR)
                raise api_ex

        return data
def updates_sync():
    """
    get:
                summary: Synchronize the updates
                responses:
                    200:
                        description: Success response
                        content:
                            application/json:
                                schema: UpdatesSyncResponseSchema

            """
    service = UpdateSynchronizerService()
    request = ApiRequest().parse_request(app)
    response = ApiResponse(request)
    status_code = 200

    try:

        raise ApiException(MessagesEnum.METHOD_NOT_IMPLEMENTED_ERROR)

    except Exception as err:
        logger.error(err)
        status_code = 501
        if isinstance(err, ApiException):
            api_ex = err
        else:
            api_ex = ApiException(MessagesEnum.METHOD_NOT_IMPLEMENTED_ERROR)

        response.set_exception(api_ex)

    return response.get_response(status_code)
def standard_list():
    """
    get:
       summary: List all standards
       responses:
            200:
                description: Success response
                content:
                    application/json:
                        schema: StandardListResponseSchema
    """
    service = StandardManagerService(logger=logger)
    request = ApiRequest().parse_request(app)
    response = ApiResponse(request)
    status_code = 200

    try:
        data = service.list(request)
        total = service.count(request)

        response.set_data(data)
        response.set_total(total)

    except Exception as err:
        logger.error(err)

        if isinstance(err, ApiException):
            api_ex = err
            status_code = 404
        else:
            api_ex = ApiException(MessagesEnum.LIST_ERROR)
            status_code = 500

        response.set_exception(api_ex)
    return response.get_response(status_code)
    def delete(self, api_request, record_uuid):
        if not record_uuid:
            api_ex = ApiException(MessagesEnum.PARAM_REQUIRED_ERROR)
            api_ex.set_params('uuid')
            raise api_ex

        # valida a request, validando os campos
        request = self.validate_request(api_request)

        result = self._repository.delete(record_uuid)

        if result is not True:
            api_ex = ApiException(MessagesEnum.DELETE_ERROR)
            api_ex.set_params(uuid)
            raise api_ex

        return result
    def get(self, api_request, record_uuid):
        if not record_uuid:
            api_ex = ApiException(MessagesEnum.PARAM_REQUIRED_ERROR)
            api_ex.set_params('uuid')
            raise api_ex

        # valida a request, validando os campos
        request = self.validate_request(api_request)

        data = self._repository.get(record_uuid, request['fields'])

        if data is None:
            api_ex = ApiException(MessagesEnum.FIND_ERROR)
            api_ex.set_params(record_uuid)
            raise api_ex

        return data
def standard_delete(uuid):
    """
    delete:
        summary: Delete Standard
        parameters:
            - in: path
              name: uuid
              description: "Standard uuid"
              required: true
              schema:
                type: string
                format: UUID
        responses:
            200:
                content:
                    application/json:
                        schema: StandardDeleteResponseSchema
    """
    service = StandardManagerService(logger=logger)
    request = ApiRequest().parse_request(app)
    response = ApiResponse(request)
    status_code = 200

    try:
        service.delete(request, uuid)
        data = {
            "success": True,
            "code": MessagesEnum.ENTITY_DELETION_SUCCESS.code,
            "label": MessagesEnum.ENTITY_DELETION_SUCCESS.label,
            "message": MessagesEnum.ENTITY_DELETION_SUCCESS.message,
            "params": [uuid]
        }
        response.set_data(data)

    except Exception as err:
        print(err)
        logger.error(err)

        if isinstance(err, ApiException):
            api_ex = err
            status_code = 404
        else:
            api_ex = ApiException(MessagesEnum.DELETE_ERROR)
            status_code = 500

        data = {
            "success": False,
            "code": api_ex.code,
            "label": api_ex.label,
            "message": api_ex.message,
            # por segurança não passar o param aqui
            "params": []
        }
        response.set_data(data)

        # response.set_exception(api_ex)
    return response.get_response(status_code)
    def update(self, api_request, record_uuid):
        if not record_uuid:
            api_ex = ApiException(MessagesEnum.PARAM_REQUIRED_ERROR)
            api_ex.set_params('uuid')
            raise api_ex

        # valida a request, validando os campos
        request = self.validate_request(api_request)
        data = request['where']

        data = self._repository.update(record_uuid, data)

        if data is None:
            api_ex = ApiException(MessagesEnum.UPDATE_ERROR)
            api_ex.set_params(uuid)
            raise api_ex

        return data
def standard_update(uuid):
    """
    put:
        summary: Update Standard
        parameters:
            - in: path
              name: uuid
              description: "Standard uuid"
              required: true
              schema:
                type: string
                format: UUID
        requestBody:
            description: 'Objeto a ser atualizado'
            required: true
            content:
                application/json:
                    schema: StandardUpdateRequest
        responses:
            200:
                content:
                    application/json:
                        schema: StandardUpdateResponseSchema
    """
    service = StandardManagerService(logger=logger)
    request = ApiRequest().parse_request(app)
    response = ApiResponse(request)
    status_code = 200

    try:
        data = service.update(request, uuid)

        response.set_data(data)

    except Exception as err:
        logger.error(err)

        if isinstance(err, ApiException):
            api_ex = err
            status_code = 404
        else:
            api_ex = ApiException(MessagesEnum.UPDATE_ERROR)
            status_code = 500

        response.set_exception(api_ex)
    return response.get_response(status_code)
def updates_get(uuid):
    """
    get:
        summary: Get Standard Update
        parameters:
            - in: path
              name: uuid
              description: "Standard Update uuid"
              required: true
              schema:
                type: string
                format: UUID
        responses:
            200:
                description: Success response
                content:
                    application/json:
                        schema: StandardUpdatesGetResponseSchema

            """

    service = StandardUpdatesService()
    request = ApiRequest().parse_request(app)
    response = ApiResponse(request)
    status_code = 200

    try:

        data = service.get(request, uuid)
        response.set_data(data)

    except Exception as err:
        logger.error(err)

        if isinstance(err, ApiException):
            api_ex = err
            status_code = 404
        else:
            api_ex = ApiException(MessagesEnum.FIND_ERROR)
            status_code = 500

        response.set_exception(api_ex)

    return response.get_response(status_code)
def updates_check():
    """
            get:
                summary: Check for updates
                responses:
                    200:
                        description: Success response
                        content:
                            application/json:
                                schema: UpdatesCheckResponseSchema

            """

    service = UpdateCheckerService()
    request = ApiRequest().parse_request(app)
    response = ApiResponse(request)
    status_code = 200

    try:
        service.execute()

        total = len(service.updates)
        data = service.get_updates()

        print(total)
        print(data)
        response.set_data(data)
        response.set_total(total)

    except Exception as err:
        logger.error(err)

        if isinstance(err, ApiException):
            api_ex = err
            status_code = 404
        else:
            api_ex = ApiException(MessagesEnum.LIST_ERROR)
            status_code = 500

        response.set_exception(api_ex)

    return response.get_response(status_code)
def standard_create():
    """
    post:
        summary: Create Standard
        requestBody:
            description: 'Objeto a ser criado'
            required: true
            content:
                application/json:
                    schema: StandardCreateRequest
        responses:
            200:
                content:
                    application/json:
                        schema: StandardCreateResponseSchema
    """
    service = StandardManagerService(logger=logger)
    request = ApiRequest().parse_request(app)
    response = ApiResponse(request)
    status_code = 200

    try:
        data = service.create(request)

        response.set_data(data)

    except Exception as err:
        logger.error(err)

        if isinstance(err, ApiException):
            api_ex = err
            status_code = 404
        else:
            api_ex = ApiException(MessagesEnum.LIST_ERROR)
            status_code = 500

        response.set_exception(api_ex)
    return response.get_response(status_code)