From d37935483f97b0aa9a46c599b2825213434796ad Mon Sep 17 00:00:00 2001 From: Shiina Kin Date: Tue, 24 Sep 2024 16:19:37 +0800 Subject: [PATCH] feat: remove incorrectly used dbQueryInner function --- .../io/sakurasou/config/InstanceCenter.kt | 10 +++++----- .../io/sakurasou/model/DatabaseSingleton.kt | 2 -- .../service/common/CommonServiceImpl.kt | 15 +++++++-------- .../service/group/GroupServiceImpl.kt | 10 +++++++--- .../sakurasou/service/role/RoleServiceImpl.kt | 13 ++++++------- .../sakurasou/service/user/UserServiceImpl.kt | 11 +++++------ .../service/common/CommonServiceTest.kt | 19 ++++++------------- .../service/group/GroupServiceTest.kt | 6 ------ .../sakurasou/service/role/RoleServiceTest.kt | 12 ------------ .../sakurasou/service/user/UserServiceTest.kt | 18 ++++++------------ 10 files changed, 42 insertions(+), 74 deletions(-) diff --git a/app/src/main/kotlin/io/sakurasou/config/InstanceCenter.kt b/app/src/main/kotlin/io/sakurasou/config/InstanceCenter.kt index 9b721b53..a2dea2b0 100644 --- a/app/src/main/kotlin/io/sakurasou/config/InstanceCenter.kt +++ b/app/src/main/kotlin/io/sakurasou/config/InstanceCenter.kt @@ -1,7 +1,6 @@ package io.sakurasou.config import io.sakurasou.model.DatabaseSingleton.dbQuery -import io.sakurasou.model.DatabaseSingleton.dbQueryInner import io.sakurasou.model.dao.album.AlbumDao import io.sakurasou.model.dao.album.AlbumDaoImpl import io.sakurasou.model.dao.group.GroupDao @@ -90,9 +89,10 @@ object InstanceCenter { authService = AuthServiceImpl(userDao, relationDao) groupService = GroupServiceImpl(groupDao, relationDao) + userService = UserServiceImpl(userDao, albumDao, settingService) + commonService = CommonServiceImpl(userDao, albumDao, settingService) + roleService = RoleServiceImpl(roleDao, permissionDao, relationDao) - userService = UserServiceImpl(userDao, albumService, settingService) - commonService = CommonServiceImpl(userDao, albumService, settingService) } fun initSystemStatus() { @@ -104,9 +104,9 @@ object InstanceCenter { fun initRolePermissions() { rolePermissions = runBlocking { dbQuery { - val roles = dbQueryInner { roleDao.listRoles() } + val roles = roleDao.listRoles() roles.associate { - it.name to dbQueryInner { relationDao.listPermissionByRole(it.name).toSet() } + it.name to relationDao.listPermissionByRole(it.name).toSet() } } } diff --git a/app/src/main/kotlin/io/sakurasou/model/DatabaseSingleton.kt b/app/src/main/kotlin/io/sakurasou/model/DatabaseSingleton.kt index 4ed4e212..f93aea4c 100644 --- a/app/src/main/kotlin/io/sakurasou/model/DatabaseSingleton.kt +++ b/app/src/main/kotlin/io/sakurasou/model/DatabaseSingleton.kt @@ -32,7 +32,5 @@ object DatabaseSingleton { } } - suspend fun dbQueryInner(block: suspend () -> T): T = newSuspendedTransaction { block() } - suspend fun dbQuery(block: suspend () -> T): T = newSuspendedTransaction(Dispatchers.IO) { block() } } \ No newline at end of file diff --git a/app/src/main/kotlin/io/sakurasou/service/common/CommonServiceImpl.kt b/app/src/main/kotlin/io/sakurasou/service/common/CommonServiceImpl.kt index ba1837f2..77529fcf 100644 --- a/app/src/main/kotlin/io/sakurasou/service/common/CommonServiceImpl.kt +++ b/app/src/main/kotlin/io/sakurasou/service/common/CommonServiceImpl.kt @@ -5,12 +5,11 @@ import io.sakurasou.config.InstanceCenter import io.sakurasou.controller.request.SiteInitRequest import io.sakurasou.exception.SiteRepeatedInitializationException import io.sakurasou.model.DatabaseSingleton.dbQuery -import io.sakurasou.model.DatabaseSingleton.dbQueryInner +import io.sakurasou.model.dao.album.AlbumDao import io.sakurasou.model.dao.user.UserDao import io.sakurasou.model.dto.UserInsertDTO import io.sakurasou.model.setting.SiteSetting import io.sakurasou.model.setting.SystemStatus -import io.sakurasou.service.album.AlbumService import io.sakurasou.service.setting.SettingService import kotlinx.datetime.Clock import kotlinx.datetime.TimeZone @@ -22,7 +21,7 @@ import kotlinx.datetime.toLocalDateTime */ class CommonServiceImpl( private val userDao: UserDao, - private val albumService: AlbumService, + private val albumDao: AlbumDao, private val settingService: SettingService ) : CommonService { override suspend fun initSite(siteInitRequest: SiteInitRequest) { @@ -58,11 +57,11 @@ class CommonServiceImpl( ) dbQuery { - val userId = dbQueryInner { userDao.saveUser(userInsertDTO) } - val defaultAlbumId = dbQueryInner { albumService.initAlbumForUser(userId) } - dbQueryInner { userDao.updateUserDefaultAlbumId(userId, defaultAlbumId) } - dbQueryInner { settingService.updateSiteSetting(siteSettingConfig) } - dbQueryInner { settingService.updateSystemStatus(systemStatus) } + val userId = userDao.saveUser(userInsertDTO) + val defaultAlbumId = albumDao.initAlbumForUser(userId) + userDao.updateUserDefaultAlbumId(userId, defaultAlbumId) + settingService.updateSiteSetting(siteSettingConfig) + settingService.updateSystemStatus(systemStatus) } InstanceCenter.systemStatus = systemStatus } diff --git a/app/src/main/kotlin/io/sakurasou/service/group/GroupServiceImpl.kt b/app/src/main/kotlin/io/sakurasou/service/group/GroupServiceImpl.kt index 44923e54..80630427 100644 --- a/app/src/main/kotlin/io/sakurasou/service/group/GroupServiceImpl.kt +++ b/app/src/main/kotlin/io/sakurasou/service/group/GroupServiceImpl.kt @@ -7,8 +7,8 @@ import io.sakurasou.controller.vo.GroupPageVO import io.sakurasou.controller.vo.GroupVO import io.sakurasou.controller.vo.PageResult import io.sakurasou.exception.GroupNotExistException +import io.sakurasou.exception.RoleNotExistException import io.sakurasou.model.DatabaseSingleton.dbQuery -import io.sakurasou.model.DatabaseSingleton.dbQueryInner import io.sakurasou.model.dao.group.GroupDao import io.sakurasou.model.dao.relation.RelationDao import io.sakurasou.model.dto.GroupInsertDTO @@ -32,8 +32,12 @@ class GroupServiceImpl( val groupRoles = insertRequest.roles dbQuery { - val groupId = dbQueryInner { groupDao.saveGroup(groupInsertDTO) } - dbQueryInner { relationDao.batchInsertGroupToRoles(groupId, groupRoles) } + val groupId = groupDao.saveGroup(groupInsertDTO) + runCatching { + relationDao.batchInsertGroupToRoles(groupId, groupRoles) + }.onFailure { + throw RoleNotExistException() + } } } diff --git a/app/src/main/kotlin/io/sakurasou/service/role/RoleServiceImpl.kt b/app/src/main/kotlin/io/sakurasou/service/role/RoleServiceImpl.kt index ea6c1735..8a8611f9 100644 --- a/app/src/main/kotlin/io/sakurasou/service/role/RoleServiceImpl.kt +++ b/app/src/main/kotlin/io/sakurasou/service/role/RoleServiceImpl.kt @@ -5,7 +5,6 @@ import io.sakurasou.controller.vo.RoleVO import io.sakurasou.exception.PermissionNotExistException import io.sakurasou.exception.RoleNotExistException import io.sakurasou.model.DatabaseSingleton.dbQuery -import io.sakurasou.model.DatabaseSingleton.dbQueryInner import io.sakurasou.model.dao.permission.PermissionDao import io.sakurasou.model.dao.relation.RelationDao import io.sakurasou.model.dao.role.RoleDao @@ -21,11 +20,11 @@ class RoleServiceImpl( ) : RoleService { override suspend fun listRolesWithPermissions(): Map { return dbQuery { - val roles = dbQueryInner { roleDao.listRoles() } + val roles = roleDao.listRoles() roles.associate { role -> - val permissionNames = dbQueryInner { relationDao.listPermissionByRole(role.name) } + val permissionNames = relationDao.listPermissionByRole(role.name) val permissionVOList = permissionNames.map { permissionName -> - val permission = dbQueryInner { permissionDao.findPermissionByName(permissionName) } + val permission = permissionDao.findPermissionByName(permissionName) ?: throw PermissionNotExistException() PermissionVO( permission.name, @@ -44,10 +43,10 @@ class RoleServiceImpl( override suspend fun listRolesWithPermissionsOfUser(roleNames: List): Map { return dbQuery { roleNames.associate { roleName -> - val role = dbQueryInner { roleDao.findRoleByName(roleName) } ?: throw RoleNotExistException() - val permissionNames = dbQueryInner { relationDao.listPermissionByRole(roleName) } + val role = roleDao.findRoleByName(roleName) ?: throw RoleNotExistException() + val permissionNames = relationDao.listPermissionByRole(roleName) val permissionVOList = permissionNames.map { permissionName -> - val permission = dbQueryInner { permissionDao.findPermissionByName(permissionName) } + val permission = permissionDao.findPermissionByName(permissionName) ?: throw PermissionNotExistException() PermissionVO( permission.name, diff --git a/app/src/main/kotlin/io/sakurasou/service/user/UserServiceImpl.kt b/app/src/main/kotlin/io/sakurasou/service/user/UserServiceImpl.kt index 7a09a86c..b7fccca6 100644 --- a/app/src/main/kotlin/io/sakurasou/service/user/UserServiceImpl.kt +++ b/app/src/main/kotlin/io/sakurasou/service/user/UserServiceImpl.kt @@ -4,10 +4,9 @@ import at.favre.lib.crypto.bcrypt.BCrypt import io.sakurasou.controller.request.UserInsertRequest import io.sakurasou.exception.SignupNotAllowedException import io.sakurasou.model.DatabaseSingleton.dbQuery -import io.sakurasou.model.DatabaseSingleton.dbQueryInner +import io.sakurasou.model.dao.album.AlbumDao import io.sakurasou.model.dao.user.UserDao import io.sakurasou.model.dto.UserInsertDTO -import io.sakurasou.service.album.AlbumService import io.sakurasou.service.setting.SettingService import kotlinx.datetime.Clock import kotlinx.datetime.TimeZone @@ -19,7 +18,7 @@ import kotlinx.datetime.toLocalDateTime */ class UserServiceImpl( private val userDao: UserDao, - private val albumService: AlbumService, + private val albumDao: AlbumDao, private val settingService: SettingService ) : UserService { override suspend fun saveUser(userInsertRequest: UserInsertRequest) { @@ -42,9 +41,9 @@ class UserServiceImpl( updateTime = now ) dbQuery { - val userId = dbQueryInner { userDao.saveUser(userInsertDTO) } - val defaultAlbumId = dbQueryInner { albumService.initAlbumForUser(userId) } - dbQueryInner { userDao.updateUserDefaultAlbumId(userId, defaultAlbumId) } + val userId = userDao.saveUser(userInsertDTO) + val defaultAlbumId = albumDao.initAlbumForUser(userId) + userDao.updateUserDefaultAlbumId(userId, defaultAlbumId) } } diff --git a/app/src/test/kotlin/io/sakurasou/service/common/CommonServiceTest.kt b/app/src/test/kotlin/io/sakurasou/service/common/CommonServiceTest.kt index 28e752e2..db59050d 100644 --- a/app/src/test/kotlin/io/sakurasou/service/common/CommonServiceTest.kt +++ b/app/src/test/kotlin/io/sakurasou/service/common/CommonServiceTest.kt @@ -5,11 +5,11 @@ import io.mockk.* import io.sakurasou.controller.request.SiteInitRequest import io.sakurasou.exception.SiteRepeatedInitializationException import io.sakurasou.model.DatabaseSingleton +import io.sakurasou.model.dao.album.AlbumDao import io.sakurasou.model.dao.user.UserDao import io.sakurasou.model.dto.UserInsertDTO import io.sakurasou.model.setting.SiteSetting import io.sakurasou.model.setting.SystemStatus -import io.sakurasou.service.album.AlbumService import io.sakurasou.service.setting.SettingService import kotlinx.coroutines.runBlocking import kotlinx.datetime.Clock @@ -25,7 +25,7 @@ import kotlin.test.assertFailsWith */ class CommonServiceTest { private lateinit var userDao: UserDao - private lateinit var albumService: AlbumService + private lateinit var albumDao: AlbumDao private lateinit var settingService: SettingService private lateinit var commonService: CommonServiceImpl @@ -35,18 +35,12 @@ class CommonServiceTest { mockkStatic(BCrypt::class) mockkObject(Clock.System) userDao = mockk() - albumService = mockk() + albumDao = mockk() settingService = mockk() - commonService = CommonServiceImpl(userDao, albumService, settingService) + commonService = CommonServiceImpl(userDao, albumDao, settingService) coEvery { DatabaseSingleton.dbQuery(any()) } coAnswers { this.arg Unit>(0).invoke() } - coEvery { DatabaseSingleton.dbQueryInner(any()) } coAnswers { - this.arg Unit>(0).invoke() - } - coEvery { DatabaseSingleton.dbQueryInner(any()) } coAnswers { - this.arg Long>(0).invoke() - } } @Test @@ -110,7 +104,7 @@ class CommonServiceTest { coEvery { settingService.getSiteSetting() } returns oldSiteSetting every { BCrypt.withDefaults().hashToString(12, siteInitRequest.password.toCharArray()) } returns encodedPassword coEvery { userDao.saveUser(userInsertDTO) } returns 1 - coEvery { albumService.initAlbumForUser(1) } returns 1 + coEvery { albumDao.initAlbumForUser(1) } returns 1 coEvery { userDao.updateUserDefaultAlbumId(1L, 1L) } just Runs coEvery { settingService.updateSiteSetting(siteSettingConfig) } just Runs coEvery { settingService.updateSystemStatus(systemStatus) } just Runs @@ -119,10 +113,9 @@ class CommonServiceTest { coVerify(exactly = 1) { DatabaseSingleton.dbQuery(any()) } coVerify(exactly = 1) { userDao.saveUser(userInsertDTO) } - coVerify(exactly = 1) { albumService.initAlbumForUser(1) } + coVerify(exactly = 1) { albumDao.initAlbumForUser(1) } coVerify(exactly = 1) { userDao.updateUserDefaultAlbumId(1L, 1L) } coVerify(exactly = 1) { settingService.updateSiteSetting(siteSettingConfig) } coVerify(exactly = 1) { settingService.updateSystemStatus(systemStatus) } } - } \ No newline at end of file diff --git a/app/src/test/kotlin/io/sakurasou/service/group/GroupServiceTest.kt b/app/src/test/kotlin/io/sakurasou/service/group/GroupServiceTest.kt index 644f796b..b175fe2e 100644 --- a/app/src/test/kotlin/io/sakurasou/service/group/GroupServiceTest.kt +++ b/app/src/test/kotlin/io/sakurasou/service/group/GroupServiceTest.kt @@ -61,12 +61,6 @@ class GroupServiceTest { coEvery { DatabaseSingleton.dbQuery(any()) } coAnswers { this.arg Unit>(0).invoke() } - coEvery { DatabaseSingleton.dbQueryInner(any()) } coAnswers { - this.arg Unit>(0).invoke() - } - coEvery { DatabaseSingleton.dbQueryInner(any()) } coAnswers { - this.arg Long>(0).invoke() - } every { groupDao.saveGroup(exceptedInsertDTO) } returns 1 every { relationDao.batchInsertGroupToRoles(1, exceptedRoles) } just Runs diff --git a/app/src/test/kotlin/io/sakurasou/service/role/RoleServiceTest.kt b/app/src/test/kotlin/io/sakurasou/service/role/RoleServiceTest.kt index c4cea97b..775725e1 100644 --- a/app/src/test/kotlin/io/sakurasou/service/role/RoleServiceTest.kt +++ b/app/src/test/kotlin/io/sakurasou/service/role/RoleServiceTest.kt @@ -69,12 +69,6 @@ class RoleServiceTest { coEvery { DatabaseSingleton.dbQuery>(any()) } coAnswers { this.arg Map>(0).invoke() } - coEvery { DatabaseSingleton.dbQueryInner>(any()) } coAnswers { - this.arg List>(0).invoke() - } - coEvery { DatabaseSingleton.dbQueryInner(any()) } coAnswers { - this.arg Permission?>(0).invoke() - } val rolesWithPermissions = roleService.listRolesWithPermissions() @@ -100,12 +94,6 @@ class RoleServiceTest { coEvery { DatabaseSingleton.dbQuery>(any()) } coAnswers { this.arg Map>(0).invoke() } - coEvery { DatabaseSingleton.dbQueryInner>(any()) } coAnswers { - this.arg List>(0).invoke() - } - coEvery { DatabaseSingleton.dbQueryInner(any()) } coAnswers { - this.arg Permission?>(0).invoke() - } val rolesWithPermissions = roleService.listRolesWithPermissionsOfUser(listOf(role1.name)) diff --git a/app/src/test/kotlin/io/sakurasou/service/user/UserServiceTest.kt b/app/src/test/kotlin/io/sakurasou/service/user/UserServiceTest.kt index 174a6a85..8e47263e 100644 --- a/app/src/test/kotlin/io/sakurasou/service/user/UserServiceTest.kt +++ b/app/src/test/kotlin/io/sakurasou/service/user/UserServiceTest.kt @@ -5,10 +5,10 @@ import io.mockk.* import io.sakurasou.controller.request.UserInsertRequest import io.sakurasou.exception.SignupNotAllowedException import io.sakurasou.model.DatabaseSingleton +import io.sakurasou.model.dao.album.AlbumDao import io.sakurasou.model.dao.user.UserDao import io.sakurasou.model.dto.UserInsertDTO import io.sakurasou.model.setting.SystemSetting -import io.sakurasou.service.album.AlbumService import io.sakurasou.service.setting.SettingService import kotlinx.coroutines.runBlocking import kotlinx.datetime.Clock @@ -24,7 +24,7 @@ import kotlin.test.assertFailsWith */ class UserServiceTest { private lateinit var userDao: UserDao - private lateinit var albumService: AlbumService + private lateinit var albumDao: AlbumDao private lateinit var settingService: SettingService private lateinit var userService: UserServiceImpl @@ -34,18 +34,12 @@ class UserServiceTest { mockkStatic(BCrypt::class) mockkObject(Clock.System) userDao = mockk() - albumService = mockk() + albumDao = mockk() settingService = mockk() - userService = UserServiceImpl(userDao, albumService, settingService) + userService = UserServiceImpl(userDao, albumDao, settingService) coEvery { DatabaseSingleton.dbQuery(any()) } coAnswers { this.arg Unit>(0).invoke() } - coEvery { DatabaseSingleton.dbQueryInner(any()) } coAnswers { - this.arg Unit>(0).invoke() - } - coEvery { DatabaseSingleton.dbQueryInner(any()) } coAnswers { - this.arg Long>(0).invoke() - } } @Test @@ -99,14 +93,14 @@ class UserServiceTest { BCrypt.withDefaults().hashToString(12, userInsertRequest.password.toCharArray()) } returns encodedPassword coEvery { userDao.saveUser(userInsertDTO) } returns 1 - coEvery { albumService.initAlbumForUser(1L) } returns 1 + coEvery { albumDao.initAlbumForUser(1L) } returns 1 coEvery { userDao.updateUserDefaultAlbumId(1L, 1L) } just Runs userService.saveUser(userInsertRequest) coVerify(exactly = 1) { DatabaseSingleton.dbQuery(any()) } coVerify(exactly = 1) { userDao.saveUser(userInsertDTO) } - coVerify(exactly = 1) { albumService.initAlbumForUser(1L) } + coVerify(exactly = 1) { albumDao.initAlbumForUser(1L) } coVerify(exactly = 1) { userDao.updateUserDefaultAlbumId(1L, 1L) } } } \ No newline at end of file