mirror of
https://github.com/MewoLab/AquaDX.git
synced 2026-02-13 19:17:34 +08:00
[F] Fix setting api
This commit is contained in:
@@ -2,26 +2,25 @@ package icu.samnyan.aqua.net
|
|||||||
|
|
||||||
import ext.*
|
import ext.*
|
||||||
import icu.samnyan.aqua.net.db.AquaGameOptions
|
import icu.samnyan.aqua.net.db.AquaGameOptions
|
||||||
|
import icu.samnyan.aqua.net.db.AquaGameOptionsRepo
|
||||||
import icu.samnyan.aqua.net.db.AquaNetUserRepo
|
import icu.samnyan.aqua.net.db.AquaNetUserRepo
|
||||||
import icu.samnyan.aqua.net.db.AquaUserServices
|
import icu.samnyan.aqua.net.db.AquaUserServices
|
||||||
import org.springframework.web.bind.annotation.RestController
|
import org.springframework.web.bind.annotation.RestController
|
||||||
import kotlin.reflect.full.findAnnotation
|
import kotlin.reflect.full.findAnnotation
|
||||||
import kotlin.reflect.full.primaryConstructor
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@API("/api/v2/settings")
|
@API("/api/v2/settings")
|
||||||
class SettingsApi(
|
class SettingsApi(
|
||||||
val us: AquaUserServices,
|
val us: AquaUserServices,
|
||||||
val userRepo: AquaNetUserRepo
|
val userRepo: AquaNetUserRepo,
|
||||||
|
val goRepo: AquaGameOptionsRepo
|
||||||
) {
|
) {
|
||||||
// Get all params with SettingField annotation
|
// Get all params with SettingField annotation
|
||||||
// Kotlin by default put all the @Annotations in the constructor param
|
val fields = AquaGameOptions::class.vars()
|
||||||
val params = AquaGameOptions::class.primaryConstructor!!.parameters
|
|
||||||
.mapNotNull { it.findAnnotation<SettingField>()?.let { an -> it to an } }
|
.mapNotNull { it.findAnnotation<SettingField>()?.let { an -> it to an } }
|
||||||
val fields = AquaGameOptions::class.vars().associateBy { it.name }
|
val fieldMap = fields.associate { (f, _) -> f.name to f }
|
||||||
val fieldMap = params.associate { (key, _) -> key.name to fields[key.name]!! }
|
val fieldDesc = fields.map { (f, an) -> mapOf(
|
||||||
val fieldDesc = params.map { (key, an) -> mapOf(
|
"key" to f.name,
|
||||||
"key" to key.name,
|
|
||||||
"name" to an.name,
|
"name" to an.name,
|
||||||
"desc" to an.desc
|
"desc" to an.desc
|
||||||
) }
|
) }
|
||||||
@@ -42,12 +41,13 @@ class SettingsApi(
|
|||||||
}
|
}
|
||||||
// Check field type
|
// Check field type
|
||||||
val type = field.returnType
|
val type = field.returnType
|
||||||
val newValue = when {
|
val newValue = when (type.classifier) {
|
||||||
type == String::class -> value
|
String::class -> value
|
||||||
type == Int::class -> value.toInt()
|
Int::class -> value.toInt()
|
||||||
type == Boolean::class -> value.toBoolean()
|
Boolean::class -> value.toBoolean()
|
||||||
else -> (400 - "Invalid field type $type")
|
else -> (400 - "Invalid field type $type")
|
||||||
}
|
}
|
||||||
field.set(options, newValue)
|
field.set(options, newValue)
|
||||||
|
goRepo.save(options)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -32,4 +32,4 @@ class AquaGameOptions(
|
|||||||
var unlockTickets: Boolean = false,
|
var unlockTickets: Boolean = false,
|
||||||
)
|
)
|
||||||
|
|
||||||
interface AquaGameOptionsRepository : JpaRepository<AquaGameOptions, Long>
|
interface AquaGameOptionsRepo : JpaRepository<AquaGameOptions, Long>
|
||||||
Reference in New Issue
Block a user