-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…endpoint feat: configuration endpoint
- Loading branch information
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/kotlin/net/sportsday/routes/v1/ConfigurationRouter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package net.sportsday.routes.v1 | ||
|
||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import net.sportsday.plugins.Role | ||
import net.sportsday.plugins.withRole | ||
import net.sportsday.services.ConfigurationService | ||
import net.sportsday.utils.DataResponse | ||
import net.sportsday.utils.respondOrInternalError | ||
|
||
/** | ||
* Created by testusuke on 2023/10/15 | ||
* @author testusuke | ||
*/ | ||
fun Route.configurationRouter() { | ||
withRole(Role.ADMIN) { | ||
route("/configuration") { | ||
route("/restrict_game_preview") { | ||
route("/status") { | ||
// get restriction is enabled | ||
get { | ||
ConfigurationService.RestrictGamePreview.isEnabled() | ||
.respondOrInternalError { | ||
call.respond( | ||
HttpStatusCode.OK, | ||
DataResponse(it), | ||
) | ||
} | ||
} | ||
|
||
// set restriction status | ||
post { | ||
val status = call.parameters["status"]?.toBoolean() ?: false | ||
|
||
ConfigurationService.RestrictGamePreview.setEnabled(status) | ||
.respondOrInternalError { | ||
call.respond( | ||
HttpStatusCode.OK, | ||
DataResponse(status), | ||
) | ||
} | ||
} | ||
} | ||
|
||
route("/percentage") { | ||
// get restriction percentage | ||
get { | ||
ConfigurationService.RestrictGamePreview.getPercentage() | ||
.respondOrInternalError { | ||
call.respond( | ||
HttpStatusCode.OK, | ||
DataResponse(it), | ||
) | ||
} | ||
} | ||
|
||
// set restriction percentage | ||
post { | ||
val percentage = call.parameters["percentage"]?.toDouble() ?: 0.6 | ||
|
||
ConfigurationService.RestrictGamePreview.setPercentage(percentage) | ||
.respondOrInternalError { | ||
call.respond( | ||
HttpStatusCode.OK, | ||
DataResponse(percentage), | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/kotlin/net/sportsday/services/ConfigurationService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package net.sportsday.services | ||
|
||
import io.ktor.server.plugins.* | ||
import net.sportsday.utils.configuration.Key | ||
import net.sportsday.utils.configuration.KeyValueStore | ||
|
||
/** | ||
* Created by testusuke on 2023/10/15 | ||
* @author testusuke | ||
*/ | ||
object ConfigurationService { | ||
|
||
object RestrictGamePreview { | ||
fun isEnabled(): Result<Boolean> { | ||
return Result.success(KeyValueStore.get(Key.RestrictGamePreview)?.toBoolean() ?: throw NotFoundException("not found configuration key")) | ||
} | ||
|
||
fun setEnabled(enabled: Boolean): Result<Unit> { | ||
return Result.success(KeyValueStore.set(Key.RestrictGamePreview, enabled.toString())) | ||
} | ||
|
||
fun getPercentage(): Result<Double> { | ||
return Result.success(KeyValueStore.get(Key.RestrictGamePreviewPercentage)?.toDouble() ?: throw NotFoundException("not found configuration key")) | ||
} | ||
|
||
fun setPercentage(percentage: Double): Result<Unit> { | ||
return Result.success(KeyValueStore.set(Key.RestrictGamePreviewPercentage, percentage.toString())) | ||
} | ||
} | ||
} |