Skip to content

Commit

Permalink
feat: add systemStatus as SettingConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiinaKin committed Sep 13, 2024
1 parent 3c0aae5 commit fbae82a
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ package io.sakurasou.constant

const val SETTING_SITE = "site"
const val SETTING_STRATEGY = "strategy"
const val SETTING_SYSTEM = "system"
const val SETTING_SYSTEM = "system"
const val SETTING_STATUS = "status"
14 changes: 6 additions & 8 deletions app/src/main/kotlin/io/sakurasou/model/common/DatabaseInit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import io.sakurasou.model.dto.*
import io.sakurasou.model.setting.SiteSetting
import io.sakurasou.model.setting.StrategySetting
import io.sakurasou.model.setting.SystemSetting
import io.sakurasou.model.setting.SystemStatus
import io.sakurasou.model.strategy.LocalStrategy
import kotlinx.datetime.Clock
import kotlinx.datetime.TimeZone
Expand Down Expand Up @@ -113,7 +114,6 @@ private val albumOpsPermissions = listOf(
ALBUM_DELETE_ALL
)


private fun initPermission() {
val allPermissions = listOf(
userOpsPermissions,
Expand Down Expand Up @@ -217,23 +217,21 @@ private fun initSetting() {
defaultGroupId = 1,
allowSignup = false
)
val systemStatus = SystemStatus(
isInit = false
)

val now = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault())

val siteSettingInsertDTO = SettingInsertDTO(SETTING_SITE, siteSettingConfig, now, now)
val strategySettingInsertDTO = SettingInsertDTO(SETTING_STRATEGY, strategySettingConfig, now, now)
val systemSettingInsertDTO = SettingInsertDTO(SETTING_SYSTEM, systemSettingConfig, now, now)
val systemStatusInsertDTO = SettingInsertDTO(SETTING_STATUS, systemStatus, now, now)

InstanceCenter.settingDao.saveSetting(siteSettingInsertDTO)
InstanceCenter.settingDao.saveSetting(strategySettingInsertDTO)
InstanceCenter.settingDao.saveSetting(systemSettingInsertDTO)
InstanceCenter.settingDao.saveSetting(systemStatusInsertDTO)

exposedLogger.info("setting init success")
}

// TODO move it into save user logic
private fun initAlbum() {
val now = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault())
val uncategorizedAlbum = AlbumInsertDTO(1, "Uncategorized", null, 0, now)
InstanceCenter.albumDao.saveAlbum(uncategorizedAlbum)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.sakurasou.model.setting

/**
* @author Shiina Kin
* 2024/9/13 15:58
*/
data class SystemStatus(
val isInit: Boolean
) : SettingConfig()
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ import io.sakurasou.controller.request.SystemSettingPatchRequest
import io.sakurasou.model.setting.SiteSetting
import io.sakurasou.model.setting.StrategySetting
import io.sakurasou.model.setting.SystemSetting
import io.sakurasou.model.setting.SystemStatus

/**
* @author Shiina Kin
* 2024/9/13 14:57
*/
interface SettingService {
suspend fun updateSystemSetting(systemSetting: SystemSettingPatchRequest)
suspend fun updateSiteSetting(siteSetting: SiteSettingPatchRequest)
suspend fun updateSiteSetting(siteSetting: SiteSetting)
suspend fun updateSiteSetting(siteSettingPatch: SiteSettingPatchRequest)
suspend fun updateStrategySetting(strategySetting: StrategySettingPatchRequest)
suspend fun updateSystemStatus(systemStatus: SystemStatus)
suspend fun getSystemSetting(): SystemSetting
suspend fun getSiteSetting(): SiteSetting
suspend fun getStrategySetting(): StrategySetting
suspend fun getSystemStatus(): SystemStatus
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.sakurasou.service.setting

import io.sakurasou.constant.SETTING_SITE
import io.sakurasou.constant.SETTING_STATUS
import io.sakurasou.constant.SETTING_STRATEGY
import io.sakurasou.constant.SETTING_SYSTEM
import io.sakurasou.controller.request.SiteSettingPatchRequest
Expand All @@ -14,6 +15,7 @@ import io.sakurasou.model.dto.SettingUpdateDTO
import io.sakurasou.model.setting.SiteSetting
import io.sakurasou.model.setting.StrategySetting
import io.sakurasou.model.setting.SystemSetting
import io.sakurasou.model.setting.SystemStatus
import kotlinx.datetime.Clock
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
Expand Down Expand Up @@ -41,13 +43,25 @@ class SettingServiceImpl(
}
}

override suspend fun updateSiteSetting(siteSetting: SiteSettingPatchRequest) {
override suspend fun updateSiteSetting(siteSetting: SiteSetting) {
val now = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault())
val settingUpdateDTO = SettingUpdateDTO(
name = SETTING_SITE,
config = siteSetting,
updateTime = now
)
dbQuery {
settingDao.updateSettingByName(settingUpdateDTO)
}
}

override suspend fun updateSiteSetting(siteSettingPatch: SiteSettingPatchRequest) {
val siteSettingConfig = SiteSetting(
siteTitle = siteSetting.siteTitle,
siteSubtitle = siteSetting.siteSubtitle,
siteDescription = siteSetting.siteDescription,
siteKeyword = siteSetting.siteKeyword,
homePageRandomPicDisplay = siteSetting.homePageRandomPicDisplay
siteTitle = siteSettingPatch.siteTitle,
siteSubtitle = siteSettingPatch.siteSubtitle,
siteDescription = siteSettingPatch.siteDescription,
siteKeyword = siteSettingPatch.siteKeyword,
homePageRandomPicDisplay = siteSettingPatch.homePageRandomPicDisplay
)
val now = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault())
val settingUpdateDTO = SettingUpdateDTO(
Expand Down Expand Up @@ -75,6 +89,18 @@ class SettingServiceImpl(
}
}

override suspend fun updateSystemStatus(systemStatus: SystemStatus) {
val now = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault())
val settingUpdateDTO = SettingUpdateDTO(
name = SETTING_STATUS,
config = systemStatus,
updateTime = now
)
dbQuery {
settingDao.updateSettingByName(settingUpdateDTO)
}
}

override suspend fun getSystemSetting(): SystemSetting {
return dbQuery {
settingDao.getSettingByName(SETTING_SYSTEM)
Expand All @@ -101,4 +127,13 @@ class SettingServiceImpl(
it.config
} ?: run { throw MissingNecessaryColumnException() }
}

override suspend fun getSystemStatus(): SystemStatus {
return dbQuery {
settingDao.getSettingByName(SETTING_STATUS)
}?.let {
if (it.config !is SystemStatus) throw ConfigTypeNotMatchException()
it.config
} ?: run { throw MissingNecessaryColumnException() }
}
}

0 comments on commit fbae82a

Please sign in to comment.