package mx.trackermap.TrackerMap.controllers import mx.trackermap.TrackerMap.client.apis.DevicesApi import mx.trackermap.TrackerMap.client.models.Device class DeviceController( private val devicesApi: DevicesApi, private val sessionController: SessionController) { var devices = emptyArray() suspend fun getDevices() { devices = devicesApi.devicesGet(userId = sessionController.user?.id) } fun getDevice(id: Int) = devices.find { it.id == id } suspend fun createDevice(device: Device) { devices += devicesApi.devicesPost(device) } suspend fun updateDevice(id: Int, device: Device) { devices = devices.map { if (it.id == id) devicesApi.devicesIdPut(id = id, body = device) else it }.toTypedArray() } suspend fun deleteDevice(id: Int) { devicesApi.devicesIdDelete(id) devices = devices.filter { it.id != id }.toTypedArray() } }