hey everyone. Got a question about the better implementation of the function, maybe someone could as...
o
hey everyone. Got a question about the better implementation of the function, maybe someone could assist. I have a function that should return different Response based on the received data from the
workflowService.findByCompanyId
, that returns data class:
Copy code
data class WorkflowWithStatusLookup(val workflow: Workflow, val status: Result<WorkflowStatus>)
also, I’d like to log the exception based on the “invalid” result of the status. Here is want I’ve came up with, but I’m pretty sure it’s not optimal way
Copy code
override fun listWorkflow(featureToggles: FeatureToggles): ResponseEntity<GetWorkflowsResponse> {
        return runBlocking(requestContext.coroutineContext) {
            val (valid, invalid) = workflowService.findByCompanyId(getCompanyId(), featureToggles)
                .partition { it.status.isSuccess }
            val apiResponseModel = valid.map {
                it.workflow.toWorkflowInfoResponse(it.status.getOrThrow())
            }
            return@runBlocking if (invalid.isEmpty()) {
                ResponseEntity(
                    GetWorkflowsResponse(
                        data = apiResponseModel
                    ),
                    HttpStatus.OK
                )
            } else {
                ResponseEntity(
                    GetWorkflowsResponse(
                        data = apiResponseModel,
                        meta = invalid.map { it.workflow.name }
                    ),
                    HttpStatus.PARTIAL_CONTENT
                ).also {
                    invalid.forEach {
                        throw FormNotFound(
                            it.workflow.definition.trigger.entityId!!.toString(),
                            it.workflow.companyId
                        )
                    }
                }
            }
        }
    }
r
You can really shrink your code in several ways. Starting from the responses / status codes you could create extensions for it and only pass what is necessary For different responses are you talking about different classes or extra data in there ? Also( it is a suggestion of course) try to avoid using val and putting into ?let ? the throw based on the type can still be an extension where you check it
1