From ae27feda705444df46d857be6983d73f12ead4ef Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Sat, 4 Dec 2021 18:27:07 -0600 Subject: Added first controllers (unreviewed and untested) --- .../TrackerMap/controllers/AttributesController.kt | 52 ++++++++++++++++++++++ .../TrackerMap/controllers/CommandsController.kt | 29 ++++++++++++ .../TrackerMap/controllers/DeviceController.kt | 34 ++++++++++++++ .../TrackerMap/controllers/GeofenceController.kt | 14 ++++++ .../TrackerMap/controllers/SessionController.kt | 20 +++++++++ .../TrackerMap/controllers/UserController.kt | 10 +++++ 6 files changed, 159 insertions(+) create mode 100644 shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/AttributesController.kt create mode 100644 shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/CommandsController.kt create mode 100644 shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/DeviceController.kt create mode 100644 shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/GeofenceController.kt create mode 100644 shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt create mode 100644 shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UserController.kt (limited to 'shared/src/commonMain/kotlin/mx') diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/AttributesController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/AttributesController.kt new file mode 100644 index 0000000..6262ec8 --- /dev/null +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/AttributesController.kt @@ -0,0 +1,52 @@ +package mx.trackermap.TrackerMap.controllers + +import mx.trackermap.TrackerMap.client.apis.AttributesApi +import mx.trackermap.TrackerMap.client.models.Attribute +import mx.trackermap.TrackerMap.client.models.Device + +class AttributesController( + private val attributesApi: AttributesApi, + private val sessionController: SessionController) +{ + var userAttributes = emptyArray() + val deviceAttributes = mutableMapOf>() + + /** userAttributes **/ + + suspend fun getUserAttributes() { + userAttributes = attributesApi.attributesComputedGet(userId = sessionController.user?.id) + } + + fun getUserAttribute(attribute: String) = userAttributes.find { it.attribute == attribute } + + suspend fun createUserAttribute(attribute: Attribute) { + userAttributes += attributesApi.attributesComputedPost(attribute) + } + + suspend fun updateUserAttribute(attribute: Attribute) { + userAttributes = userAttributes.map { + if (attribute.id != null && it.id == attribute.id) { + attributesApi.attributesComputedIdPut(attribute, attribute.id) + } else it + }.toTypedArray() + } + + suspend fun deleteUserAttribute(attribute: String) { + userAttributes.find { it.attribute == attribute }?.id?.let { id -> + attributesApi.attributesComputedIdDelete(id) + userAttributes = userAttributes.filter { it.id != id }.toTypedArray() + } + } + + /** deviceAttributes **/ + + suspend fun getDeviceAttributes(device: Device) { + device.id?.let { + deviceAttributes[it] = attributesApi.attributesComputedGet(deviceId = device.id) + } + } + + fun getDeviceAttribute(device: Device, attribute: String) = + deviceAttributes[device.id]?.find { it.attribute == attribute } + +} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/CommandsController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/CommandsController.kt new file mode 100644 index 0000000..b710763 --- /dev/null +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/CommandsController.kt @@ -0,0 +1,29 @@ +package mx.trackermap.TrackerMap.controllers + +import mx.trackermap.TrackerMap.client.apis.CommandsApi +import mx.trackermap.TrackerMap.client.models.Command +import mx.trackermap.TrackerMap.client.models.Device + +class CommandsController( + val commandsApi: CommandsApi, + val sessionController: SessionController) +{ + var commands = emptyArray() + val deviceCommands = mutableMapOf>() + + suspend fun getAllCommands () { + sessionController.user?.id?.let { + commands = commandsApi.commandsGet(userId = it) + } + } + + suspend fun getDeviceCommands (device: Device) { + device.id?.let { + deviceCommands[it] = commandsApi.commandsSendGet (deviceId = device.id) + } + } + + suspend fun sendCommand (command: Command) { + commandsApi.commandsSendPost(command) + } +} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/DeviceController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/DeviceController.kt new file mode 100644 index 0000000..2e0d924 --- /dev/null +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/DeviceController.kt @@ -0,0 +1,34 @@ +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() + } +} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/GeofenceController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/GeofenceController.kt new file mode 100644 index 0000000..011d094 --- /dev/null +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/GeofenceController.kt @@ -0,0 +1,14 @@ +package mx.trackermap.TrackerMap.controllers + +import mx.trackermap.TrackerMap.client.apis.GeofencesApi +import mx.trackermap.TrackerMap.client.models.Geofence + +class GeofenceController(val geofencesApi: GeofencesApi) { + var geofences = emptyArray() + + suspend fun getGeofences () { + geofences = geofencesApi.geofencesGet() + } + + fun getGeofence (id: Int) = geofences.find { it.id == id } +} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt new file mode 100644 index 0000000..ba892f6 --- /dev/null +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt @@ -0,0 +1,20 @@ +package mx.trackermap.TrackerMap.controllers + +import mx.trackermap.TrackerMap.client.apis.SessionApi +import mx.trackermap.TrackerMap.client.models.User + +class SessionController(private val sessionApi: SessionApi) { + var user: User? = null + + suspend fun createSession (email: String, password: String) { + user = sessionApi.sessionPost(email, password) + } + + suspend fun getSession () { + user = sessionApi.sessionGet() + } + + suspend fun deleteSession () { + sessionApi.sessionDelete() + } +} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UserController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UserController.kt new file mode 100644 index 0000000..a398185 --- /dev/null +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UserController.kt @@ -0,0 +1,10 @@ +package mx.trackermap.TrackerMap.controllers + +import mx.trackermap.TrackerMap.client.apis.UsersApi +import mx.trackermap.TrackerMap.client.models.User + +class UserController(private val usersApi: UsersApi) { + suspend fun updateUser(user: User) { + usersApi.usersPost(user) + } +} \ No newline at end of file -- cgit v1.2.3