Assignments are not expressions, and only expressions are allowed in this context Spring Boot Kotllin

Viewed 167

I use Kotlin and Springboot in my application . I want to set problem.problemId=patient.patientId in side a lambda function but kotlin give error as response. I get Below type of error

Assignments are not expressions, and only expressions are allowed in this context Spring Boot Kotllin

patientservice.kt

package com.nillmani.hospitalmanagement.service

import com.nillmani.hospitalmanagement.entity.Patient
import com.nillmani.hospitalmanagement.exception.PatientNotFoundException
import com.nillmani.hospitalmanagement.model.ReqPatient
import com.nillmani.hospitalmanagement.repository.PatientRepository
import org.modelmapper.ModelMapper
import org.slf4j.Logger
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.*
import java.util.function.Consumer


@Service
class PatientService {
    @Autowired
    private lateinit var patientRepository: PatientRepository
    private lateinit var modelMapper : ModelMapper
    private lateinit var logger: Logger

    fun PatientService(patientRepository: PatientRepository, modelMapper: ModelMapper, logger: Logger) {
        this.patientRepository = patientRepository
        this.modelMapper = modelMapper
        this.logger = logger
    }

    @Throws(Exception::class)
     fun findAll():List<ReqPatient>{
        try {
            var patient : List<Patient?>? = patientRepository.findAllByStatusEquelsOne()
            if (patient != null) {
                if (patient.size < 1){
                    logger.error("Here No patient Record available")
                    throw PatientNotFoundException()
                }
            }
            val dto : ReqPatient = modelMapper.map(patient,ReqPatient::class.java)
            val patientDto  : List<ReqPatient> = listOf(dto)
            patientDto.forEach{patient ->(patient.problems).forEach { problem ->(problem.problemId=patient.patientId ) }}
            return listOf(dto)
        }catch (e:Exception){
            throw Exception(e)
        }
     }
}

I get error at this line

val patientDto  : List<ReqPatient> = listOf(dto)
            patientDto.forEach{patient ->(patient.problems).forEach { problem ->(problem.problemId=patient.patientId ) }}

How can I assigb PatientId to ProblemID if I Use assignment operator kotlin gives exception

1 Answers

You need to remove the parentheses around the assignment

val patientDto  : List<ReqPatient> = listOf(dto)
patientDto.forEach{patient -> 
    (patient.problems).forEach { problem -> problem.problemId = patient.patientId  }
}

In kotlin you can enclose an expression in parentheses to create a parenthesizedExpression. since assignments are not expressions you get the error.

Related