Skip to content

Commit

Permalink
Merge pull request #54 from Sports-day/feature/#44-add-configuration-…
Browse files Browse the repository at this point in the history
…endpoint

feat: configuration endpoint
  • Loading branch information
testusuke authored Oct 14, 2023
2 parents 93807b3 + 63a2624 commit 4fe4a38
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/kotlin/net/sportsday/plugins/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ fun Application.configureRouting() {
informationRouter()
// Tags
tagRouter()
// Configurations
configurationRouter()
}
}
}
Expand Down
75 changes: 75 additions & 0 deletions src/main/kotlin/net/sportsday/routes/v1/ConfigurationRouter.kt
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 src/main/kotlin/net/sportsday/services/ConfigurationService.kt
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()))
}
}
}

0 comments on commit 4fe4a38

Please sign in to comment.