From 1ebb7c14007f36f47c7c2de0820dcbaf15fe03ba Mon Sep 17 00:00:00 2001 From: Thomas Andres Gomez Date: Tue, 1 Apr 2025 16:01:43 +0200 Subject: [PATCH] Client : Error management --- .../kotlin/com/pixelized/desktop/lwa/App.kt | 7 +- .../desktop/lwa/DataSyncViewModel.kt | 9 +- .../com/pixelized/desktop/lwa/Module.kt | 6 +- .../desktop/lwa/network/LwaClient.kt | 70 +++---- .../desktop/lwa/network/LwaClientImpl.kt | 87 +++++---- .../lwa/network/LwaNetworkException.kt | 13 ++ .../alteration/AlterationRepository.kt | 18 +- .../repository/alteration/AlterationStore.kt | 83 ++++----- .../repository/campaign/CampaignRepository.kt | 40 ++-- .../lwa/repository/campaign/CampaignStore.kt | 108 +++++------ .../CharacterSheetRepository.kt | 33 +--- .../characterSheet/CharacterSheetStore.kt | 171 ++++++++---------- .../lwa/repository/tag/TagRepository.kt | 16 ++ .../desktop/lwa/repository/tag/TagStore.kt | 79 ++++++++ .../lwa/ui/composable/error/ErrorSnackUio.kt | 2 +- .../screen/campaign/network/NetworkDialog.kt | 4 +- .../alteration/edit/GMAlterationEditPage.kt | 5 + .../edit/GMAlterationEditViewModel.kt | 24 ++- .../alteration/list/GMAlterationViewModel.kt | 6 +- .../character/list/GMCharacterViewModel.kt | 4 +- .../lwa/model/alteration/AlterationStore.kt | 2 +- .../lwa/model/campaign/CampaignStore.kt | 2 +- .../model/character/CharacterSheetStore.kt | 2 +- .../server/lwa/model/tag/TagStore.kt | 2 +- .../com/pixelized/server/lwa/server/Engine.kt | 2 +- .../rest/alteration/DELETE_Alteration.kt | 10 +- .../server/rest/alteration/GET_Alteration.kt | 10 +- .../rest/alteration/GET_AlterationTags.kt | 10 +- .../server/rest/alteration/GET_Alterations.kt | 10 +- .../server/rest/alteration/PUT_Alateration.kt | 10 +- .../campaign/DELETE_Campaign_Character.kt | 10 +- .../rest/campaign/DELETE_Campaign_Npc.kt | 10 +- .../lwa/server/rest/campaign/GET_Campaign.kt | 10 +- .../rest/campaign/PUT_Campaign_Character.kt | 10 +- .../server/rest/campaign/PUT_Campaign_Npc.kt | 10 +- .../rest/campaign/PUT_Campaign_Scene_Name.kt | 14 +- .../server/rest/character/DELETE_Character.kt | 10 +- .../server/rest/character/GET_Character.kt | 12 +- .../rest/character/GET_CharacterTags.kt | 10 +- .../server/rest/character/GET_Characters.kt | 10 +- .../server/rest/character/PUT_Character.kt | 10 +- .../character/PUT_Character_Alteration.kt | 10 +- .../rest/character/PUT_Character_Damage.kt | 12 +- .../character/PUT_Character_Diminished.kt | 12 +- .../rest/character/PUT_Character_Fatigue.kt | 12 +- .../lwa/utils/extentions/ParametersExt.kt | 10 +- .../shared/lwa/protocol/rest/APIResponse.kt | 52 ++++++ .../shared/lwa/protocol/rest/ResultJson.kt | 34 ---- 48 files changed, 630 insertions(+), 473 deletions(-) create mode 100644 composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/network/LwaNetworkException.kt create mode 100644 composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/tag/TagRepository.kt create mode 100644 composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/tag/TagStore.kt create mode 100644 shared/src/commonMain/kotlin/com/pixelized/shared/lwa/protocol/rest/APIResponse.kt delete mode 100644 shared/src/commonMain/kotlin/com/pixelized/shared/lwa/protocol/rest/ResultJson.kt diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/App.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/App.kt index b2e5a63..93dd0e0 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/App.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/App.kt @@ -1,5 +1,8 @@ package com.pixelized.desktop.lwa +import androidx.compose.animation.AnimatedVisibility +import androidx.compose.animation.core.tween +import androidx.compose.animation.fadeOut import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material.ButtonDefaults import androidx.compose.material.MaterialTheme @@ -10,11 +13,13 @@ import androidx.compose.material.SnackbarDefaults import androidx.compose.material.SnackbarDuration import androidx.compose.material.SnackbarHost import androidx.compose.material.SnackbarHostState +import androidx.compose.material.Surface import androidx.compose.material.Text import androidx.compose.material.TextButton import androidx.compose.runtime.Composable import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.collectAsState import androidx.compose.runtime.compositionLocalOf import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.remember @@ -146,7 +151,7 @@ private fun MainWindowScreen( } LwaScaffold( - modifier = Modifier.fillMaxSize(), + modifier = Modifier.fillMaxSize() ) { MainNavHost() diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/DataSyncViewModel.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/DataSyncViewModel.kt index 349d046..4cc3f87 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/DataSyncViewModel.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/DataSyncViewModel.kt @@ -6,6 +6,7 @@ import com.pixelized.desktop.lwa.repository.campaign.CampaignRepository import com.pixelized.desktop.lwa.repository.characterSheet.CharacterSheetRepository import com.pixelized.desktop.lwa.repository.network.NetworkRepository import com.pixelized.desktop.lwa.repository.settings.SettingsRepository +import com.pixelized.desktop.lwa.repository.tag.TagRepository import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.flow.distinctUntilChanged @@ -19,6 +20,7 @@ class DataSyncViewModel( private val characterRepository: CharacterSheetRepository, private val alterationRepository: AlterationRepository, private val campaignRepository: CampaignRepository, + private val tagRepository: TagRepository, private val settingsRepository: SettingsRepository, private val networkRepository: NetworkRepository, ) : ViewModel() { @@ -40,10 +42,10 @@ class DataSyncViewModel( networkRepository.status .filter { status -> status == NetworkRepository.Status.CONNECTED } .onEach { + tagRepository.updateAlterationTags() alterationRepository.updateAlterations() - alterationRepository.updateTags() + tagRepository.updateCharacterTags() characterRepository.updateCharacterPreviews() - characterRepository.updateTags() campaignRepository.updateCampaign() } .launchIn(this) @@ -54,9 +56,8 @@ class DataSyncViewModel( .distinctUntilChanged() .onEach { instances -> instances.forEach { characterSheetId -> - characterRepository.characterDetail( + characterRepository.updateCharacterSheet( characterSheetId = characterSheetId, - forceUpdate = true, ) } } diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/Module.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/Module.kt index 9d9574f..4c03877 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/Module.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/Module.kt @@ -13,6 +13,8 @@ import com.pixelized.desktop.lwa.repository.roll_history.RollHistoryRepository import com.pixelized.desktop.lwa.repository.settings.SettingsFactory import com.pixelized.desktop.lwa.repository.settings.SettingsRepository import com.pixelized.desktop.lwa.repository.settings.SettingsStore +import com.pixelized.desktop.lwa.repository.tag.TagRepository +import com.pixelized.desktop.lwa.repository.tag.TagStore import com.pixelized.desktop.lwa.ui.composable.character.characteristic.CharacterDetailCharacteristicDialogViewModel import com.pixelized.desktop.lwa.ui.composable.character.characteristic.CharacterSheetCharacteristicDialogFactory import com.pixelized.desktop.lwa.ui.composable.character.diminished.CharacterSheetDiminishedDialogFactory @@ -37,9 +39,9 @@ import com.pixelized.desktop.lwa.ui.screen.characterSheet.edit.common.SkillField import com.pixelized.desktop.lwa.ui.screen.gamemaster.GameMasterViewModel import com.pixelized.desktop.lwa.ui.screen.gamemaster.action.GMActionViewModel import com.pixelized.desktop.lwa.ui.screen.gamemaster.alteration.edit.GMAlterationEditFactory +import com.pixelized.desktop.lwa.ui.screen.gamemaster.alteration.edit.GMAlterationEditViewModel import com.pixelized.desktop.lwa.ui.screen.gamemaster.alteration.list.GMAlterationFactory import com.pixelized.desktop.lwa.ui.screen.gamemaster.alteration.list.GMAlterationViewModel -import com.pixelized.desktop.lwa.ui.screen.gamemaster.alteration.edit.GMAlterationEditViewModel import com.pixelized.desktop.lwa.ui.screen.gamemaster.character.list.GMCharacterFactory import com.pixelized.desktop.lwa.ui.screen.gamemaster.character.list.GMCharacterViewModel import com.pixelized.desktop.lwa.ui.screen.gamemaster.common.tag.GMTagFactory @@ -100,6 +102,7 @@ val storeDependencies singleOf(::SettingsStore) singleOf(::AlterationStore) singleOf(::CampaignStore) + singleOf(::TagStore) } val repositoryDependencies @@ -110,6 +113,7 @@ val repositoryDependencies singleOf(::SettingsRepository) singleOf(::AlterationRepository) singleOf(::CampaignRepository) + singleOf(::TagRepository) } val factoryDependencies diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/network/LwaClient.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/network/LwaClient.kt index 24e8365..e3c66b8 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/network/LwaClient.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/network/LwaClient.kt @@ -5,83 +5,89 @@ import com.pixelized.shared.lwa.model.campaign.CampaignJson import com.pixelized.shared.lwa.model.characterSheet.CharacterSheetJson import com.pixelized.shared.lwa.model.tag.TagJson import com.pixelized.shared.lwa.protocol.rest.CharacterPreviewJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse interface LwaClient { // Alteration - suspend fun alterations(): List + suspend fun getAlterations(): APIResponse> - suspend fun alterations( + suspend fun getAlteration( alterationId: String, - ): AlterationJson? + ): APIResponse - suspend fun updateAlteration( - alterationJson: AlterationJson - ) + suspend fun putAlteration( + alterationJson: AlterationJson, + create: Boolean, + ): APIResponse suspend fun deleteAlteration( alterationId: String, - ) + ): APIResponse - suspend fun alterationTags(): List + suspend fun getAlterationTags(): APIResponse> // Campaign - suspend fun campaign(): CampaignJson + suspend fun getCampaign(): APIResponse - suspend fun campaignAddCharacter( + suspend fun putCampaignCharacter( characterSheetId: String, - ) + ): APIResponse - suspend fun campaignRemoveCharacter( + suspend fun removeCampaignCharacter( characterSheetId: String, - ) + ): APIResponse - suspend fun campaignAddNpc( + suspend fun putCampaignNpc( characterSheetId: String, - ) + ): APIResponse - suspend fun campaignRemoveNpc( + suspend fun removeCampaignNpc( characterSheetId: String, - ) + ): APIResponse // Character - suspend fun characters(): List + suspend fun getCharacters(): APIResponse> - suspend fun characterTags(): List + suspend fun getCharacterTags(): APIResponse> - suspend fun character( + suspend fun getCharacter( characterSheetId: String, - ): CharacterSheetJson + ): APIResponse - suspend fun updateCharacter( + suspend fun putCharacter( sheet: CharacterSheetJson, - ) + ): APIResponse - suspend fun updateCharacterDamage( + suspend fun putCharacterDamage( characterSheetId: String, damage: Int, - ) + ): APIResponse - suspend fun updateCharacterFatigue( + suspend fun putCharacterFatigue( characterSheetId: String, fatigue: Int, - ) + ): APIResponse - suspend fun updateCharacterDiminished( + suspend fun putCharacterDiminished( characterSheetId: String, diminished: Int, - ) + ): APIResponse - suspend fun updateCharacterAlteration( + suspend fun putCharacterAlteration( characterSheetId: String, alterationId: Int, active: Boolean, - ) + ): APIResponse suspend fun deleteCharacterSheet( characterSheetId: String, - ) + ): APIResponse + + companion object { + fun error(error: APIResponse<*>): Nothing = throw LwaNetworkException(error) + } } \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/network/LwaClientImpl.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/network/LwaClientImpl.kt index 62d2449..cf4bf3a 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/network/LwaClientImpl.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/network/LwaClientImpl.kt @@ -5,6 +5,7 @@ import com.pixelized.shared.lwa.model.alteration.AlterationJson import com.pixelized.shared.lwa.model.campaign.CampaignJson import com.pixelized.shared.lwa.model.characterSheet.CharacterSheetJson import com.pixelized.shared.lwa.model.tag.TagJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import com.pixelized.shared.lwa.protocol.rest.CharacterPreviewJson import io.ktor.client.HttpClient import io.ktor.client.call.body @@ -21,114 +22,134 @@ class LwaClientImpl( ) : LwaClient { private val root = setting.settings().network.root - override suspend fun alterations(): List = client + @Throws + override suspend fun getAlterations(): APIResponse> = client .get("$root/alteration/all") .body() - override suspend fun alterations( + @Throws + override suspend fun getAlteration( alterationId: String, - ): AlterationJson? = client + ): APIResponse = client .get("$root/alteration/detail?alterationId=$alterationId") .body() - override suspend fun updateAlteration( - alterationJson: AlterationJson + @Throws + override suspend fun putAlteration( + alterationJson: AlterationJson, + create: Boolean, ) = client - .put("$root/alteration/update") { + .put("$root/alteration/update?create=$create") { contentType(ContentType.Application.Json) setBody(alterationJson) } - .body() + .body>() + @Throws override suspend fun deleteAlteration(alterationId: String) = client .delete("$root/alteration/delete?alterationId=$alterationId") - .body() + .body>() - override suspend fun alterationTags(): List = client + @Throws + override suspend fun getAlterationTags(): APIResponse> = client .get("$root/alteration/tags") .body() - override suspend fun campaign(): CampaignJson = client + @Throws + override suspend fun getCampaign(): APIResponse = client .get("$root/campaign") .body() - override suspend fun campaignAddCharacter( + @Throws + override suspend fun putCampaignCharacter( characterSheetId: String, ) = client .put("$root/campaign/character/add?characterSheetId=$characterSheetId") - .body() + .body>() - override suspend fun campaignRemoveCharacter( + @Throws + override suspend fun removeCampaignCharacter( characterSheetId: String, ) = client .delete("$root/campaign/character/delete?characterSheetId=$characterSheetId") - .body() + .body>() - override suspend fun campaignAddNpc( + @Throws + override suspend fun putCampaignNpc( characterSheetId: String, ) = client .put("$root/campaign/npc/add?characterSheetId=$characterSheetId") - .body() + .body>() - override suspend fun campaignRemoveNpc( + @Throws + override suspend fun removeCampaignNpc( characterSheetId: String, ) = client .delete("$root/campaign/npc/delete?characterSheetId=$characterSheetId") - .body() + .body>() - override suspend fun characters(): List = client + @Throws + override suspend fun getCharacters(): APIResponse> = client .get("$root/character/all") .body() - override suspend fun characterTags(): List = client + @Throws + override suspend fun getCharacterTags(): APIResponse> = client .get("$root/character/tags") .body() - override suspend fun character( + @Throws + override suspend fun getCharacter( characterSheetId: String, - ): CharacterSheetJson = client + ): APIResponse = client .get("$root/character/detail?characterSheetId=$characterSheetId") .body() - override suspend fun updateCharacter( + @Throws + override suspend fun putCharacter( sheet: CharacterSheetJson, ) = client .put("$root/character/update/sheet") { contentType(ContentType.Application.Json) setBody(sheet) } - .body() + .body>() - override suspend fun updateCharacterDamage( + @Throws + override suspend fun putCharacterDamage( characterSheetId: String, damage: Int, ) = client .put("$root/character/update/damage?characterSheetId=$characterSheetId&damage=$damage") - .body() + .body>() - override suspend fun updateCharacterFatigue( + @Throws + override suspend fun putCharacterFatigue( characterSheetId: String, fatigue: Int, ) = client .put("$root/character/update/fatigue?characterSheetId=$characterSheetId&fatigue=$fatigue") - .body() + .body>() - override suspend fun updateCharacterDiminished( + @Throws + override suspend fun putCharacterDiminished( characterSheetId: String, diminished: Int, ) = client .put("$root/character/update/diminished?characterSheetId=$characterSheetId&diminished=$diminished") - .body() + .body>() - override suspend fun updateCharacterAlteration( + @Throws + override suspend fun putCharacterAlteration( characterSheetId: String, alterationId: Int, active: Boolean, ) = client .put("$root/character/update/alteration?characterSheetId=$characterSheetId&alterationId=$alterationId&active=$active") - .body() + .body>() + @Throws override suspend fun deleteCharacterSheet(characterSheetId: String) = client .delete("$root/character/delete?characterSheetId=$characterSheetId") - .body() + .body>() } \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/network/LwaNetworkException.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/network/LwaNetworkException.kt new file mode 100644 index 0000000..3f048e8 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/network/LwaNetworkException.kt @@ -0,0 +1,13 @@ +package com.pixelized.desktop.lwa.network + +import com.pixelized.shared.lwa.protocol.rest.APIResponse + +class LwaNetworkException( + val status: Int, + message: String, +) : Exception(message) { + constructor(error: APIResponse<*>) : this( + status = error.status, + message = error.message ?: "An unknown error occurred" + ) +} \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/alteration/AlterationRepository.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/alteration/AlterationRepository.kt index 7687eaa..18be883 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/alteration/AlterationRepository.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/alteration/AlterationRepository.kt @@ -19,6 +19,7 @@ import kotlinx.coroutines.flow.stateIn class AlterationRepository( private val alterationStore: AlterationStore, + campaignRepository: CampaignRepository, characterRepository: CharacterSheetRepository, ) { @@ -26,8 +27,6 @@ class AlterationRepository( val alterationFlow get() = alterationStore.alterationsFlow - val tagsFlow get() = alterationStore.tagsFlow - /** * This flow transform the campaign instance (player + npc) into a * Map>. @@ -66,11 +65,7 @@ class AlterationRepository( ) suspend fun updateAlterations() { - alterationStore.updateAlterations() - } - - suspend fun updateTags() { - alterationStore.updateTags() + alterationStore.updateAlterationsFlow() } fun alteration( @@ -91,10 +86,15 @@ class AlterationRepository( return activeAlterationMapFlow.map { it[characterSheetId] ?: emptyMap() } } + @Throws suspend fun updateAlteration( - alteration: Alteration + alteration: Alteration, + create: Boolean, ) { - alterationStore.putAlteration(alteration) + alterationStore.putAlteration( + alteration = alteration, + create = create, + ) } suspend fun deleteAlteration( diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/alteration/AlterationStore.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/alteration/AlterationStore.kt index 1924f74..74ed229 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/alteration/AlterationStore.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/alteration/AlterationStore.kt @@ -4,8 +4,6 @@ import com.pixelized.desktop.lwa.network.LwaClient import com.pixelized.desktop.lwa.repository.network.NetworkRepository import com.pixelized.shared.lwa.model.alteration.Alteration import com.pixelized.shared.lwa.model.alteration.AlterationJsonFactory -import com.pixelized.shared.lwa.model.tag.Tag -import com.pixelized.shared.lwa.model.tag.TagJsonFactory import com.pixelized.shared.lwa.protocol.websocket.ApiSynchronisation import com.pixelized.shared.lwa.protocol.websocket.SocketMessage import kotlinx.coroutines.CoroutineScope @@ -18,22 +16,14 @@ import kotlinx.coroutines.launch class AlterationStore( private val networkRepository: NetworkRepository, - private val alterationFactory: AlterationJsonFactory, - private val tagFactory: TagJsonFactory, + private val factory: AlterationJsonFactory, private val client: LwaClient, ) { private val _alterationsFlow = MutableStateFlow>(emptyMap()) val alterationsFlow: StateFlow> = _alterationsFlow - private val _tagsFlow = MutableStateFlow>(emptyMap()) - val tagsFlow: StateFlow> = _tagsFlow - init { val scope = CoroutineScope(Dispatchers.IO + Job()) - scope.launch { - updateAlterations() - updateTags() - } // data update through WebSocket. scope.launch { networkRepository.data.collect(::handleMessage) @@ -44,19 +34,11 @@ class AlterationStore( return alterationsFlow.value.values } - fun tags(): Collection { - return tagsFlow.value.values - } - fun alteration(alterationId: String): Alteration? { return alterationsFlow.value[alterationId] } - fun tag(tagId: String): Tag? { - return tagsFlow.value[tagId] - } - - suspend fun updateAlterations() { + suspend fun updateAlterationsFlow() { _alterationsFlow.value = try { getAlteration() } catch (exception: Exception) { @@ -65,7 +47,7 @@ class AlterationStore( } } - suspend fun updateAlteration( + private suspend fun updateAlterationFlow( alterationId: String, ) { val alteration = try { @@ -74,9 +56,9 @@ class AlterationStore( println(exception.message) // TODO proper exception handling null } - + // guard case if getAlteration failed if (alteration == null) return - + // update the flow with the alteration. _alterationsFlow.update { alterations -> alterations.toMutableMap().also { it[alterationId] = alteration @@ -84,60 +66,59 @@ class AlterationStore( } } - suspend fun updateTags() { - _tagsFlow.value = try { - getAlterationTag() - } catch (exception: Exception) { - println(exception.message) // TODO proper exception handling - emptyMap() - } - } - @Throws private suspend fun getAlteration(): Map { - val request = client.alterations() - val data = request.map { alterationFactory.convertFromJson(json = it) } - return data.associateBy { it.id } + val request = client.getAlterations() + return when (request.success) { + true -> request.data + ?.map { factory.convertFromJson(json = it) } + ?.associateBy { it.id } + ?: emptyMap() + + else -> LwaClient.error(error = request) + } } @Throws private suspend fun getAlteration( alterationId: String, ): Alteration? { - val request = client.alterations(alterationId = alterationId) - return request?.let { alterationFactory.convertFromJson(json = it) } - } - - @Throws - private suspend fun getAlterationTag(): Map { - val request = client.alterationTags() - val data = request.map { tagFactory.convertFromJson(json = it) } - return data.associateBy { it.id } + val request = client.getAlteration(alterationId = alterationId) + return when (request.success) { + true -> request.data?.let { factory.convertFromJson(json = it) } + else -> LwaClient.error(error = request) + } } @Throws suspend fun putAlteration( alteration: Alteration, + create: Boolean, ) { - client.updateAlteration( - alterationJson = alterationFactory.convertToJson(data = alteration) + val request = client.putAlteration( + alterationJson = factory.convertToJson(data = alteration), + create = create, ) + if (request.success.not()) { + LwaClient.error(error = request) + } } @Throws suspend fun deleteAlteration( - alterationId: String + alterationId: String, ) { - client.deleteAlteration( - alterationId = alterationId - ) + val request = client.deleteAlteration(alterationId = alterationId) + if (request.success.not()) { + LwaClient.error(error = request) + } } // region: WebSocket & data update. private suspend fun handleMessage(message: SocketMessage) { when (message) { - is ApiSynchronisation.AlterationUpdate -> updateAlteration( + is ApiSynchronisation.AlterationUpdate -> updateAlterationFlow( alterationId = message.alterationId, ) diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/campaign/CampaignRepository.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/campaign/CampaignRepository.kt index 7041c69..da1b34b 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/campaign/CampaignRepository.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/campaign/CampaignRepository.kt @@ -3,33 +3,45 @@ package com.pixelized.desktop.lwa.repository.campaign class CampaignRepository( private val store: CampaignStore, ) { - val campaignFlow get() = store.campaignFlow + val campaignFlow = store.campaignFlow() suspend fun updateCampaign() { - store.campaign(update = true) + store.updateCampaignFlow() } + @Throws suspend fun addCharacter( characterSheetId: String, - ) = store.addCharacter( - characterSheetId = characterSheetId, - ) + ) { + store.addCharacter( + characterSheetId = characterSheetId, + ) + } + @Throws suspend fun removeCharacter( characterSheetId: String, - ) = store.removeCharacter( - characterSheetId = characterSheetId, - ) + ) { + store.removeCharacter( + characterSheetId = characterSheetId, + ) + } + @Throws suspend fun addNpc( characterSheetId: String, - ) = store.addNpc( - characterSheetId = characterSheetId, - ) + ) { + store.addNpc( + characterSheetId = characterSheetId, + ) + } + @Throws suspend fun removeNpc( characterSheetId: String, - ) = store.removeNpc( - characterSheetId = characterSheetId, - ) + ) { + store.removeNpc( + characterSheetId = characterSheetId, + ) + } } \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/campaign/CampaignStore.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/campaign/CampaignStore.kt index 7e0d480..74adc1c 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/campaign/CampaignStore.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/campaign/CampaignStore.kt @@ -19,81 +19,76 @@ class CampaignStore( private val client: LwaClient, private val network: NetworkRepository, private val factory: CampaignJsonFactory, - - ) { - private val _campaignFlow = MutableStateFlow(value = Campaign.empty()) - val campaignFlow: StateFlow get() = _campaignFlow +) { + private val campaignFlow = MutableStateFlow(value = Campaign.empty()) init { val scope = CoroutineScope(Dispatchers.IO + Job()) - scope.launch { - campaign(update = true) - } scope.launch { network.data.collect(::handleMessage) } } - suspend fun campaign(update: Boolean): Campaign { - val campaign = _campaignFlow.value - if (update || campaign == Campaign.empty()) { - val data = try { - val request = client.campaign() - factory.convertFromJson(json = request) - } catch (exception: Exception) { - println(exception) // TODO proper exception handling - Campaign.empty() - } - return _campaignFlow.update(data) + fun campaignFlow(): StateFlow = campaignFlow + + suspend fun updateCampaignFlow() { + campaignFlow.value = try { + getCampaign() + } catch (exception: Exception) { + println(exception.message) // TODO proper exception handling + Campaign.empty() } - return campaign } + @Throws suspend fun addCharacter( characterSheetId: String, ) { - try { - client.campaignAddCharacter( - characterSheetId = characterSheetId - ) - } catch (exception: Exception) { - println(exception) // TODO proper exception handling + val request = client.putCampaignCharacter(characterSheetId = characterSheetId) + if (request.success.not()) { + LwaClient.error(error = request) } } + @Throws suspend fun removeCharacter( characterSheetId: String, ) { - try { - client.campaignRemoveCharacter( - characterSheetId = characterSheetId, - ) - } catch (exception: Exception) { - println(exception) // TODO proper exception handling + val request = client.removeCampaignCharacter(characterSheetId = characterSheetId) + if (request.success.not()) { + LwaClient.error(error = request) } } + @Throws suspend fun addNpc( characterSheetId: String, ) { - try { - client.campaignAddNpc( - characterSheetId = characterSheetId - ) - } catch (exception: Exception) { - println(exception) // TODO proper exception handling + val request = client.putCampaignNpc(characterSheetId = characterSheetId) + if (request.success.not()) { + LwaClient.error(error = request) } } + @Throws suspend fun removeNpc( characterSheetId: String, ) { - try { - client.campaignRemoveNpc( - characterSheetId = characterSheetId, - ) - } catch (exception: Exception) { - println(exception) // TODO proper exception handling + val request = client.removeCampaignNpc(characterSheetId = characterSheetId) + if (request.success.not()) { + LwaClient.error(error = request) + } + } + + @Throws + private suspend fun getCampaign(): Campaign { + val request = client.getCampaign() + return when (request.success) { + true -> request.data + ?.let { factory.convertFromJson(json = it) } + ?: Campaign.empty() + + else -> LwaClient.error(error = request) } } @@ -103,7 +98,7 @@ class CampaignStore( when (message) { is CampaignEvent -> { when (message) { - is CampaignEvent.CharacterAdded -> _campaignFlow.update { campaign -> + is CampaignEvent.CharacterAdded -> campaignFlow.update { campaign -> campaign.copy( characters = campaign.characters.toMutableSet().also { it.add(message.characterSheetId) @@ -111,7 +106,7 @@ class CampaignStore( ) } - is CampaignEvent.CharacterRemoved -> _campaignFlow.update { campaign -> + is CampaignEvent.CharacterRemoved -> campaignFlow.update { campaign -> campaign.copy( characters = campaign.characters.toMutableSet().also { it.remove(message.characterSheetId) @@ -119,7 +114,7 @@ class CampaignStore( ) } - is CampaignEvent.NpcAdded -> _campaignFlow.update { campaign -> + is CampaignEvent.NpcAdded -> campaignFlow.update { campaign -> campaign.copy( npcs = campaign.npcs.toMutableSet().also { it.add(message.characterSheetId) @@ -127,7 +122,7 @@ class CampaignStore( ) } - is CampaignEvent.NpcRemoved -> _campaignFlow.update { campaign -> + is CampaignEvent.NpcRemoved -> campaignFlow.update { campaign -> campaign.copy( npcs = campaign.npcs.toMutableSet().also { it.remove(message.characterSheetId) @@ -135,7 +130,7 @@ class CampaignStore( ) } - is CampaignEvent.UpdateScene -> _campaignFlow.update { campaign -> + is CampaignEvent.UpdateScene -> campaignFlow.update { campaign -> campaign.copy( scene = Campaign.Scene(name = message.name) ) @@ -145,17 +140,17 @@ class CampaignStore( is GameMasterEvent -> when (message) { is GameMasterEvent.ToggleNpc -> { - _campaignFlow.value = _campaignFlow.value.copy( - options = _campaignFlow.value.options.copy( - showNpcs = _campaignFlow.value.options.showNpcs.not() + campaignFlow.value = campaignFlow.value.copy( + options = campaignFlow.value.options.copy( + showNpcs = campaignFlow.value.options.showNpcs.not() ) ) } is GameMasterEvent.TogglePlayer -> { - _campaignFlow.value = _campaignFlow.value.copy( - options = _campaignFlow.value.options.copy( - showParty = _campaignFlow.value.options.showParty.not() + campaignFlow.value = campaignFlow.value.copy( + options = campaignFlow.value.options.copy( + showParty = campaignFlow.value.options.showParty.not() ) ) } @@ -168,9 +163,4 @@ class CampaignStore( } // endregion - - private fun MutableStateFlow.update(campaign: Campaign): Campaign { - value = campaign - return campaign - } } diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/characterSheet/CharacterSheetRepository.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/characterSheet/CharacterSheetRepository.kt index 34f1f3d..beba518 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/characterSheet/CharacterSheetRepository.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/characterSheet/CharacterSheetRepository.kt @@ -18,46 +18,27 @@ class CharacterSheetRepository( val characterSheetPreviewFlow get() = store.previewFlow - val characterDetailFlow get() = store.detailFlow - val tagsFlow get() = store.tagsFlow - suspend fun updateCharacterPreviews() { - store.updateCharactersPreview() + store.updateCharactersPreviewFlow() } - suspend fun updateTags() { - store.updateTags() + suspend fun updateCharacterSheet(characterSheetId: String) { + store.updateCharacterSheetDetailFlow(characterSheetId = characterSheetId) } fun characterPreview(characterId: String?): CharacterSheetPreview? { return characterSheetPreviewFlow.value.firstOrNull { it.characterSheetId == characterId } } - suspend fun characterDetail( + fun characterDetail( characterSheetId: String?, - forceUpdate: Boolean = false, ): CharacterSheet? { - return try { - characterSheetId?.let { - store.getCharacterSheet( - characterSheetId = it, - forceUpdate = forceUpdate - ) - } - } catch (exception: Exception) { - null - } + return store.detailFlow.value[characterSheetId] } fun characterDetailFlow( characterSheetId: String?, ): StateFlow { - val initial = store.detailFlow.value[characterSheetId] - - if (initial == null) { - scope.launch { characterDetail(characterSheetId = characterSheetId) } - } - return store.detailFlow .map { sheets -> sheets[characterSheetId] @@ -65,16 +46,18 @@ class CharacterSheetRepository( .stateIn( scope = scope, started = SharingStarted.Eagerly, - initialValue = initial, + initialValue = store.detailFlow.value[characterSheetId], ) } + @Throws suspend fun updateCharacter( sheet: CharacterSheet, ) { store.updateCharacterSheet(sheet = sheet) } + @Throws suspend fun deleteCharacter( characterSheetId: String, ) { diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/characterSheet/CharacterSheetStore.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/characterSheet/CharacterSheetStore.kt index def69ff..29c4635 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/characterSheet/CharacterSheetStore.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/characterSheet/CharacterSheetStore.kt @@ -6,12 +6,9 @@ import com.pixelized.desktop.lwa.repository.network.NetworkRepository import com.pixelized.shared.lwa.model.characterSheet.CharacterSheet import com.pixelized.shared.lwa.model.characterSheet.CharacterSheetPreview import com.pixelized.shared.lwa.model.characterSheet.factory.CharacterSheetJsonFactory -import com.pixelized.shared.lwa.model.tag.Tag -import com.pixelized.shared.lwa.model.tag.TagJsonFactory import com.pixelized.shared.lwa.protocol.websocket.ApiSynchronisation import com.pixelized.shared.lwa.protocol.websocket.CharacterSheetEvent import com.pixelized.shared.lwa.protocol.websocket.SocketMessage -import com.pixelized.shared.lwa.usecase.CharacterSheetUseCase import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job @@ -25,7 +22,6 @@ class CharacterSheetStore( private val client: LwaClient, private val network: NetworkRepository, private val factory: CharacterSheetJsonFactory, - private val tagFactory: TagJsonFactory, ) { private val _previewFlow = MutableStateFlow>(value = emptyList()) val previewFlow: StateFlow> get() = _previewFlow @@ -33,16 +29,8 @@ class CharacterSheetStore( private val _detailFlow = MutableStateFlow>(value = emptyMap()) val detailFlow: StateFlow> get() = _detailFlow - private val _tagsFlow = MutableStateFlow>(emptyMap()) - val tagsFlow: StateFlow> = _tagsFlow - init { val scope = CoroutineScope(Dispatchers.IO + Job()) - // initial data loading. - scope.launch { - updateCharactersPreview() - updateTags() - } // data update through WebSocket. scope.launch { network.data.collect(::handleMessage) @@ -51,71 +39,75 @@ class CharacterSheetStore( // region Rest - suspend fun updateCharactersPreview(): List { - val request = try { - client.characters() + suspend fun updateCharactersPreviewFlow() { + _previewFlow.value = try { + getCharacters() } catch (exception: Exception) { - println(exception) // TODO proper exception handling + println(exception.message) // TODO proper exception handling emptyList() } - val characters = request.map { - factory.convertFromJson(it) - } - return _previewFlow.update(characters) } - suspend fun updateTags(): Map { - val request = try { - client.characterTags() - } catch (exception: Exception) { - println(exception) // TODO proper exception handling - emptyList() - } - val tags = request.map { - tagFactory.convertFromJson(json = it) - }.associateBy { it.id } - - _tagsFlow.value = tags - return tags - } - - suspend fun getCharacterSheet( + suspend fun updateCharacterSheetDetailFlow( characterSheetId: String, - forceUpdate: Boolean = false, - ): CharacterSheet? { - val character = _detailFlow.value[characterSheetId] - if (forceUpdate || character == null) { - try { - val request = client.character(characterSheetId = characterSheetId) - val data = factory.convertFromJson(json = request) - return _detailFlow.update(data) - } catch (exception: Exception) { - println(exception) // TODO proper exception handling - return null + ) { + val characterSheet = try { + getCharacterSheet(characterSheetId = characterSheetId) + } catch (exception: Exception) { + println(exception.message) // TODO proper exception handling + null + } + if (characterSheet == null) return + _detailFlow.update { data -> + data.toMutableMap().also { + it[characterSheetId] = characterSheet } } - return character } + @Throws + suspend fun getCharacters(): List { + val request = client.getCharacters() + return when (request.success) { + true -> request.data + ?.map { factory.convertFromJson(it) } + ?: emptyList() + + else -> LwaClient.error(error = request) + } + } + + @Throws + suspend fun getCharacterSheet( + characterSheetId: String, + ): CharacterSheet { + val request = client.getCharacter(characterSheetId = characterSheetId) + return when (request.success) { + true -> request.data!!.let { factory.convertFromJson(json = it) } + else -> LwaClient.error(error = request) + } + } + + // TODO check crash + @Throws suspend fun updateCharacterSheet( sheet: CharacterSheet, ) { val json = factory.convertToJson(sheet = sheet) - try { - client.updateCharacter(sheet = json) - } catch (exception: Exception) { - println(exception) // TODO + val request = client.putCharacter(sheet = json) + if (request.success) { + LwaClient.error(error = request) } - _detailFlow.update(sheet = sheet) } + // TODO check crash + @Throws suspend fun deleteCharacterSheet( characterSheetId: String, ) { - try { - client.deleteCharacterSheet(characterSheetId = characterSheetId) - } catch (exception: Exception) { - println(exception) // TODO + val request = client.deleteCharacterSheet(characterSheetId = characterSheetId) + if (request.success) { + LwaClient.error(error = request) } } @@ -124,32 +116,35 @@ class CharacterSheetStore( private suspend fun handleMessage(message: SocketMessage) { when (message) { - is ApiSynchronisation -> when (message) { - is ApiSynchronisation.CharacterSheetUpdate -> { - getCharacterSheet( - characterSheetId = message.characterSheetId, - forceUpdate = true, - ) - if (_previewFlow.value.firstOrNull { it.characterSheetId == message.characterSheetId } == null) { - updateCharactersPreview() - } - } - - is ApiSynchronisation.CharacterSheetDelete -> { - _previewFlow.update { previews -> - previews.toMutableList().also { sheet -> - sheet.removeIf { - it.characterSheetId == message.characterSheetId - } + is ApiSynchronisation -> try { + when (message) { + is ApiSynchronisation.CharacterSheetUpdate -> { + _detailFlow.update( + sheet = getCharacterSheet(characterSheetId = message.characterSheetId) + ) + if (_previewFlow.value.firstOrNull { it.characterSheetId == message.characterSheetId } == null) { + _previewFlow.value = getCharacters() } } - _detailFlow.update { sheets -> - sheets.toMutableMap().also { it.remove(message.characterSheetId) } - } - } - is ApiSynchronisation.AlterationUpdate -> Unit - is ApiSynchronisation.AlterationDelete -> Unit + is ApiSynchronisation.CharacterSheetDelete -> { + _previewFlow.update { previews -> + previews.toMutableList().also { sheet -> + sheet.removeIf { + it.characterSheetId == message.characterSheetId + } + } + } + _detailFlow.update { sheets -> + sheets.toMutableMap().also { it.remove(message.characterSheetId) } + } + } + + is ApiSynchronisation.AlterationUpdate -> Unit + is ApiSynchronisation.AlterationDelete -> Unit + } + } catch (exception: Exception) { + println(exception.message) // TODO proper exception handling } is CharacterSheetEvent -> when (message) { @@ -190,7 +185,7 @@ class CharacterSheetStore( skillId: String, used: Boolean, ) { - val sheet = getCharacterSheet(characterSheetId = characterSheetId) ?: return + val sheet = getCharacterSheet(characterSheetId = characterSheetId) val character = sheet.copy( commonSkills = sheet.commonSkills.map { skill -> skill.takeIf { skill.id == skillId }?.copy(used = used) ?: skill @@ -211,7 +206,7 @@ class CharacterSheetStore( active: Boolean, ) { if (alterationStore.alteration(alterationId = alterationId) == null) return - val sheet = getCharacterSheet(characterSheetId = characterSheetId) ?: return + val sheet = getCharacterSheet(characterSheetId = characterSheetId) val containAlteration = sheet.alterations.contains(alterationId) @@ -233,7 +228,7 @@ class CharacterSheetStore( characterSheetId: String, damage: Int, ) { - val sheet = getCharacterSheet(characterSheetId = characterSheetId) ?: return + val sheet = getCharacterSheet(characterSheetId = characterSheetId) _detailFlow.update(sheet.copy(damage = damage)) } @@ -241,7 +236,7 @@ class CharacterSheetStore( characterSheetId: String, diminished: Int, ) { - val sheet = getCharacterSheet(characterSheetId = characterSheetId) ?: return + val sheet = getCharacterSheet(characterSheetId = characterSheetId) _detailFlow.update(sheet.copy(diminished = diminished)) } @@ -249,18 +244,12 @@ class CharacterSheetStore( characterSheetId: String, fatigue: Int, ) { - val sheet = getCharacterSheet(characterSheetId = characterSheetId) ?: return + val sheet = getCharacterSheet(characterSheetId = characterSheetId) _detailFlow.update(sheet.copy(fatigue = fatigue)) } // endregion - private fun MutableStateFlow>.update( - previews: List, - ): List { - value = previews - return previews - } private fun MutableStateFlow>.update( sheet: CharacterSheet, diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/tag/TagRepository.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/tag/TagRepository.kt new file mode 100644 index 0000000..b44049c --- /dev/null +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/tag/TagRepository.kt @@ -0,0 +1,16 @@ +package com.pixelized.desktop.lwa.repository.tag + +import com.pixelized.shared.lwa.model.tag.Tag +import kotlinx.coroutines.flow.StateFlow + +class TagRepository( + private val store: TagStore, +) { + suspend fun updateCharacterTags() = store.updateCharacterTagsFlow() + + suspend fun updateAlterationTags() = store.updateAlterationTagsFlow() + + fun charactersTagFlow(): StateFlow> = store.charactersTagFlow() + + fun alterationsTagFlow(): StateFlow> = store.alterationsTagFlow() +} \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/tag/TagStore.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/tag/TagStore.kt new file mode 100644 index 0000000..3263260 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/tag/TagStore.kt @@ -0,0 +1,79 @@ +package com.pixelized.desktop.lwa.repository.tag + +import com.pixelized.desktop.lwa.network.LwaClient +import com.pixelized.shared.lwa.model.tag.Tag +import com.pixelized.shared.lwa.model.tag.TagJsonFactory +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow + +class TagStore( + private val factory: TagJsonFactory, + private val client: LwaClient, +) { + private val characterTagsFlow = MutableStateFlow>(emptyMap()) + private val alterationTagsFlow = MutableStateFlow>(emptyMap()) + + fun charactersTagFlow(): StateFlow> = characterTagsFlow + + fun characters(): Collection { + return characterTagsFlow.value.values + } + + fun character(tagId: String): Tag? { + return characterTagsFlow.value[tagId] + } + + fun alterationsTagFlow(): StateFlow> = alterationTagsFlow + + fun alterations(): Collection { + return alterationTagsFlow.value.values + } + + fun alteration(tagId: String): Tag? { + return alterationTagsFlow.value[tagId] + } + + suspend fun updateCharacterTagsFlow() { + characterTagsFlow.value = try { + getCharacterTag() + } catch (exception: Exception) { + println(exception.message) // TODO proper exception handling + emptyMap() + } + } + + suspend fun updateAlterationTagsFlow() { + alterationTagsFlow.value = try { + getAlterationTag() + } catch (exception: Exception) { + println(exception.message) // TODO proper exception handling + emptyMap() + } + } + + @Throws + private suspend fun getCharacterTag(): Map { + val request = client.getCharacterTags() + return when (request.success) { + true -> request.data + ?.map { factory.convertFromJson(json = it) } + ?.associateBy { it.id } + ?: emptyMap() + + else -> LwaClient.error(error = request) + } + } + + @Throws + private suspend fun getAlterationTag(): Map { + val request = client.getAlterationTags() + return when (request.success) { + true -> request.data + ?.map { factory.convertFromJson(json = it) } + ?.associateBy { it.id } + ?: emptyMap() + + else -> LwaClient.error(error = request) + } + } +} \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/composable/error/ErrorSnackUio.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/composable/error/ErrorSnackUio.kt index b45f07f..3b39cda 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/composable/error/ErrorSnackUio.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/composable/error/ErrorSnackUio.kt @@ -24,7 +24,7 @@ class ErrorSnackUio( } @Composable -fun ErrorSnack( +fun ErrorSnackHandler( snack: SnackbarHostState = LocalErrorSnackHost.current, error: SharedFlow, ) { diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/campaign/network/NetworkDialog.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/campaign/network/NetworkDialog.kt index 37d295d..a2a34ea 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/campaign/network/NetworkDialog.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/campaign/network/NetworkDialog.kt @@ -43,7 +43,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import com.pixelized.desktop.lwa.LocalSnackHost import com.pixelized.desktop.lwa.ui.composable.blur.BlurContent -import com.pixelized.desktop.lwa.ui.composable.error.ErrorSnack +import com.pixelized.desktop.lwa.ui.composable.error.ErrorSnackHandler import com.pixelized.desktop.lwa.ui.theme.lwa import kotlinx.coroutines.launch import lwacharactersheet.composeapp.generated.resources.Res @@ -148,7 +148,7 @@ fun NetworkDialog( } } - ErrorSnack( + ErrorSnackHandler( error = viewModel.networkError, ) } diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/alteration/edit/GMAlterationEditPage.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/alteration/edit/GMAlterationEditPage.kt index 84716ee..d0f2099 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/alteration/edit/GMAlterationEditPage.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/alteration/edit/GMAlterationEditPage.kt @@ -35,6 +35,7 @@ import androidx.compose.ui.input.key.KeyEventType import androidx.compose.ui.input.key.key import androidx.compose.ui.input.key.type import androidx.compose.ui.unit.dp +import com.pixelized.desktop.lwa.ui.composable.error.ErrorSnackHandler import com.pixelized.desktop.lwa.ui.composable.key.KeyHandler import com.pixelized.desktop.lwa.ui.composable.textfield.LwaTextField import com.pixelized.desktop.lwa.ui.composable.textfield.LwaTextFieldUio @@ -109,6 +110,10 @@ fun GMAlterationEditPage( } } + ErrorSnackHandler( + error = viewModel.error, + ) + AlterationEditKeyHandler( onDismissRequest = { screen.popBackStack() diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/alteration/edit/GMAlterationEditViewModel.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/alteration/edit/GMAlterationEditViewModel.kt index 6b706a1..c3a1d1f 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/alteration/edit/GMAlterationEditViewModel.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/alteration/edit/GMAlterationEditViewModel.kt @@ -4,8 +4,11 @@ import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.pixelized.desktop.lwa.repository.alteration.AlterationRepository +import com.pixelized.desktop.lwa.ui.composable.error.ErrorSnackUio import com.pixelized.desktop.lwa.ui.navigation.screen.destination.gamemaster.GMAlterationEditDestination +import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch @@ -20,6 +23,9 @@ class GMAlterationEditViewModel( private val _form = MutableStateFlow(null) val form: StateFlow get() = _form + private val _error = MutableSharedFlow() + val error: SharedFlow get() = _error + init { viewModelScope.launch { _form.value = factory.createForm( @@ -30,14 +36,22 @@ class GMAlterationEditViewModel( suspend fun save() { val edited = factory.createAlteration(form = form.value) - val actual = alterationRepository.alterationFlow.value[edited?.id] - // TODO if argument.id == null et actual?.id != null on créer et on écrase existant !!! + if (edited == null) return - if (edited != null) - - alterationRepository.updateAlteration(edited) + try { + if (argument.id == null && actual?.id != null) { + error("Id already taken by an another alteration") + } + alterationRepository.updateAlteration( + alteration = edited, + create = argument.id == null, + ) + } catch (exception: Exception) { + val message = ErrorSnackUio.from(exception = exception) + _error.emit(message) + } } suspend fun addField() { diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/alteration/list/GMAlterationViewModel.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/alteration/list/GMAlterationViewModel.kt index b12767d..d30b99c 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/alteration/list/GMAlterationViewModel.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/alteration/list/GMAlterationViewModel.kt @@ -3,6 +3,7 @@ package com.pixelized.desktop.lwa.ui.screen.gamemaster.alteration.list import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.pixelized.desktop.lwa.repository.alteration.AlterationRepository +import com.pixelized.desktop.lwa.repository.tag.TagRepository import com.pixelized.desktop.lwa.ui.composable.textfield.LwaTextFieldUio import com.pixelized.desktop.lwa.ui.screen.gamemaster.common.tag.GMTagFactory import com.pixelized.desktop.lwa.ui.screen.gamemaster.common.tag.GMTagUio @@ -22,6 +23,7 @@ import org.jetbrains.compose.resources.getString class GMAlterationViewModel( private val alterationRepository: AlterationRepository, alterationFactory: GMAlterationFactory, + tagRepository: TagRepository, tagFactory: GMTagFactory, ) : ViewModel() { @@ -38,7 +40,7 @@ class GMAlterationViewModel( ) val tags: StateFlow> = combine( - alterationRepository.tagsFlow, + tagRepository.alterationsTagFlow(), selectedTagId, ) { tags, selectedTagId -> tagFactory.convertToGMTagItemUio( @@ -53,7 +55,7 @@ class GMAlterationViewModel( val alterations: StateFlow> = combine( alterationRepository.alterationFlow, - alterationRepository.tagsFlow, + tagRepository.alterationsTagFlow(), filter.valueFlow.map { it.unAccent() }, selectedTagId, ) { alterations, tags, unAccentFilter, selectedTagId -> diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/character/list/GMCharacterViewModel.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/character/list/GMCharacterViewModel.kt index 212848f..162334b 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/character/list/GMCharacterViewModel.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/ui/screen/gamemaster/character/list/GMCharacterViewModel.kt @@ -5,6 +5,7 @@ import androidx.lifecycle.viewModelScope import com.pixelized.desktop.lwa.repository.campaign.CampaignRepository import com.pixelized.desktop.lwa.repository.characterSheet.CharacterSheetRepository import com.pixelized.desktop.lwa.repository.network.NetworkRepository +import com.pixelized.desktop.lwa.repository.tag.TagRepository import com.pixelized.desktop.lwa.ui.composable.textfield.LwaTextFieldUio import com.pixelized.desktop.lwa.ui.screen.gamemaster.character.list.GMCharacterItemUio.Action import com.pixelized.desktop.lwa.ui.screen.gamemaster.common.tag.GMTagFactory @@ -27,6 +28,7 @@ class GMCharacterViewModel( private val networkRepository: NetworkRepository, private val campaignRepository: CampaignRepository, characterSheetRepository: CharacterSheetRepository, + tagRepository: TagRepository, private val factory: GMCharacterFactory, private val tagFactory: GMTagFactory, ) : ViewModel() { @@ -44,7 +46,7 @@ class GMCharacterViewModel( ) val tags = combine( - characterSheetRepository.tagsFlow, + tagRepository.charactersTagFlow(), selectedTagId, ) { tags, selectedTagId -> tagFactory.convertToGMTagItemUio( diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/model/alteration/AlterationStore.kt b/server/src/main/kotlin/com/pixelized/server/lwa/model/alteration/AlterationStore.kt index 106f4be..0a155cd 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/model/alteration/AlterationStore.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/model/alteration/AlterationStore.kt @@ -39,7 +39,7 @@ class AlterationStore( alterationFlow.value = try { load() } catch (exception: Exception) { - println(exception) // TODO proper exception handling + println(exception.message) // TODO proper exception handling emptyList() } } diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/model/campaign/CampaignStore.kt b/server/src/main/kotlin/com/pixelized/server/lwa/model/campaign/CampaignStore.kt index 856b7ee..1baa868 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/model/campaign/CampaignStore.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/model/campaign/CampaignStore.kt @@ -38,7 +38,7 @@ class CampaignStore( campaignFlow.value = try { load() } catch (exception: Exception) { - println(exception) // TODO proper exception handling + println(exception.message) // TODO proper exception handling Campaign.empty() } } diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/model/character/CharacterSheetStore.kt b/server/src/main/kotlin/com/pixelized/server/lwa/model/character/CharacterSheetStore.kt index a3ee360..d804870 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/model/character/CharacterSheetStore.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/model/character/CharacterSheetStore.kt @@ -38,7 +38,7 @@ class CharacterSheetStore( characterSheetsFlow.value = try { load() } catch (exception: Exception) { - println(exception) // TODO proper exception handling + println(exception.message) // TODO proper exception handling emptyList() } } diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/model/tag/TagStore.kt b/server/src/main/kotlin/com/pixelized/server/lwa/model/tag/TagStore.kt index 137e4d0..5bbff0b 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/model/tag/TagStore.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/model/tag/TagStore.kt @@ -49,7 +49,7 @@ class TagStore( flow.value = try { file.readTags().associateBy { it.id } } catch (exception: Exception) { - println(exception) // TODO proper exception handling + println(exception.message) // TODO proper exception handling emptyMap() } } diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/Engine.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/Engine.kt index f59604f..fa178ec 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/Engine.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/Engine.kt @@ -24,7 +24,7 @@ class Engine( try { unSafeHandle(message) } catch (exception: Exception) { - println(exception) // TODO proper exception handling + println(exception.message) // TODO proper exception handling } } diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/alteration/DELETE_Alteration.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/alteration/DELETE_Alteration.kt index 4fd10d8..557ce43 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/alteration/DELETE_Alteration.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/alteration/DELETE_Alteration.kt @@ -3,7 +3,7 @@ package com.pixelized.server.lwa.server.rest.alteration import com.pixelized.server.lwa.server.Engine import com.pixelized.server.lwa.utils.extentions.MissingParameterException import com.pixelized.server.lwa.utils.extentions.alterationId -import com.pixelized.shared.lwa.protocol.rest.ResultJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import com.pixelized.shared.lwa.protocol.websocket.ApiSynchronisation import io.ktor.server.response.respond import io.ktor.server.routing.RoutingContext @@ -19,7 +19,7 @@ fun Engine.deleteAlteration(): suspend RoutingContext.() -> Unit { ) // API & WebSocket responses. call.respond( - message = ResultJson.Success(), + message = APIResponse.success(), ) webSocket.emit( value = ApiSynchronisation.AlterationDelete( @@ -29,15 +29,15 @@ fun Engine.deleteAlteration(): suspend RoutingContext.() -> Unit { ) } catch (exception: MissingParameterException) { call.respond( - message = ResultJson.Error( + message = APIResponse.error( status = exception.errorCode, message = exception.message ?: "?", ) ) } catch (exception: Exception) { call.respond( - message = ResultJson.Error( - status = ResultJson.Error.GENERIC, + message = APIResponse.error( + status = APIResponse.GENERIC, message = exception.message ?: "?", ) ) diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/alteration/GET_Alteration.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/alteration/GET_Alteration.kt index 443b39e..6a70c41 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/alteration/GET_Alteration.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/alteration/GET_Alteration.kt @@ -2,7 +2,7 @@ package com.pixelized.server.lwa.server.rest.alteration import com.pixelized.server.lwa.server.Engine import com.pixelized.server.lwa.utils.extentions.alterationId -import com.pixelized.shared.lwa.protocol.rest.ResultJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import io.ktor.server.response.respond import io.ktor.server.routing.RoutingContext @@ -17,12 +17,14 @@ fun Engine.getAlteration(): suspend RoutingContext.() -> Unit { ?: error("Alteration with id:$alterationId not found.") // send it back to the user. call.respond( - message = alteration, + message = APIResponse.success( + data = alteration + ) ) } catch (exception: Exception) { call.respond( - message = ResultJson.Error( - status = ResultJson.Error.GENERIC, + message = APIResponse.error( + status = APIResponse.GENERIC, message = exception.message ?: "?", ) ) diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/alteration/GET_AlterationTags.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/alteration/GET_AlterationTags.kt index 5639499..07edd33 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/alteration/GET_AlterationTags.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/alteration/GET_AlterationTags.kt @@ -1,7 +1,7 @@ package com.pixelized.server.lwa.server.rest.alteration import com.pixelized.server.lwa.server.Engine -import com.pixelized.shared.lwa.protocol.rest.ResultJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import io.ktor.server.response.respond import io.ktor.server.routing.RoutingContext @@ -9,12 +9,14 @@ fun Engine.getAlterationTags(): suspend RoutingContext.() -> Unit { return { try { call.respond( - message = alterationService.tags(), + message = APIResponse.success( + data = alterationService.tags(), + ), ) } catch (exception: Exception) { call.respond( - message = ResultJson.Error( - status = ResultJson.Error.GENERIC, + message = APIResponse.error( + status = APIResponse.GENERIC, message = exception.message ?: "?", ) ) diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/alteration/GET_Alterations.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/alteration/GET_Alterations.kt index 10cf56f..6022814 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/alteration/GET_Alterations.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/alteration/GET_Alterations.kt @@ -1,7 +1,7 @@ package com.pixelized.server.lwa.server.rest.alteration import com.pixelized.server.lwa.server.Engine -import com.pixelized.shared.lwa.protocol.rest.ResultJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import io.ktor.server.response.respond import io.ktor.server.routing.RoutingContext @@ -9,12 +9,14 @@ fun Engine.getAlterations(): suspend RoutingContext.() -> Unit { return { try { call.respond( - message = alterationService.alterations(), + message = APIResponse.success( + data = alterationService.alterations(), + ) ) } catch (exception: Exception) { call.respond( - message = ResultJson.Error( - status = ResultJson.Error.GENERIC, + message = APIResponse.error( + status = APIResponse.GENERIC, message = exception.message ?: "?", ) ) diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/alteration/PUT_Alateration.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/alteration/PUT_Alateration.kt index 1438132..da2e233 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/alteration/PUT_Alateration.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/alteration/PUT_Alateration.kt @@ -4,7 +4,7 @@ import com.pixelized.server.lwa.server.Engine import com.pixelized.server.lwa.utils.extentions.MissingParameterException import com.pixelized.server.lwa.utils.extentions.create import com.pixelized.shared.lwa.model.alteration.AlterationJson -import com.pixelized.shared.lwa.protocol.rest.ResultJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import com.pixelized.shared.lwa.protocol.websocket.ApiSynchronisation import io.ktor.server.request.receive import io.ktor.server.response.respond @@ -21,7 +21,7 @@ fun Engine.putAlteration(): suspend RoutingContext.() -> Unit { create = create, ) call.respond( - message = ResultJson.Success(), + message = APIResponse.success(), ) webSocket.emit( value = ApiSynchronisation.AlterationUpdate( @@ -31,15 +31,15 @@ fun Engine.putAlteration(): suspend RoutingContext.() -> Unit { ) } catch (exception: MissingParameterException) { call.respond( - message = ResultJson.Error( + message = APIResponse.error( status = exception.errorCode, message = exception.message ?: "?", ) ) } catch (exception: Exception) { call.respond( - message = ResultJson.Error( - status = ResultJson.Error.GENERIC, + message = APIResponse.error( + status = APIResponse.GENERIC, message = exception.message ?: "?", ) ) diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/DELETE_Campaign_Character.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/DELETE_Campaign_Character.kt index 9159681..a2e1a2d 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/DELETE_Campaign_Character.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/DELETE_Campaign_Character.kt @@ -3,7 +3,7 @@ package com.pixelized.server.lwa.server.rest.campaign import com.pixelized.server.lwa.server.Engine import com.pixelized.server.lwa.utils.extentions.MissingParameterException import com.pixelized.server.lwa.utils.extentions.characterSheetId -import com.pixelized.shared.lwa.protocol.rest.ResultJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import com.pixelized.shared.lwa.protocol.websocket.CampaignEvent import io.ktor.server.response.respond import io.ktor.server.routing.RoutingContext @@ -19,7 +19,7 @@ fun Engine.removeCampaignCharacter(): suspend RoutingContext.() -> Unit { ) // API & WebSocket responses call.respond( - message = ResultJson.Success(), + message = APIResponse.success(), ) webSocket.emit( value = CampaignEvent.CharacterRemoved( @@ -29,15 +29,15 @@ fun Engine.removeCampaignCharacter(): suspend RoutingContext.() -> Unit { ) } catch (exception: MissingParameterException) { call.respond( - message = ResultJson.Error( + message = APIResponse.error( status = exception.errorCode, message = exception.message ?: "?", ) ) } catch (exception: Exception) { call.respond( - message = ResultJson.Error( - status = ResultJson.Error.GENERIC, + message = APIResponse.error( + status = APIResponse.GENERIC, message = exception.message ?: "?", ) ) diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/DELETE_Campaign_Npc.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/DELETE_Campaign_Npc.kt index 9c6837c..336e562 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/DELETE_Campaign_Npc.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/DELETE_Campaign_Npc.kt @@ -3,7 +3,7 @@ package com.pixelized.server.lwa.server.rest.campaign import com.pixelized.server.lwa.server.Engine import com.pixelized.server.lwa.utils.extentions.MissingParameterException import com.pixelized.server.lwa.utils.extentions.characterSheetId -import com.pixelized.shared.lwa.protocol.rest.ResultJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import com.pixelized.shared.lwa.protocol.websocket.CampaignEvent import io.ktor.server.response.respond import io.ktor.server.routing.RoutingContext @@ -19,7 +19,7 @@ fun Engine.removeCampaignNpc(): suspend RoutingContext.() -> Unit { ) // API & WebSocket responses call.respond( - message = ResultJson.Success(), + message = APIResponse.success(), ) webSocket.emit( value = CampaignEvent.NpcRemoved( @@ -29,15 +29,15 @@ fun Engine.removeCampaignNpc(): suspend RoutingContext.() -> Unit { ) } catch (exception: MissingParameterException) { call.respond( - message = ResultJson.Error( + message = APIResponse.error( status = exception.errorCode, message = exception.message ?: "?", ) ) } catch (exception: Exception) { call.respond( - message = ResultJson.Error( - status = ResultJson.Error.GENERIC, + message = APIResponse.error( + status = APIResponse.GENERIC, message = exception.message ?: "?", ) ) diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/GET_Campaign.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/GET_Campaign.kt index 63efe2e..f345e13 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/GET_Campaign.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/GET_Campaign.kt @@ -1,7 +1,7 @@ package com.pixelized.server.lwa.server.rest.campaign import com.pixelized.server.lwa.server.Engine -import com.pixelized.shared.lwa.protocol.rest.ResultJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import io.ktor.server.response.respond import io.ktor.server.routing.RoutingContext @@ -9,12 +9,14 @@ fun Engine.getCampaign(): suspend RoutingContext.() -> Unit { return { try { call.respond( - message = campaignService.campaignJson(), + message = APIResponse.success( + data = campaignService.campaignJson(), + ), ) } catch (exception: Exception) { call.respond( - message = ResultJson.Error( - status = ResultJson.Error.GENERIC, + message = APIResponse.error( + status = APIResponse.GENERIC, message = exception.message ?: "?", ) ) diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/PUT_Campaign_Character.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/PUT_Campaign_Character.kt index c76a380..80f012a 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/PUT_Campaign_Character.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/PUT_Campaign_Character.kt @@ -3,7 +3,7 @@ package com.pixelized.server.lwa.server.rest.campaign import com.pixelized.server.lwa.server.Engine import com.pixelized.server.lwa.utils.extentions.MissingParameterException import com.pixelized.server.lwa.utils.extentions.characterSheetId -import com.pixelized.shared.lwa.protocol.rest.ResultJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import com.pixelized.shared.lwa.protocol.websocket.CampaignEvent import io.ktor.server.response.respond import io.ktor.server.routing.RoutingContext @@ -19,7 +19,7 @@ fun Engine.putCampaignCharacter(): suspend RoutingContext.() -> Unit { ) // API & WebSocket responses. call.respond( - message = ResultJson.Success(), + message = APIResponse.success(), ) webSocket.emit( value = CampaignEvent.CharacterAdded( @@ -29,15 +29,15 @@ fun Engine.putCampaignCharacter(): suspend RoutingContext.() -> Unit { ) } catch (exception: MissingParameterException) { call.respond( - message = ResultJson.Error( + message = APIResponse.error( status = exception.errorCode, message = exception.message ?: "?", ) ) } catch (exception: Exception) { call.respond( - message = ResultJson.Error( - status = ResultJson.Error.GENERIC, + message = APIResponse.error( + status = APIResponse.GENERIC, message = exception.message ?: "?", ) ) diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/PUT_Campaign_Npc.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/PUT_Campaign_Npc.kt index 7fe30c6..07dfd5d 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/PUT_Campaign_Npc.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/PUT_Campaign_Npc.kt @@ -3,7 +3,7 @@ package com.pixelized.server.lwa.server.rest.campaign import com.pixelized.server.lwa.server.Engine import com.pixelized.server.lwa.utils.extentions.MissingParameterException import com.pixelized.server.lwa.utils.extentions.characterSheetId -import com.pixelized.shared.lwa.protocol.rest.ResultJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import com.pixelized.shared.lwa.protocol.websocket.CampaignEvent import io.ktor.server.response.respond import io.ktor.server.routing.RoutingContext @@ -19,7 +19,7 @@ fun Engine.putCampaignNpc(): suspend RoutingContext.() -> Unit { ) // API & WebSocket responses. call.respond( - message = ResultJson.Success(), + message = APIResponse.success(), ) webSocket.emit( value = CampaignEvent.NpcAdded( @@ -29,15 +29,15 @@ fun Engine.putCampaignNpc(): suspend RoutingContext.() -> Unit { ) } catch (exception: MissingParameterException) { call.respond( - message = ResultJson.Error( + message = APIResponse.error( status = exception.errorCode, message = exception.message ?: "?", ) ) } catch (exception: Exception) { call.respond( - message = ResultJson.Error( - status = ResultJson.Error.GENERIC, + message = APIResponse.error( + status = APIResponse.GENERIC, message = exception.message ?: "?", ) ) diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/PUT_Campaign_Scene_Name.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/PUT_Campaign_Scene_Name.kt index 0efc8b7..e2e37bb 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/PUT_Campaign_Scene_Name.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/campaign/PUT_Campaign_Scene_Name.kt @@ -3,7 +3,7 @@ package com.pixelized.server.lwa.server.rest.campaign import com.pixelized.server.lwa.server.Engine import com.pixelized.server.lwa.utils.extentions.MissingParameterException import com.pixelized.shared.lwa.model.campaign.CampaignJsonV2 -import com.pixelized.shared.lwa.protocol.rest.ResultJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import com.pixelized.shared.lwa.protocol.websocket.CampaignEvent import io.ktor.server.request.receive import io.ktor.server.response.respond @@ -17,10 +17,12 @@ fun Engine.putCampaignScene(): suspend RoutingContext.() -> Unit { // convert the scene into the a usable data model. val scene = campaignJsonFactory.convertFromJson(json = form) // update the campaign. - campaignService.setScene(scene = scene) + campaignService.setScene( + scene = scene, + ) // API & WebSocket responses call.respond( - message = ResultJson.Success(), + message = APIResponse.success(), ) webSocket.emit( value = CampaignEvent.UpdateScene( @@ -30,15 +32,15 @@ fun Engine.putCampaignScene(): suspend RoutingContext.() -> Unit { ) } catch (exception: MissingParameterException) { call.respond( - message = ResultJson.Error( + message = APIResponse.error( status = exception.errorCode, message = exception.message ?: "?", ) ) } catch (exception: Exception) { call.respond( - message = ResultJson.Error( - status = ResultJson.Error.GENERIC, + message = APIResponse.error( + status = APIResponse.GENERIC, message = exception.message ?: "?", ) ) diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/DELETE_Character.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/DELETE_Character.kt index 01be69c..42629b4 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/DELETE_Character.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/DELETE_Character.kt @@ -3,7 +3,7 @@ package com.pixelized.server.lwa.server.rest.character import com.pixelized.server.lwa.server.Engine import com.pixelized.server.lwa.utils.extentions.MissingParameterException import com.pixelized.server.lwa.utils.extentions.characterSheetId -import com.pixelized.shared.lwa.protocol.rest.ResultJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import com.pixelized.shared.lwa.protocol.websocket.ApiSynchronisation import io.ktor.server.response.respond import io.ktor.server.routing.RoutingContext @@ -19,7 +19,7 @@ fun Engine.deleteCharacter(): suspend RoutingContext.() -> Unit { ) // API & WebSocket responses. call.respond( - message = ResultJson.Success() + message = APIResponse.success() ) webSocket.emit( value = ApiSynchronisation.CharacterSheetDelete( @@ -29,15 +29,15 @@ fun Engine.deleteCharacter(): suspend RoutingContext.() -> Unit { ) } catch (exception: MissingParameterException) { call.respond( - message = ResultJson.Error( + message = APIResponse.error( status = exception.errorCode, message = exception.message ?: "?", ) ) } catch (exception: Exception) { call.respond( - message = ResultJson.Error( - status = ResultJson.Error.GENERIC, + message = APIResponse.error( + status = APIResponse.GENERIC, message = exception.message ?: "?", ) ) diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/GET_Character.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/GET_Character.kt index dd8a726..200bba4 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/GET_Character.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/GET_Character.kt @@ -3,7 +3,7 @@ package com.pixelized.server.lwa.server.rest.character import com.pixelized.server.lwa.server.Engine import com.pixelized.server.lwa.utils.extentions.MissingParameterException import com.pixelized.server.lwa.utils.extentions.characterSheetId -import com.pixelized.shared.lwa.protocol.rest.ResultJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import io.ktor.server.response.respond import io.ktor.server.routing.RoutingContext @@ -18,19 +18,21 @@ fun Engine.getCharacter(): suspend RoutingContext.() -> Unit { ?: error("CharacterSheet with id:$characterSheetId not found.") // send it back to the user. call.respond( - message = characterSheet, + message = APIResponse.success( + data = characterSheet, + ), ) } catch (exception: MissingParameterException) { call.respond( - message = ResultJson.Error( + message = APIResponse.error( status = exception.errorCode, message = exception.message ?: "?", ) ) } catch (exception: Exception) { call.respond( - message = ResultJson.Error( - status = ResultJson.Error.GENERIC, + message = APIResponse.error( + status = APIResponse.GENERIC, message = exception.message ?: "?", ) ) diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/GET_CharacterTags.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/GET_CharacterTags.kt index 81607df..52b5c3f 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/GET_CharacterTags.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/GET_CharacterTags.kt @@ -1,7 +1,7 @@ package com.pixelized.server.lwa.server.rest.character import com.pixelized.server.lwa.server.Engine -import com.pixelized.shared.lwa.protocol.rest.ResultJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import io.ktor.server.response.respond import io.ktor.server.routing.RoutingContext @@ -9,12 +9,14 @@ fun Engine.getCharacterTags(): suspend RoutingContext.() -> Unit { return { try { call.respond( - message = characterService.tags(), + message = APIResponse.success( + data = characterService.tags(), + ), ) } catch (exception: Exception) { call.respond( - message = ResultJson.Error( - status = ResultJson.Error.GENERIC, + message = APIResponse.error( + status = APIResponse.GENERIC, message = exception.message ?: "?", ) ) diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/GET_Characters.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/GET_Characters.kt index 8e8fab6..354b4ee 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/GET_Characters.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/GET_Characters.kt @@ -1,7 +1,7 @@ package com.pixelized.server.lwa.server.rest.character import com.pixelized.server.lwa.server.Engine -import com.pixelized.shared.lwa.protocol.rest.ResultJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import io.ktor.server.response.respond import io.ktor.server.routing.RoutingContext @@ -9,12 +9,14 @@ fun Engine.getCharacters(): suspend RoutingContext.() -> Unit { return { try { call.respond( - message = characterService.charactersJson(), + message = APIResponse.success( + data = characterService.charactersJson(), + ), ) } catch (exception: Exception) { call.respond( - message = ResultJson.Error( - status = ResultJson.Error.GENERIC, + message = APIResponse.error( + status = APIResponse.GENERIC, message = exception.message ?: "?", ) ) diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/PUT_Character.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/PUT_Character.kt index 5f8d4eb..dbc0f21 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/PUT_Character.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/PUT_Character.kt @@ -4,7 +4,7 @@ import com.pixelized.server.lwa.server.Engine import com.pixelized.server.lwa.utils.extentions.MissingParameterException import com.pixelized.server.lwa.utils.extentions.create import com.pixelized.shared.lwa.model.characterSheet.CharacterSheetJson -import com.pixelized.shared.lwa.protocol.rest.ResultJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import com.pixelized.shared.lwa.protocol.websocket.ApiSynchronisation import io.ktor.server.request.receive import io.ktor.server.response.respond @@ -20,7 +20,7 @@ fun Engine.putCharacter(): suspend RoutingContext.() -> Unit { create = create, ) call.respond( - message = ResultJson.Success() + message = APIResponse.success() ) webSocket.emit( value = ApiSynchronisation.CharacterSheetUpdate( @@ -30,15 +30,15 @@ fun Engine.putCharacter(): suspend RoutingContext.() -> Unit { ) } catch (exception: MissingParameterException) { call.respond( - message = ResultJson.Error( + message = APIResponse.error( status = exception.errorCode, message = exception.message ?: "?", ) ) } catch (exception: Exception) { call.respond( - message = ResultJson.Error( - status = ResultJson.Error.GENERIC, + message = APIResponse.error( + status = APIResponse.GENERIC, message = exception.message ?: "?", ) ) diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/PUT_Character_Alteration.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/PUT_Character_Alteration.kt index 6e0f399..a6fd05b 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/PUT_Character_Alteration.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/PUT_Character_Alteration.kt @@ -5,7 +5,7 @@ import com.pixelized.server.lwa.utils.extentions.MissingParameterException import com.pixelized.server.lwa.utils.extentions.active import com.pixelized.server.lwa.utils.extentions.alterationId import com.pixelized.server.lwa.utils.extentions.characterSheetId -import com.pixelized.shared.lwa.protocol.rest.ResultJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import com.pixelized.shared.lwa.protocol.websocket.CharacterSheetEvent import io.ktor.server.response.respond import io.ktor.server.routing.RoutingContext @@ -25,7 +25,7 @@ fun Engine.putCharacterAlteration(): suspend RoutingContext.() -> Unit { ) // API & WebSocket responses. call.respond( - message = ResultJson.Success() + message = APIResponse.success() ) webSocket.emit( value = CharacterSheetEvent.UpdateAlteration( @@ -37,15 +37,15 @@ fun Engine.putCharacterAlteration(): suspend RoutingContext.() -> Unit { ) } catch (exception: MissingParameterException) { call.respond( - message = ResultJson.Error( + message = APIResponse.error( status = exception.errorCode, message = exception.message ?: "?", ) ) } catch (exception: Exception) { call.respond( - message = ResultJson.Error( - status = ResultJson.Error.GENERIC, + message = APIResponse.error( + status = APIResponse.GENERIC, message = exception.message ?: "?", ) ) diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/PUT_Character_Damage.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/PUT_Character_Damage.kt index 831b2d8..427c4be 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/PUT_Character_Damage.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/PUT_Character_Damage.kt @@ -3,7 +3,7 @@ package com.pixelized.server.lwa.server.rest.character import com.pixelized.server.lwa.server.Engine import com.pixelized.server.lwa.utils.extentions.MissingParameterException import com.pixelized.server.lwa.utils.extentions.characterSheetId -import com.pixelized.shared.lwa.protocol.rest.ResultJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import com.pixelized.shared.lwa.protocol.websocket.CharacterSheetEvent import io.ktor.server.response.respond import io.ktor.server.routing.RoutingContext @@ -16,7 +16,7 @@ fun Engine.putCharacterDamage(): suspend RoutingContext.() -> Unit { val damage = call.queryParameters["damage"]?.toIntOrNull() ?: throw MissingParameterException( name = "damage", - errorCode = ResultJson.Error.MISSING_DAMAGE + errorCode = APIResponse.MISSING_DAMAGE ) // fetch the character sheet val characterSheet = characterService.character(characterSheetId) @@ -28,7 +28,7 @@ fun Engine.putCharacterDamage(): suspend RoutingContext.() -> Unit { ) // API & WebSocket responses. call.respond( - message = ResultJson.Success() + message = APIResponse.success() ) webSocket.emit( value = CharacterSheetEvent.UpdateDamage( @@ -40,15 +40,15 @@ fun Engine.putCharacterDamage(): suspend RoutingContext.() -> Unit { ) } catch (exception: MissingParameterException) { call.respond( - message = ResultJson.Error( + message = APIResponse.error( status = exception.errorCode, message = exception.message ?: "?", ) ) } catch (exception: Exception) { call.respond( - message = ResultJson.Error( - status = ResultJson.Error.GENERIC, + message = APIResponse.error( + status = APIResponse.GENERIC, message = exception.message ?: "?", ) ) diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/PUT_Character_Diminished.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/PUT_Character_Diminished.kt index 7f8426e..b695e26 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/PUT_Character_Diminished.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/PUT_Character_Diminished.kt @@ -3,7 +3,7 @@ package com.pixelized.server.lwa.server.rest.character import com.pixelized.server.lwa.server.Engine import com.pixelized.server.lwa.utils.extentions.MissingParameterException import com.pixelized.server.lwa.utils.extentions.characterSheetId -import com.pixelized.shared.lwa.protocol.rest.ResultJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import com.pixelized.shared.lwa.protocol.websocket.CharacterSheetEvent import io.ktor.server.response.respond import io.ktor.server.routing.RoutingContext @@ -16,7 +16,7 @@ fun Engine.putCharacterDiminished(): suspend RoutingContext.() -> Unit { val diminished = call.queryParameters["diminished"]?.toIntOrNull() ?: throw MissingParameterException( name = "diminished", - errorCode = ResultJson.Error.MISSING_DIMINISHED + errorCode = APIResponse.MISSING_DIMINISHED ) // Update the character damage characterService.updateDiminished( @@ -25,7 +25,7 @@ fun Engine.putCharacterDiminished(): suspend RoutingContext.() -> Unit { ) // API & WebSocket responses. call.respond( - message = ResultJson.Success() + message = APIResponse.success() ) webSocket.emit( value = CharacterSheetEvent.UpdateDiminished( @@ -36,15 +36,15 @@ fun Engine.putCharacterDiminished(): suspend RoutingContext.() -> Unit { ) } catch (exception: MissingParameterException) { call.respond( - message = ResultJson.Error( + message = APIResponse.error( status = exception.errorCode, message = exception.message ?: "?", ) ) } catch (exception: Exception) { call.respond( - message = ResultJson.Error( - status = ResultJson.Error.GENERIC, + message = APIResponse.error( + status = APIResponse.GENERIC, message = exception.message ?: "?", ) ) diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/PUT_Character_Fatigue.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/PUT_Character_Fatigue.kt index 62431ca..6f565cc 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/PUT_Character_Fatigue.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/character/PUT_Character_Fatigue.kt @@ -3,7 +3,7 @@ package com.pixelized.server.lwa.server.rest.character import com.pixelized.server.lwa.server.Engine import com.pixelized.server.lwa.utils.extentions.MissingParameterException import com.pixelized.server.lwa.utils.extentions.characterSheetId -import com.pixelized.shared.lwa.protocol.rest.ResultJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import com.pixelized.shared.lwa.protocol.websocket.CharacterSheetEvent import io.ktor.server.response.respond import io.ktor.server.routing.RoutingContext @@ -16,7 +16,7 @@ fun Engine.putCharacterFatigue(): suspend RoutingContext.() -> Unit { val fatigue = call.queryParameters["fatigue"]?.toIntOrNull() ?: throw MissingParameterException( name = "fatigue", - errorCode = ResultJson.Error.MISSING_FATIGUE + errorCode = APIResponse.MISSING_FATIGUE ) // fetch the character sheet val characterSheet = characterService.character(characterSheetId) @@ -28,7 +28,7 @@ fun Engine.putCharacterFatigue(): suspend RoutingContext.() -> Unit { ) // API & WebSocket responses. call.respond( - message = ResultJson.Success() + message = APIResponse.success() ) webSocket.emit( value = CharacterSheetEvent.UpdateFatigue( @@ -40,15 +40,15 @@ fun Engine.putCharacterFatigue(): suspend RoutingContext.() -> Unit { ) } catch (exception: MissingParameterException) { call.respond( - message = ResultJson.Error( + message = APIResponse.error( status = exception.errorCode, message = exception.message ?: "?", ) ) } catch (exception: Exception) { call.respond( - message = ResultJson.Error( - status = ResultJson.Error.GENERIC, + message = APIResponse.error( + status = APIResponse.GENERIC, message = exception.message ?: "?", ) ) diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/utils/extentions/ParametersExt.kt b/server/src/main/kotlin/com/pixelized/server/lwa/utils/extentions/ParametersExt.kt index ac119ad..b719213 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/utils/extentions/ParametersExt.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/utils/extentions/ParametersExt.kt @@ -1,13 +1,13 @@ package com.pixelized.server.lwa.utils.extentions -import com.pixelized.shared.lwa.protocol.rest.ResultJson +import com.pixelized.shared.lwa.protocol.rest.APIResponse import io.ktor.http.Parameters val Parameters.characterSheetId get() = "characterSheetId".let { param -> this[param] ?: throw MissingParameterException( name = param, - errorCode = ResultJson.Error.MISSING_CHARACTER_SHEET_ID, + errorCode = APIResponse.MISSING_CHARACTER_SHEET_ID, ) } @@ -15,7 +15,7 @@ val Parameters.alterationId get() = "alterationId".let { param -> this[param] ?: throw MissingParameterException( name = param, - errorCode = ResultJson.Error.MISSING_ALTERATION_ID, + errorCode = APIResponse.MISSING_ALTERATION_ID, ) } @@ -23,7 +23,7 @@ val Parameters.create get() = "create".let { param -> this[param]?.toBooleanStrictOrNull() ?: throw MissingParameterException( name = param, - errorCode = ResultJson.Error.MISSING_CREATE + errorCode = APIResponse.MISSING_CREATE ) } @@ -31,7 +31,7 @@ val Parameters.active get() = "active".let { param -> this[param]?.toBooleanStrictOrNull() ?: throw MissingParameterException( name = param, - errorCode = ResultJson.Error.MISSING_ACTIVE + errorCode = APIResponse.MISSING_ACTIVE ) } diff --git a/shared/src/commonMain/kotlin/com/pixelized/shared/lwa/protocol/rest/APIResponse.kt b/shared/src/commonMain/kotlin/com/pixelized/shared/lwa/protocol/rest/APIResponse.kt new file mode 100644 index 0000000..f5893b4 --- /dev/null +++ b/shared/src/commonMain/kotlin/com/pixelized/shared/lwa/protocol/rest/APIResponse.kt @@ -0,0 +1,52 @@ +package com.pixelized.shared.lwa.protocol.rest + +import kotlinx.serialization.Serializable + +@Serializable +data class APIResponse( + val success: Boolean, + val status: Int, + val message: String?, + val data: T?, +) { + companion object { + const val SUCCESS = 100 + + const val GENERIC = 600 + + const val MISSING_PARAMETER = 700 + const val MISSING_CHARACTER_SHEET_ID = MISSING_PARAMETER + 1 + const val MISSING_ALTERATION_ID = MISSING_PARAMETER + 2 + const val MISSING_CREATE = MISSING_PARAMETER + 3 + const val MISSING_ACTIVE = MISSING_PARAMETER + 4 + const val MISSING_DAMAGE = MISSING_PARAMETER + 5 + const val MISSING_FATIGUE = MISSING_PARAMETER + 6 + const val MISSING_DIMINISHED = MISSING_PARAMETER + 7 + + fun error( + status: Int, + message: String?, + ) = APIResponse( + success = false, + status = status, + message = message, + data = null, + ) + + fun success() = APIResponse( + success = true, + status = SUCCESS, + message = null, + data = null, + ) + + inline fun success( + data: T? = null, + ) = APIResponse( + success = true, + status = SUCCESS, + message = null, + data = data, + ) + } +} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/com/pixelized/shared/lwa/protocol/rest/ResultJson.kt b/shared/src/commonMain/kotlin/com/pixelized/shared/lwa/protocol/rest/ResultJson.kt deleted file mode 100644 index 2b4ebc5..0000000 --- a/shared/src/commonMain/kotlin/com/pixelized/shared/lwa/protocol/rest/ResultJson.kt +++ /dev/null @@ -1,34 +0,0 @@ -package com.pixelized.shared.lwa.protocol.rest - -import kotlinx.serialization.Serializable - -@Serializable -sealed interface ResultJson { - val success: Boolean - - @Serializable - data class Error( - override val success: Boolean = false, - val status: Int, - val message: String, - ) : ResultJson { - companion object { - const val GENERIC = 600 - - const val MISSING_PARAMETER = 700 - const val MISSING_CHARACTER_SHEET_ID = MISSING_PARAMETER + 1 - const val MISSING_ALTERATION_ID = MISSING_PARAMETER + 2 - const val MISSING_CREATE = MISSING_PARAMETER + 3 - const val MISSING_ACTIVE = MISSING_PARAMETER + 4 - const val MISSING_DAMAGE = MISSING_PARAMETER + 5 - const val MISSING_FATIGUE = MISSING_PARAMETER + 6 - const val MISSING_DIMINISHED = MISSING_PARAMETER + 7 - } - } - - @Serializable - data class Success( - override val success: Boolean = true, - val status: Int = 100, - ) : ResultJson -} \ No newline at end of file