I know I'm probably doing this wrong but I am a bit stuck on returning the right type in this function.
val dbFields = dbInfo.equipmentList
fun DbEquipmentWithMediaList.toApiUpdateDeviceList(): ApiUpdateDeviceList {
val newUpdate: ApiUpdateDeviceList
newUpdate = dbFields.forEach { ApiUpdateDeviceList(
deviceEquipmentId = it.sSEquipmentDbInfo.deviceEquipmentId,
secondaryDeviceId = it.sSEquipmentDbInfo.secondaryDeviceId,
deviceInstanceId = it.sSEquipmentDbInfo.deviceInstanceId.toString(),
deviceSerialNumber = it.sSEquipmentDbInfo.deviceSerialNumber,
devicePartNumber = it.sSEquipmentDbInfo.devicePartNumber,
deviceManufacturerName = it.sSEquipmentDbInfo.deviceManufacturer,
deviceInstallationDate = DateUtil.dateToStringUTCSiteSurvey(it.sSEquipmentDbInfo.deviceInstallationDate),
deviceLocation = it.sSEquipmentDbInfo.locationInfo,
deviceTechnicianNotes = it.sSEquipmentDbInfo.deviceTechnicianNotes
)
}
}
I need to return the function as an ApiUpdateDeviceList after the forEach Loop. Any help is appreciated, Thank you.
Here is a more information about the two classes for the DbEquipmentWithMediaList class and the ApiUpdateDeviceList
For DbEquipmentWithMediaList
data class DbEquipmentWithMediaList(
@Embedded val sSEquipmentDbInfo: SSEquipmentDbInfo,
@Relation(
parentColumn = "id",
entityColumn = "media_id",
entity = SurveySiteDbMedia::class
)
val mediaList: List<SurveySiteDbMedia>
)
data class SSEquipmentDbInfo(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id") val id: Long,
@ColumnInfo(name = "equipment_id", index = true) val equipmentId: Long,
@ColumnInfo(name = "actual_device_equipmentId") val deviceEquipmentId: Int?,
@ColumnInfo(name = "secondary_device_id") val secondaryDeviceId: Int,
@ColumnInfo(name = "secondary_device_checked_status") val secondaryDeviceCheckedStatus: Int?,
@ColumnInfo(name = "device_instance_id") val deviceInstanceId: Int?,
@ColumnInfo(name = "media_names_list") val mediaNames: List<String>? = null,
@ColumnInfo(name = "device_serial_number") var deviceSerialNumber: String? = null,
@ColumnInfo(name = "device_part_number") var devicePartNumber: String? = null,
@ColumnInfo(name = "device_manufacturer") var deviceManufacturer: String? = null,
@ColumnInfo(name = "device_installation_date") var deviceInstallationDate: Date? = null,
@ColumnInfo(name = "location") var locationInfo: String? = null,
@ColumnInfo(name = "device_technician_notes") var deviceTechnicianNotes: String? = null,
@ColumnInfo(name = "image_start_count") val imageStartCount: Int?,
@ColumnInfo(name = "video_start_count") val videoStartCount: Int?,
@ColumnInfo(name = "device_equipment_id") val deviceEquipmentMediaId: Int? = 0,
@ColumnInfo(name = "device_update_mode") val isDeviceUpdateMode: Boolean
)
For the ApiUpdateDeviceList
data class ApiUpdateDeviceList(
@SerializedName("DeviceEquipmentId") val deviceEquipmentId: Int?,
@SerializedName("DeviceId") val secondaryDeviceId: Int?,
@SerializedName("DeviceInstance") val deviceInstanceId: String?,
@SerializedName("SerialNumber") val deviceSerialNumber: String?,
@SerializedName("PartNumber") val devicePartNumber: String?,
@SerializedName("ManufacturerName") val deviceManufacturerName: String?,
@SerializedName("InstallationDate") val deviceInstallationDate: String?,
@SerializedName("Location") val deviceLocation: String?,
@SerializedName("TechnicianNotes") val deviceTechnicianNotes: String?
)
CodePudding user response:
So, I would rename this class not to have List in it's name, since it represents only one item:
ApiUpdateDeviceList -> ApiUpdateDevice
And since this is a one-to-many relationship, your return type from this extension function should be List<ApiUpdateDevice>
.
I would actually first break out the one-to-one relationship into its own extension function, using the with
scope function to simplify the code:
fun SurveySiteDbMedia.toApiUpdateDevice(): ApiUpdateDevice = with(sSEquipmentDbInfo) {
ApiUpdateDevice(
deviceEquipmentId = deviceEquipmentId,
secondaryDeviceId = secondaryDeviceId,
deviceInstanceId = deviceInstanceId.toString(),
deviceSerialNumber = deviceSerialNumber,
devicePartNumber = devicePartNumber,
deviceManufacturerName = deviceManufacturer,
deviceInstallationDate = DateUtil.dateToStringUTCSiteSurvey(deviceInstallationDate),
deviceLocation = locationInfo,
deviceTechnicianNotes = deviceTechnicianNotes
)
}
You can use the map
function instead of forEach
to produce a List of something out of another List. So we use that with the above function to create your list conversion succinctly:
fun DbEquipmentWithMediaList.toApiUpdateDeviceList(): List<ApiUpdateDevice> =
dbFields.map { it.toApiUpdateDevice() }