From d806048510de1b03651101db32c501963c5120e1 Mon Sep 17 00:00:00 2001 From: Thomas Andres Gomez Date: Sun, 30 Mar 2025 21:17:17 +0200 Subject: [PATCH] Server : Character error management --- .../com/pixelized/server/lwa/Application.kt | 3 - .../lwa/model/alteration/AlterationStore.kt | 31 ++++-- .../lwa/model/campaign/CampaignStore.kt | 1 - .../model/character/CharacterSheetService.kt | 48 ++++++-- .../model/character/CharacterSheetStore.kt | 105 ++++++++++++------ .../com/pixelized/server/lwa/server/Engine.kt | 28 ++++- .../com/pixelized/server/lwa/server/Server.kt | 2 +- .../rest/alteration/DELETE_Alteration.kt | 4 +- .../server/rest/alteration/GET_Alteration.kt | 3 +- .../rest/alteration/GET_AlterationTags.kt | 3 +- .../server/rest/alteration/GET_Alterations.kt | 3 +- .../server/rest/alteration/PUT_Alateration.kt | 4 +- .../campaign/DELETE_Campaign_Character.kt | 3 +- .../rest/campaign/DELETE_Campaign_Npc.kt | 5 +- .../lwa/server/rest/campaign/GET_Campaign.kt | 3 +- .../rest/campaign/PUT_Campaign_Character.kt | 3 +- .../server/rest/campaign/PUT_Campaign_Npc.kt | 3 +- .../rest/campaign/PUT_Campaign_Scene_Name.kt | 5 +- .../server/rest/character/DELETE_Character.kt | 45 +++++--- .../server/rest/character/GET_Character.kt | 22 +++- .../rest/character/GET_CharacterTags.kt | 19 +++- .../server/rest/character/GET_Characters.kt | 19 +++- .../server/rest/character/PUT_Character.kt | 33 ++++-- .../character/PUT_Character_Alteration.kt | 32 ++++-- .../rest/character/PUT_Character_Damage.kt | 35 ++++-- .../character/PUT_Character_Diminished.kt | 33 ++++-- .../rest/character/PUT_Character_Fatigue.kt | 35 ++++-- .../lwa/utils/extentions/ParametersExt.kt | 29 +++-- .../shared/lwa/protocol/rest/ResultJson.kt | 4 + 29 files changed, 388 insertions(+), 175 deletions(-) diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/Application.kt b/server/src/main/kotlin/com/pixelized/server/lwa/Application.kt index cff6e7c..70e6e5e 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/Application.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/Application.kt @@ -1,9 +1,6 @@ package com.pixelized.server.lwa import com.pixelized.server.lwa.server.LocalServer -import com.pixelized.shared.lwa.sharedModuleDependencies -import org.koin.core.context.startKoin -import org.koin.java.KoinJavaComponent.inject fun main() { LocalServer().create().start() 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 1eba998..106f4be 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 @@ -13,6 +13,7 @@ import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import kotlinx.serialization.json.Json import java.io.File +import java.text.Collator class AlterationStore( private val pathProvider: PathProvider, @@ -75,6 +76,7 @@ class AlterationStore( throw JsonConversionException(root = exception) } } + ?.sortedWith(compareBy(Collator.getInstance()) { it.metadata.name }) ?: emptyList() } @@ -117,15 +119,18 @@ class AlterationStore( } // Update the dataflow. alterationFlow.update { alterations -> - val index = alterations.indexOfFirst { - it.id == json.id - } - alterations.toMutableList().also { - when { - index >= 0 -> it[index] = alteration - else -> it.add(alteration) + val index = alterations.indexOfFirst { it.id == json.id } + alterations.toMutableList() + .also { + if (index >= 0) { + it[index] = alteration + } else { + it.add(alteration) + } } - } + .sortedWith(compareBy(Collator.getInstance()) { + it.metadata.name + }) } } @@ -144,9 +149,13 @@ class AlterationStore( } // Update the data model with the deleted alteration. alterationFlow.update { alterations -> - alterations.toMutableList().also { alteration -> - alteration.removeIf { it.id == id } - } + alterations.toMutableList() + .also { alteration -> + alteration.removeIf { it.id == id } + } + .sortedWith(compareBy(Collator.getInstance()) { + it.metadata.name + }) } } 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 16fd568..856b7ee 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 @@ -1,6 +1,5 @@ package com.pixelized.server.lwa.model.campaign -import com.pixelized.server.lwa.model.alteration.AlterationStore.AlterationStoreException import com.pixelized.shared.lwa.model.campaign.Campaign import com.pixelized.shared.lwa.model.campaign.CampaignJson import com.pixelized.shared.lwa.model.campaign.factory.CampaignJsonFactory diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/model/character/CharacterSheetService.kt b/server/src/main/kotlin/com/pixelized/server/lwa/model/character/CharacterSheetService.kt index 5222b7d..57d7b92 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/model/character/CharacterSheetService.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/model/character/CharacterSheetService.kt @@ -19,7 +19,9 @@ class CharacterSheetService( private val factory: CharacterSheetJsonFactory, ) { private val scope = CoroutineScope(Dispatchers.IO + Job()) + private val sheets get() = sheetsFlow.value + private val sheetsFlow = characterStore.characterSheetsFlow() .map { entry -> entry.associateBy { character -> character.id } } .stateIn( @@ -40,7 +42,7 @@ class CharacterSheetService( return sheets[characterSheetId] } - fun tags() : Collection { + fun tags(): Collection { return alterationTags.value } @@ -52,14 +54,24 @@ class CharacterSheetService( return sheets[characterSheetId]?.let(factory::convertToJson) } - suspend fun updateCharacterSheet(character: CharacterSheetJson) { + @Throws + suspend fun save( + character: CharacterSheetJson, + create: Boolean, + ) { return characterStore.save( - sheet = factory.convertFromJson(character) + sheet = factory.convertFromJson(character), + create = create, ) } - fun deleteCharacterSheet(characterSheetId: String): Boolean { - return characterStore.delete(id = characterSheetId) + @Throws + fun deleteCharacterSheet( + characterSheetId: String, + ) { + characterStore.delete( + characterSheetId = characterSheetId + ) } // Data manipulation through WebSocket. @@ -78,7 +90,8 @@ class CharacterSheetService( characterStore.save( sheet = character.copy( alterations = alterations, - ) + ), + create = false, ) } if (active.not() && contain) { @@ -88,7 +101,8 @@ class CharacterSheetService( characterStore.save( sheet = character.copy( alterations = alterations, - ) + ), + create = false, ) } } @@ -100,7 +114,10 @@ class CharacterSheetService( ) { sheets[characterSheetId]?.let { character -> val update = character.copy(damage = damage) - characterStore.save(sheet = update) + characterStore.save( + sheet = update, + create = false, + ) } } @@ -110,7 +127,10 @@ class CharacterSheetService( ) { sheets[characterSheetId]?.let { character -> val update = character.copy(diminished = diminished) - characterStore.save(sheet = update) + characterStore.save( + sheet = update, + create = false, + ) } } @@ -120,7 +140,10 @@ class CharacterSheetService( ) { sheets[characterSheetId]?.let { character -> val update = character.copy(fatigue = fatigue) - characterStore.save(sheet = update) + characterStore.save( + sheet = update, + create = false, + ) } } @@ -141,7 +164,10 @@ class CharacterSheetService( skill.takeIf { skill.id == skillId }?.copy(used = used) ?: skill }, ) - characterStore.save(sheet = update) + characterStore.save( + sheet = update, + create = false, + ) } } } \ No newline at end of file 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 2a7e54b..a3ee360 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 @@ -9,6 +9,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import kotlinx.serialization.json.Json import java.io.File @@ -27,15 +28,15 @@ class CharacterSheetStore( val scope = CoroutineScope(Dispatchers.IO + Job()) // load the initial data scope.launch { - updateCharacterSheets() + updateCharacterFlow() } } fun characterSheetsFlow(): StateFlow> = characterSheetsFlow - private suspend fun updateCharacterSheets() { + private suspend fun updateCharacterFlow() { characterSheetsFlow.value = try { - loadCharacterSheets() + load() } catch (exception: Exception) { println(exception) // TODO proper exception handling emptyList() @@ -44,9 +45,12 @@ class CharacterSheetStore( @Throws( FileReadException::class, + JsonCodingException::class, JsonConversionException::class, ) - suspend fun loadCharacterSheets(): List { + suspend fun load( + directory: File = this.directory, + ): List { return directory .listFiles() ?.mapNotNull { file -> @@ -59,9 +63,15 @@ class CharacterSheetStore( if (json.isBlank()) { return@mapNotNull null } + // decode the file + val data = try { + this.json.decodeFromString(json) + } catch (exception: Exception) { + throw JsonCodingException(root = exception) + } + // parse the json string. try { - val sheet = this.json.decodeFromString(json) - factory.convertFromJson(sheet) + factory.convertFromJson(data) } catch (exception: Exception) { throw JsonConversionException(root = exception) } @@ -71,19 +81,35 @@ class CharacterSheetStore( } @Throws( - FileWriteException::class, + BusinessException::class, JsonConversionException::class, + JsonCodingException::class, + FileWriteException::class, ) - fun save(sheet: CharacterSheet) { - // convert the character sheet into json format. - val data = try { - factory.convertToJson(sheet = sheet).let(json::encodeToString) + fun save( + sheet: CharacterSheet, + create: Boolean, + ) { + val file = characterSheetFile(id = sheet.id) + // Guard case on update alteration + if (create && file.exists()) { + val root = Exception("Character already exist, creation is impossible.") + throw BusinessException(root = root) + } + // Transform the json into the model. + val json = try { + factory.convertToJson(sheet = sheet) } catch (exception: Exception) { throw JsonConversionException(root = exception) } + // Encode the json into a string. + val data = try { + this.json.encodeToString(json) + } catch (exception: Exception) { + throw JsonCodingException(root = exception) + } // write the character file. try { - val file = characterSheetFile(id = sheet.id) file.writeText( text = data, charset = Charsets.UTF_8, @@ -92,32 +118,41 @@ class CharacterSheetStore( throw FileWriteException(root = exception) } // Update the dataflow. - characterSheetsFlow.value = characterSheetsFlow.value - .toMutableList() - .also { data -> - val index = data.indexOfFirst { it.id == sheet.id } - if (index >= 0) { - data[index] = sheet - } else { - data.add(sheet) + characterSheetsFlow.update { characters -> + characters.toMutableList() + .also { data -> + val index = data.indexOfFirst { it.id == sheet.id } + if (index >= 0) { + data[index] = sheet + } else { + data.add(sheet) + } } - } - .sortedWith(compareBy(Collator.getInstance()) { it.name }) + .sortedWith(compareBy(Collator.getInstance()) { + it.name + }) + } } - fun delete(id: String): Boolean { - val file = characterSheetFile(id = id) - val deleted = file.delete() - if (deleted) { - characterSheetsFlow.value = characterSheetsFlow.value.toMutableList() - .also { data -> - data.removeIf { it.id == id } - } - .sortedBy { - it.name - } + @Throws(BusinessException::class) + fun delete(characterSheetId: String) { + val file = characterSheetFile(id = characterSheetId) + // Guard case on the file existence. + if (file.exists().not()) { + val root = Exception("Character file with id:$characterSheetId doesn't not exist.") + throw BusinessException(root = root) + } + // Guard case on the file deletion + if (file.delete().not()) { + val root = Exception("Character file have not been deleted for unknown reason.") + throw BusinessException(root = root) + } + // Update the data model with the deleted character. + characterSheetsFlow.update { characters -> + characters.toMutableList() + .also { data -> data.removeIf { it.id == characterSheetId } } + .sortedWith(compareBy(Collator.getInstance()) { it.name }) } - return deleted } private fun characterSheetFile(id: String): File { @@ -126,6 +161,8 @@ class CharacterSheetStore( sealed class CharacterSheetStoreException(root: Exception) : Exception(root) class JsonConversionException(root: Exception) : CharacterSheetStoreException(root) + class JsonCodingException(root: Exception) : CharacterSheetStoreException(root) + class BusinessException(root: Exception) : CharacterSheetStoreException(root) class FileWriteException(root: Exception) : CharacterSheetStoreException(root) class FileReadException(root: Exception) : CharacterSheetStoreException(root) } \ No newline at end of file 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 58be8dd..f59604f 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 @@ -21,6 +21,14 @@ class Engine( val webSocket = MutableSharedFlow() suspend fun handle(message: SocketMessage) { + try { + unSafeHandle(message) + } catch (exception: Exception) { + println(exception) // TODO proper exception handling + } + } + + private suspend fun unSafeHandle(message: SocketMessage) { when (message) { is RollEvent -> Unit // Nothing to do here. @@ -61,10 +69,22 @@ class Engine( } is CampaignEvent -> when (message) { - is CampaignEvent.CharacterAdded -> Unit // TODO - is CampaignEvent.CharacterRemoved -> Unit // TODO - is CampaignEvent.NpcAdded -> Unit // TODO - is CampaignEvent.NpcRemoved -> Unit // TODO + is CampaignEvent.CharacterAdded -> campaignService.addCharacter( + characterSheetId = message.characterSheetId, + ) + + is CampaignEvent.CharacterRemoved -> campaignService.removeCharacter( + characterSheetId = message.characterSheetId, + ) + + is CampaignEvent.NpcAdded -> campaignService.addNpc( + characterSheetId = message.characterSheetId, + ) + + is CampaignEvent.NpcRemoved -> campaignService.removeNpc( + characterSheetId = message.characterSheetId, + ) + is CampaignEvent.UpdateScene -> Unit // TODO } diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/Server.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/Server.kt index 49cfc50..29f003c 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/Server.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/Server.kt @@ -6,12 +6,12 @@ import com.pixelized.server.lwa.server.rest.alteration.getAlteration import com.pixelized.server.lwa.server.rest.alteration.getAlterationTags import com.pixelized.server.lwa.server.rest.alteration.getAlterations import com.pixelized.server.lwa.server.rest.alteration.putAlteration -import com.pixelized.server.lwa.server.rest.campaign.removeCampaignNpc import com.pixelized.server.lwa.server.rest.campaign.getCampaign import com.pixelized.server.lwa.server.rest.campaign.putCampaignCharacter import com.pixelized.server.lwa.server.rest.campaign.putCampaignNpc import com.pixelized.server.lwa.server.rest.campaign.putCampaignScene import com.pixelized.server.lwa.server.rest.campaign.removeCampaignCharacter +import com.pixelized.server.lwa.server.rest.campaign.removeCampaignNpc import com.pixelized.server.lwa.server.rest.character.deleteCharacter import com.pixelized.server.lwa.server.rest.character.getCharacter import com.pixelized.server.lwa.server.rest.character.getCharacterTags 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 0652f60..4fd10d8 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 @@ -1,14 +1,14 @@ package com.pixelized.server.lwa.server.rest.alteration -import com.pixelized.server.lwa.model.alteration.AlterationStore 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.websocket.ApiSynchronisation import io.ktor.server.response.respond +import io.ktor.server.routing.RoutingContext -fun Engine.deleteAlteration(): suspend io.ktor.server.routing.RoutingContext.() -> Unit { +fun Engine.deleteAlteration(): suspend RoutingContext.() -> Unit { return { try { // get the query parameter 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 a5e4df7..443b39e 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 @@ -4,8 +4,9 @@ import com.pixelized.server.lwa.server.Engine import com.pixelized.server.lwa.utils.extentions.alterationId import com.pixelized.shared.lwa.protocol.rest.ResultJson import io.ktor.server.response.respond +import io.ktor.server.routing.RoutingContext -fun Engine.getAlteration(): suspend io.ktor.server.routing.RoutingContext.() -> Unit { +fun Engine.getAlteration(): suspend RoutingContext.() -> Unit { return { try { // get the query parameter 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 76ac1d9..5639499 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 @@ -3,8 +3,9 @@ package com.pixelized.server.lwa.server.rest.alteration import com.pixelized.server.lwa.server.Engine import com.pixelized.shared.lwa.protocol.rest.ResultJson import io.ktor.server.response.respond +import io.ktor.server.routing.RoutingContext -fun Engine.getAlterationTags(): suspend io.ktor.server.routing.RoutingContext.() -> Unit { +fun Engine.getAlterationTags(): suspend RoutingContext.() -> Unit { return { try { call.respond( 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 e0c029b..10cf56f 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 @@ -3,8 +3,9 @@ package com.pixelized.server.lwa.server.rest.alteration import com.pixelized.server.lwa.server.Engine import com.pixelized.shared.lwa.protocol.rest.ResultJson import io.ktor.server.response.respond +import io.ktor.server.routing.RoutingContext -fun Engine.getAlterations(): suspend io.ktor.server.routing.RoutingContext.() -> Unit { +fun Engine.getAlterations(): suspend RoutingContext.() -> Unit { return { try { call.respond( 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 c8bed66..1438132 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 @@ -1,6 +1,5 @@ package com.pixelized.server.lwa.server.rest.alteration -import com.pixelized.server.lwa.model.alteration.AlterationStore import com.pixelized.server.lwa.server.Engine import com.pixelized.server.lwa.utils.extentions.MissingParameterException import com.pixelized.server.lwa.utils.extentions.create @@ -9,8 +8,9 @@ import com.pixelized.shared.lwa.protocol.rest.ResultJson import com.pixelized.shared.lwa.protocol.websocket.ApiSynchronisation import io.ktor.server.request.receive import io.ktor.server.response.respond +import io.ktor.server.routing.RoutingContext -fun Engine.putAlteration(): suspend io.ktor.server.routing.RoutingContext.() -> Unit { +fun Engine.putAlteration(): suspend RoutingContext.() -> Unit { return { try { val form = call.receive() 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 e73e688..9159681 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 @@ -6,8 +6,9 @@ import com.pixelized.server.lwa.utils.extentions.characterSheetId import com.pixelized.shared.lwa.protocol.rest.ResultJson import com.pixelized.shared.lwa.protocol.websocket.CampaignEvent import io.ktor.server.response.respond +import io.ktor.server.routing.RoutingContext -fun Engine.removeCampaignCharacter(): suspend io.ktor.server.routing.RoutingContext.() -> Unit { +fun Engine.removeCampaignCharacter(): suspend RoutingContext.() -> Unit { return { try { // get the query parameter 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 8a85c97..9c6837c 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 @@ -5,11 +5,10 @@ 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.websocket.CampaignEvent -import io.ktor.http.HttpStatusCode import io.ktor.server.response.respond -import io.ktor.server.response.respondText +import io.ktor.server.routing.RoutingContext -fun Engine.removeCampaignNpc(): suspend io.ktor.server.routing.RoutingContext.() -> Unit { +fun Engine.removeCampaignNpc(): suspend RoutingContext.() -> Unit { return { try { // get the query parameter 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 135c559..63efe2e 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 @@ -3,8 +3,9 @@ package com.pixelized.server.lwa.server.rest.campaign import com.pixelized.server.lwa.server.Engine import com.pixelized.shared.lwa.protocol.rest.ResultJson import io.ktor.server.response.respond +import io.ktor.server.routing.RoutingContext -fun Engine.getCampaign(): suspend io.ktor.server.routing.RoutingContext.() -> Unit { +fun Engine.getCampaign(): suspend RoutingContext.() -> Unit { return { try { call.respond( 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 fea4532..c76a380 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 @@ -6,8 +6,9 @@ import com.pixelized.server.lwa.utils.extentions.characterSheetId import com.pixelized.shared.lwa.protocol.rest.ResultJson import com.pixelized.shared.lwa.protocol.websocket.CampaignEvent import io.ktor.server.response.respond +import io.ktor.server.routing.RoutingContext -fun Engine.putCampaignCharacter(): suspend io.ktor.server.routing.RoutingContext.() -> Unit { +fun Engine.putCampaignCharacter(): suspend RoutingContext.() -> Unit { return { try { // get the query parameter 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 f461b19..7fe30c6 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 @@ -6,8 +6,9 @@ import com.pixelized.server.lwa.utils.extentions.characterSheetId import com.pixelized.shared.lwa.protocol.rest.ResultJson import com.pixelized.shared.lwa.protocol.websocket.CampaignEvent import io.ktor.server.response.respond +import io.ktor.server.routing.RoutingContext -fun Engine.putCampaignNpc(): suspend io.ktor.server.routing.RoutingContext.() -> Unit { +fun Engine.putCampaignNpc(): suspend RoutingContext.() -> Unit { return { try { // get the query parameter 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 e5b2d1e..0efc8b7 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 @@ -5,12 +5,11 @@ 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.websocket.CampaignEvent -import io.ktor.http.HttpStatusCode import io.ktor.server.request.receive import io.ktor.server.response.respond -import io.ktor.server.response.respondText +import io.ktor.server.routing.RoutingContext -fun Engine.putCampaignScene(): suspend io.ktor.server.routing.RoutingContext.() -> Unit { +fun Engine.putCampaignScene(): suspend RoutingContext.() -> Unit { return { try { // Get the scene json from the body of the request 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 ca99cda..01be69c 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 @@ -1,23 +1,25 @@ 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.websocket.ApiSynchronisation -import io.ktor.http.HttpStatusCode -import io.ktor.server.response.respondText +import io.ktor.server.response.respond +import io.ktor.server.routing.RoutingContext -fun Engine.deleteCharacter(): suspend io.ktor.server.routing.RoutingContext.() -> Unit { +fun Engine.deleteCharacter(): suspend RoutingContext.() -> Unit { return { - val characterSheetId = call.parameters.characterSheetId - - val deleted = characterService.deleteCharacterSheet( - characterSheetId = characterSheetId - ) - - if (deleted) { - call.respondText( - text = "${HttpStatusCode.OK}", - status = HttpStatusCode.OK, + try { + // get the query parameter + val characterSheetId = call.parameters.characterSheetId + // delete the character sheet. + characterService.deleteCharacterSheet( + characterSheetId = characterSheetId + ) + // API & WebSocket responses. + call.respond( + message = ResultJson.Success() ) webSocket.emit( value = ApiSynchronisation.CharacterSheetDelete( @@ -25,10 +27,19 @@ fun Engine.deleteCharacter(): suspend io.ktor.server.routing.RoutingContext.() - characterSheetId = characterSheetId, ), ) - } else { - call.respondText( - text = "${HttpStatusCode.UnprocessableEntity}", - status = HttpStatusCode.UnprocessableEntity, + } catch (exception: MissingParameterException) { + call.respond( + message = ResultJson.Error( + status = exception.errorCode, + message = exception.message ?: "?", + ) + ) + } catch (exception: Exception) { + call.respond( + message = ResultJson.Error( + status = ResultJson.Error.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 ea8c381..dd8a726 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 @@ -1,12 +1,13 @@ 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 io.ktor.http.HttpStatusCode +import com.pixelized.shared.lwa.protocol.rest.ResultJson import io.ktor.server.response.respond -import io.ktor.server.response.respondText +import io.ktor.server.routing.RoutingContext -fun Engine.getCharacter(): suspend io.ktor.server.routing.RoutingContext.() -> Unit { +fun Engine.getCharacter(): suspend RoutingContext.() -> Unit { return { try { // get the query parameter @@ -19,10 +20,19 @@ fun Engine.getCharacter(): suspend io.ktor.server.routing.RoutingContext.() -> U call.respond( message = characterSheet, ) + } catch (exception: MissingParameterException) { + call.respond( + message = ResultJson.Error( + status = exception.errorCode, + message = exception.message ?: "?", + ) + ) } catch (exception: Exception) { - call.respondText( - text = "${HttpStatusCode.UnprocessableEntity}", - status = HttpStatusCode.UnprocessableEntity + call.respond( + message = ResultJson.Error( + status = ResultJson.Error.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 9da14ff..81607df 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,12 +1,23 @@ package com.pixelized.server.lwa.server.rest.character import com.pixelized.server.lwa.server.Engine +import com.pixelized.shared.lwa.protocol.rest.ResultJson import io.ktor.server.response.respond +import io.ktor.server.routing.RoutingContext -fun Engine.getCharacterTags(): suspend io.ktor.server.routing.RoutingContext.() -> Unit { +fun Engine.getCharacterTags(): suspend RoutingContext.() -> Unit { return { - call.respond( - message = characterService.tags(), - ) + try { + call.respond( + message = characterService.tags(), + ) + } catch (exception: Exception) { + call.respond( + message = ResultJson.Error( + status = ResultJson.Error.GENERIC, + message = exception.message ?: "?", + ) + ) + } } } \ No newline at end of file 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 727b986..8e8fab6 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,12 +1,23 @@ package com.pixelized.server.lwa.server.rest.character import com.pixelized.server.lwa.server.Engine +import com.pixelized.shared.lwa.protocol.rest.ResultJson import io.ktor.server.response.respond +import io.ktor.server.routing.RoutingContext -fun Engine.getCharacters(): suspend io.ktor.server.routing.RoutingContext.() -> Unit { +fun Engine.getCharacters(): suspend RoutingContext.() -> Unit { return { - call.respond( - message = characterService.charactersJson(), - ) + try { + call.respond( + message = characterService.charactersJson(), + ) + } catch (exception: Exception) { + call.respond( + message = ResultJson.Error( + status = ResultJson.Error.GENERIC, + message = exception.message ?: "?", + ) + ) + } } } \ No newline at end of file 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 e521ca5..5f8d4eb 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 @@ -1,22 +1,26 @@ 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.create import com.pixelized.shared.lwa.model.characterSheet.CharacterSheetJson +import com.pixelized.shared.lwa.protocol.rest.ResultJson import com.pixelized.shared.lwa.protocol.websocket.ApiSynchronisation -import io.ktor.http.HttpStatusCode import io.ktor.server.request.receive -import io.ktor.server.response.respondText +import io.ktor.server.response.respond +import io.ktor.server.routing.RoutingContext -fun Engine.putCharacter(): suspend io.ktor.server.routing.RoutingContext.() -> Unit { +fun Engine.putCharacter(): suspend RoutingContext.() -> Unit { return { try { val form = call.receive() - characterService.updateCharacterSheet( + val create = call.queryParameters.create + characterService.save( character = form, + create = create, ) - call.respondText( - text = "${HttpStatusCode.OK}", - status = HttpStatusCode.OK, + call.respond( + message = ResultJson.Success() ) webSocket.emit( value = ApiSynchronisation.CharacterSheetUpdate( @@ -24,10 +28,19 @@ fun Engine.putCharacter(): suspend io.ktor.server.routing.RoutingContext.() -> U characterSheetId = form.id, ), ) + } catch (exception: MissingParameterException) { + call.respond( + message = ResultJson.Error( + status = exception.errorCode, + message = exception.message ?: "?", + ) + ) } catch (exception: Exception) { - call.respondText( - text = "${HttpStatusCode.UnprocessableEntity}", - status = HttpStatusCode.UnprocessableEntity, + call.respond( + message = ResultJson.Error( + status = ResultJson.Error.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 b4fd0fa..6e0f399 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 @@ -1,20 +1,22 @@ 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.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.websocket.CharacterSheetEvent -import io.ktor.http.HttpStatusCode -import io.ktor.server.response.respondText +import io.ktor.server.response.respond +import io.ktor.server.routing.RoutingContext -fun Engine.putCharacterAlteration(): suspend io.ktor.server.routing.RoutingContext.() -> Unit { +fun Engine.putCharacterAlteration(): suspend RoutingContext.() -> Unit { return { try { // get the query parameter val characterSheetId = call.queryParameters.characterSheetId val alterationId = call.queryParameters.alterationId - val active = call.queryParameters["active"]?.toBooleanStrictOrNull() - ?: error("Missing active parameter.") + val active = call.queryParameters.active // Update the character damage characterService.updateAlteration( characterSheetId = characterSheetId, @@ -22,9 +24,8 @@ fun Engine.putCharacterAlteration(): suspend io.ktor.server.routing.RoutingConte active = active ) // API & WebSocket responses. - call.respondText( - text = "${HttpStatusCode.OK}", - status = HttpStatusCode.OK, + call.respond( + message = ResultJson.Success() ) webSocket.emit( value = CharacterSheetEvent.UpdateAlteration( @@ -34,10 +35,19 @@ fun Engine.putCharacterAlteration(): suspend io.ktor.server.routing.RoutingConte active = active, ) ) + } catch (exception: MissingParameterException) { + call.respond( + message = ResultJson.Error( + status = exception.errorCode, + message = exception.message ?: "?", + ) + ) } catch (exception: Exception) { - call.respondText( - text = "${HttpStatusCode.UnprocessableEntity}", - status = HttpStatusCode.UnprocessableEntity, + call.respond( + message = ResultJson.Error( + status = ResultJson.Error.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 0dae255..831b2d8 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 @@ -1,30 +1,34 @@ 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.websocket.CharacterSheetEvent -import io.ktor.http.HttpStatusCode -import io.ktor.server.response.respondText +import io.ktor.server.response.respond +import io.ktor.server.routing.RoutingContext -fun Engine.putCharacterDamage(): suspend io.ktor.server.routing.RoutingContext.() -> Unit { +fun Engine.putCharacterDamage(): suspend RoutingContext.() -> Unit { return { try { // get the query parameter val characterSheetId = call.queryParameters.characterSheetId val damage = call.queryParameters["damage"]?.toIntOrNull() - ?: error("Missing damage parameter.") + ?: throw MissingParameterException( + name = "damage", + errorCode = ResultJson.Error.MISSING_DAMAGE + ) // fetch the character sheet val characterSheet = characterService.character(characterSheetId) - ?: error("Character sheet not found for characterSheetId: $characterSheetId") + ?: error("CharacterSheet with id:$characterSheetId not found.") // Update the character damage characterService.updateDamage( characterSheetId = characterSheetId, damage = damage ) // API & WebSocket responses. - call.respondText( - text = "${HttpStatusCode.OK}", - status = HttpStatusCode.OK, + call.respond( + message = ResultJson.Success() ) webSocket.emit( value = CharacterSheetEvent.UpdateDamage( @@ -34,10 +38,19 @@ fun Engine.putCharacterDamage(): suspend io.ktor.server.routing.RoutingContext.( damage = damage, ) ) + } catch (exception: MissingParameterException) { + call.respond( + message = ResultJson.Error( + status = exception.errorCode, + message = exception.message ?: "?", + ) + ) } catch (exception: Exception) { - call.respondText( - text = "${HttpStatusCode.UnprocessableEntity}", - status = HttpStatusCode.UnprocessableEntity, + call.respond( + message = ResultJson.Error( + status = ResultJson.Error.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 fc44ae9..7f8426e 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 @@ -1,27 +1,31 @@ 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.websocket.CharacterSheetEvent -import io.ktor.http.HttpStatusCode -import io.ktor.server.response.respondText +import io.ktor.server.response.respond +import io.ktor.server.routing.RoutingContext -fun Engine.putCharacterDiminished(): suspend io.ktor.server.routing.RoutingContext.() -> Unit { +fun Engine.putCharacterDiminished(): suspend RoutingContext.() -> Unit { return { try { // get the query parameter val characterSheetId = call.queryParameters.characterSheetId val diminished = call.queryParameters["diminished"]?.toIntOrNull() - ?: error("Missing diminished parameter.") + ?: throw MissingParameterException( + name = "diminished", + errorCode = ResultJson.Error.MISSING_DIMINISHED + ) // Update the character damage characterService.updateDiminished( characterSheetId = characterSheetId, diminished = diminished ) // API & WebSocket responses. - call.respondText( - text = "${HttpStatusCode.OK}", - status = HttpStatusCode.OK, + call.respond( + message = ResultJson.Success() ) webSocket.emit( value = CharacterSheetEvent.UpdateDiminished( @@ -30,10 +34,19 @@ fun Engine.putCharacterDiminished(): suspend io.ktor.server.routing.RoutingConte diminished = diminished, ) ) + } catch (exception: MissingParameterException) { + call.respond( + message = ResultJson.Error( + status = exception.errorCode, + message = exception.message ?: "?", + ) + ) } catch (exception: Exception) { - call.respondText( - text = "${HttpStatusCode.UnprocessableEntity}", - status = HttpStatusCode.UnprocessableEntity, + call.respond( + message = ResultJson.Error( + status = ResultJson.Error.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 5b31494..62431ca 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 @@ -1,30 +1,34 @@ 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.websocket.CharacterSheetEvent -import io.ktor.http.HttpStatusCode -import io.ktor.server.response.respondText +import io.ktor.server.response.respond +import io.ktor.server.routing.RoutingContext -fun Engine.putCharacterFatigue(): suspend io.ktor.server.routing.RoutingContext.() -> Unit { +fun Engine.putCharacterFatigue(): suspend RoutingContext.() -> Unit { return { try { // get the query parameter val characterSheetId = call.queryParameters.characterSheetId val fatigue = call.queryParameters["fatigue"]?.toIntOrNull() - ?: error("Missing fatigue parameter.") + ?: throw MissingParameterException( + name = "fatigue", + errorCode = ResultJson.Error.MISSING_FATIGUE + ) // fetch the character sheet val characterSheet = characterService.character(characterSheetId) - ?: error("Character sheet not found for characterSheetId: $characterSheetId") + ?: error("CharacterSheet with id:$characterSheetId not found.") // Update the character damage characterService.updateFatigue( characterSheetId = characterSheetId, fatigue = fatigue ) // API & WebSocket responses. - call.respondText( - text = "${HttpStatusCode.OK}", - status = HttpStatusCode.OK, + call.respond( + message = ResultJson.Success() ) webSocket.emit( value = CharacterSheetEvent.UpdateFatigue( @@ -34,10 +38,19 @@ fun Engine.putCharacterFatigue(): suspend io.ktor.server.routing.RoutingContext. fatigue = fatigue, ) ) + } catch (exception: MissingParameterException) { + call.respond( + message = ResultJson.Error( + status = exception.errorCode, + message = exception.message ?: "?", + ) + ) } catch (exception: Exception) { - call.respondText( - text = "${HttpStatusCode.UnprocessableEntity}", - status = HttpStatusCode.UnprocessableEntity, + call.respond( + message = ResultJson.Error( + status = ResultJson.Error.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 4901cdb..ac119ad 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 @@ -4,25 +4,36 @@ import com.pixelized.shared.lwa.protocol.rest.ResultJson import io.ktor.http.Parameters val Parameters.characterSheetId - get() = this["characterSheetId"] - ?: throw MissingParameterException( - name = "characterSheetId", + get() = "characterSheetId".let { param -> + this[param] ?: throw MissingParameterException( + name = param, errorCode = ResultJson.Error.MISSING_CHARACTER_SHEET_ID, ) + } val Parameters.alterationId - get() = this["alterationId"] - ?: throw MissingParameterException( - name = "alterationId", + get() = "alterationId".let { param -> + this[param] ?: throw MissingParameterException( + name = param, errorCode = ResultJson.Error.MISSING_ALTERATION_ID, ) + } val Parameters.create - get() = this["create"]?.toBooleanStrictOrNull() - ?: throw MissingParameterException( - name = "create", + get() = "create".let { param -> + this[param]?.toBooleanStrictOrNull() ?: throw MissingParameterException( + name = param, errorCode = ResultJson.Error.MISSING_CREATE ) + } + +val Parameters.active + get() = "active".let { param -> + this[param]?.toBooleanStrictOrNull() ?: throw MissingParameterException( + name = param, + errorCode = ResultJson.Error.MISSING_ACTIVE + ) + } class MissingParameterException( name: String, 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 index c23172c..2b4ebc5 100644 --- 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 @@ -19,6 +19,10 @@ sealed interface ResultJson { 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 } }