Add setting controller

This commit is contained in:
2023-12-03 23:07:22 +08:00
parent d7c0b7376d
commit 5c8775bf12
10 changed files with 166 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
package top.fatweb.api.controller.system
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import top.fatweb.api.entity.common.ResponseResult
import top.fatweb.api.param.system.SettingParam
import top.fatweb.api.service.system.ISysSettingService
import top.fatweb.api.vo.system.SettingVo
/**
* System setting controller
*
* @author FatttSnake, fatttsnake@gmail.com
* @since 1.0.0
*/
@Tag(name = "系统设置", description = "系统设置相关接口")
@RequestMapping("/system/setting")
@RestController
class SettingController(
private val sysSettingService: ISysSettingService
) {
@Operation(summary = "获取")
fun get(): ResponseResult<SettingVo> = ResponseResult.success(data = sysSettingService.get())
@Operation(summary = "更新")
fun update(settingParam: SettingParam): ResponseResult<Nothing> {
sysSettingService.update(settingParam)
return ResponseResult.success()
}
}

View File

@@ -0,0 +1,32 @@
package top.fatweb.api.converter.system
import top.fatweb.api.param.system.SettingParam
import top.fatweb.api.setting.MailSetting
import top.fatweb.api.setting.SystemSetting
import top.fatweb.api.vo.system.SettingVo
object SettingConverter {
fun mailSettingToMailSettingVo(mailSetting: MailSetting) = SettingVo.MailSettingVo(
host = mailSetting.host,
port = mailSetting.port,
username = mailSetting.username,
password = mailSetting.password,
from = mailSetting.from
)
fun systemSettingToSettingVo(sysSetting: SystemSetting) = SettingVo(
mail = sysSetting.mail?.let { mailSettingToMailSettingVo(it) }
)
fun mailSettingParamToMailSetting(mailSettingParam: SettingParam.MailSettingParam) = MailSetting(
host = mailSettingParam.host,
port = mailSettingParam.port,
username = mailSettingParam.username,
password = mailSettingParam.password,
from = mailSettingParam.from
)
fun settingParamToSystemSetting(settingParam: SettingParam) = SystemSetting(
mail = settingParam.mail?.let { mailSettingParamToMailSetting(it) }
)
}

View File

@@ -0,0 +1,13 @@
package top.fatweb.api.param.system
data class SettingParam (
val mail: MailSettingParam?
) {
data class MailSettingParam (
val host: String,
val port: Int,
val username: String,
val password: String,
val from: String
)
}

View File

@@ -0,0 +1,10 @@
package top.fatweb.api.service.system
import top.fatweb.api.param.system.SettingParam
import top.fatweb.api.vo.system.SettingVo
interface ISysSettingService {
fun get(): SettingVo
fun update(settingParam: SettingParam)
}

View File

@@ -0,0 +1,17 @@
package top.fatweb.api.service.system.impl
import org.springframework.stereotype.Service
import top.fatweb.api.param.system.SettingParam
import top.fatweb.api.service.system.ISysSettingService
import top.fatweb.api.vo.system.SettingVo
@Service
class SysSettingServiceImpl : ISysSettingService {
override fun get(): SettingVo {
TODO("Not yet implemented")
}
override fun update(settingParam: SettingParam) {
TODO("Not yet implemented")
}
}

View File

@@ -0,0 +1,9 @@
package top.fatweb.api.setting
data class MailSetting(
var host: String? = null,
var port: Int? = null,
var username: String? = null,
var password: String? = null,
var from: String? = null,
)

View File

@@ -0,0 +1,31 @@
package top.fatweb.api.setting
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper
import com.fasterxml.jackson.module.kotlin.readValue
import java.io.File
import java.io.IOException
object SettingOperator {
const val SETTING_FILE_NAME = "data/config/setting.yaml"
private val yamlMapper = YAMLMapper()
var systemSetting: SystemSetting? = null
fun init() {
}
fun getFromSettingFile(): SystemSetting {
val settingFile = File(SETTING_FILE_NAME)
if (!settingFile.isFile) {
if (!settingFile.parentFile.isDirectory) {
if (!settingFile.parentFile.mkdirs()) {
throw IOException("Can not make directory: ${settingFile.parentFile.absolutePath}")
}
}
settingFile.delete()
settingFile.createNewFile()
}
return yamlMapper.readValue<SystemSetting>(settingFile)
}
}

View File

@@ -0,0 +1,5 @@
package top.fatweb.api.setting
data class SystemSetting(
var mail: MailSetting? = null
)

View File

@@ -0,0 +1,13 @@
package top.fatweb.api.vo.system
data class SettingVo(
val mail: MailSettingVo?
) {
data class MailSettingVo(
val host: String?,
val port: Int?,
val username: String?,
val password: String?,
val from: String?
)
}