diff --git a/STCommon/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/common/interfaces/appSettings/EmailToken.kt b/STCommon/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/common/interfaces/appSettings/EmailToken.kt new file mode 100644 index 00000000..3aed2c25 --- /dev/null +++ b/STCommon/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/common/interfaces/appSettings/EmailToken.kt @@ -0,0 +1,23 @@ +/* + * Infomaniak SwissTransfer - Multiplatform + * Copyright (C) 2024 Infomaniak Network SA + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.infomaniak.multiplatform_swisstransfer.common.interfaces.appSettings + +interface EmailToken { + val email: String + val token: String +} \ No newline at end of file diff --git a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/RealmProvider.kt b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/RealmProvider.kt index bb48e191..be5d02b0 100644 --- a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/RealmProvider.kt +++ b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/RealmProvider.kt @@ -18,6 +18,7 @@ package com.infomaniak.multiplatform_swisstransfer.database import com.infomaniak.multiplatform_swisstransfer.database.models.appSettings.AppSettingsDB +import com.infomaniak.multiplatform_swisstransfer.database.models.appSettings.EmailTokenDB import com.infomaniak.multiplatform_swisstransfer.database.models.transfers.ContainerDB import com.infomaniak.multiplatform_swisstransfer.database.models.transfers.FileDB import com.infomaniak.multiplatform_swisstransfer.database.models.transfers.TransferDB @@ -58,7 +59,7 @@ class RealmProvider(private val loadDataInMemory: Boolean = false) { } private val realmAppSettingsConfiguration = RealmConfiguration - .Builder(schema = setOf(AppSettingsDB::class)) + .Builder(schema = setOf(AppSettingsDB::class, EmailTokenDB::class)) .name("AppSettings.realm") .deleteRealmDataIfNeeded() .loadDataInMemoryIfNeeded() diff --git a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/controllers/AppSettingsController.kt b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/controllers/AppSettingsController.kt index 4fce1a5d..955c4d32 100644 --- a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/controllers/AppSettingsController.kt +++ b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/controllers/AppSettingsController.kt @@ -19,9 +19,14 @@ package com.infomaniak.multiplatform_swisstransfer.database.controllers import com.infomaniak.multiplatform_swisstransfer.common.exceptions.RealmException import com.infomaniak.multiplatform_swisstransfer.common.interfaces.appSettings.AppSettings +import com.infomaniak.multiplatform_swisstransfer.common.interfaces.appSettings.EmailToken +import com.infomaniak.multiplatform_swisstransfer.common.interfaces.transfers.File import com.infomaniak.multiplatform_swisstransfer.common.models.* import com.infomaniak.multiplatform_swisstransfer.database.RealmProvider import com.infomaniak.multiplatform_swisstransfer.database.models.appSettings.AppSettingsDB +import com.infomaniak.multiplatform_swisstransfer.database.models.appSettings.EmailTokenDB +import com.infomaniak.multiplatform_swisstransfer.database.models.transfers.FileDB +import com.infomaniak.multiplatform_swisstransfer.database.models.transfers.TransferDB import com.infomaniak.multiplatform_swisstransfer.database.utils.RealmUtils.runThrowingRealm import io.realm.kotlin.UpdatePolicy import io.realm.kotlin.ext.query @@ -56,6 +61,12 @@ class AppSettingsController(private val realmProvider: RealmProvider) { fun getAppSettings(): AppSettings? = runThrowingRealm { return appSettingsQuery.find() } + + @Throws(RealmException::class) + fun getTokenForEmail(email: String): EmailToken? = runThrowingRealm { + val query = "${EmailTokenDB::email} == '$email'" + return realm.query(query).first().find() + } //endregion //region Update data @@ -116,5 +127,13 @@ class AppSettingsController(private val realmProvider: RealmProvider) { suspend fun removeData() = runThrowingRealm { realm.write { deleteAll() } } + + @Throws(RealmException::class, CancellationException::class) + suspend fun setEmailAndToken(email: String, token: String) = runThrowingRealm { + realm.write { + val emailTokenDB = EmailTokenDB(email, token) + this.copyToRealm(emailTokenDB, UpdatePolicy.ALL) + } + } //endregion } diff --git a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/models/appSettings/EmailTokenDB.kt b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/models/appSettings/EmailTokenDB.kt new file mode 100644 index 00000000..21f9669a --- /dev/null +++ b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/models/appSettings/EmailTokenDB.kt @@ -0,0 +1,28 @@ +/* + * Infomaniak SwissTransfer - Multiplatform + * Copyright (C) 2024 Infomaniak Network SA + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.infomaniak.multiplatform_swisstransfer.database.models.appSettings + +import com.infomaniak.multiplatform_swisstransfer.common.interfaces.appSettings.EmailToken +import io.realm.kotlin.types.RealmObject +import io.realm.kotlin.types.annotations.PrimaryKey + +class EmailTokenDB( + @PrimaryKey + override var email: String = "", + override var token: String = "" +) : RealmObject, EmailToken \ No newline at end of file