-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: unified management of service instantiation
- Loading branch information
Showing
15 changed files
with
205 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
app/src/main/kotlin/io/sakurasou/service/album/AlbumService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.sakurasou.service.album | ||
|
||
/** | ||
* @author Shiina Kin | ||
* 2024/9/13 14:48 | ||
*/ | ||
interface AlbumService { | ||
suspend fun initAlbumForUser(userId: Long) | ||
suspend fun saveAlbum() | ||
} |
28 changes: 28 additions & 0 deletions
28
app/src/main/kotlin/io/sakurasou/service/album/AlbumServiceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.sakurasou.service.album | ||
|
||
import io.sakurasou.model.DatabaseSingleton.dbQuery | ||
import io.sakurasou.model.dao.album.AlbumDao | ||
import io.sakurasou.model.dto.AlbumInsertDTO | ||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.TimeZone | ||
import kotlinx.datetime.toLocalDateTime | ||
|
||
/** | ||
* @author Shiina Kin | ||
* 2024/9/13 14:48 | ||
*/ | ||
class AlbumServiceImpl( | ||
private val albumDao: AlbumDao | ||
) : AlbumService { | ||
override suspend fun initAlbumForUser(userId: Long) { | ||
val now = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()) | ||
val uncategorizedAlbum = AlbumInsertDTO(userId, "uncategorized", "default, cannot delete", 0, now) | ||
dbQuery { | ||
albumDao.saveAlbum(uncategorizedAlbum) | ||
} | ||
} | ||
|
||
override suspend fun saveAlbum() { | ||
TODO("Not yet implemented") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
app/src/main/kotlin/io/sakurasou/service/common/CommonService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.sakurasou.service.common | ||
|
||
import io.sakurasou.controller.request.SiteInitRequest | ||
|
||
/** | ||
* @author Shiina Kin | ||
* 2024/9/13 16:23 | ||
*/ | ||
interface CommonService { | ||
suspend fun initSite(siteInitRequest: SiteInitRequest) | ||
} |
63 changes: 63 additions & 0 deletions
63
app/src/main/kotlin/io/sakurasou/service/common/CommonServiceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.sakurasou.service.common | ||
|
||
import at.favre.lib.crypto.bcrypt.BCrypt | ||
import io.sakurasou.controller.request.SiteInitRequest | ||
import io.sakurasou.exception.SiteRepeatedInitializationException | ||
import io.sakurasou.model.DatabaseSingleton.dbQuery | ||
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 | ||
import kotlinx.datetime.toLocalDateTime | ||
|
||
/** | ||
* @author Shiina Kin | ||
* 2024/9/13 15:34 | ||
*/ | ||
class CommonServiceImpl( | ||
private val userDao: UserDao, | ||
private val albumService: AlbumService, | ||
private val settingService: SettingService | ||
) : CommonService { | ||
override suspend fun initSite(siteInitRequest: SiteInitRequest) { | ||
val status = settingService.getSystemStatus() | ||
if (status.isInit) throw SiteRepeatedInitializationException() | ||
|
||
val rawPassword = siteInitRequest.password | ||
val encodePassword = BCrypt.withDefaults().hashToString(12, rawPassword.toCharArray()) | ||
|
||
val now = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()) | ||
val userInsertDTO = UserInsertDTO( | ||
groupId = 1, | ||
username = siteInitRequest.username, | ||
password = encodePassword, | ||
email = siteInitRequest.email, | ||
createTime = now, | ||
updateTime = now | ||
) | ||
|
||
val oldSiteSetting = settingService.getSiteSetting() | ||
val siteSettingConfig = SiteSetting( | ||
siteTitle = siteInitRequest.siteTitle, | ||
siteSubtitle = siteInitRequest.siteSubtitle, | ||
siteDescription = siteInitRequest.siteDescription, | ||
siteKeyword = oldSiteSetting.siteKeyword, | ||
homePageRandomPicDisplay = oldSiteSetting.homePageRandomPicDisplay | ||
) | ||
|
||
val systemStatus = SystemStatus( | ||
isInit = true | ||
) | ||
|
||
dbQuery { | ||
val userId = userDao.saveUser(userInsertDTO) | ||
albumService.initAlbumForUser(userId) | ||
settingService.updateSiteSetting(siteSettingConfig) | ||
settingService.updateSystemStatus(systemStatus) | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/kotlin/io/sakurasou/service/group/GroupService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.sakurasou.service.group | ||
|
||
/** | ||
* @author Shiina Kin | ||
* 2024/9/13 14:47 | ||
*/ | ||
interface GroupService { | ||
fun saveGroup() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
package io.sakurasou.service.user | ||
|
||
import io.sakurasou.controller.request.UserInsertRequest | ||
|
||
/** | ||
* @author ShiinaKin | ||
* 2024/9/5 15:30 | ||
*/ | ||
interface UserService { | ||
|
||
suspend fun saveUser(userInsertRequest: UserInsertRequest) | ||
} |
Oops, something went wrong.