From 0e5d7c92f61eef459dfe2dc51dfe2d9a229e0ee2 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sat, 30 Sep 2023 13:41:42 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Exposed=E3=81=A7=E5=88=9D=E6=9C=9F?= =?UTF-8?q?=E5=8C=96=E3=82=92=E8=87=AA=E5=8B=95=E3=81=A7=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev/usbharu/hideout/config/SpringConfig.kt | 12 ------------ .../hideout/repository/MetaRepositoryImpl.kt | 15 +++++---------- .../hideout/repository/PostRepositoryImpl.kt | 10 +--------- .../hideout/repository/ReactionRepositoryImpl.kt | 8 -------- .../repository/RegisteredClientRepositoryImpl.kt | 9 +-------- .../hideout/repository/UserRepositoryImpl.kt | 13 +------------ .../ExposedOAuth2AuthorizationConsentService.kt | 9 --------- .../auth/ExposedOAuth2AuthorizationService.kt | 8 -------- src/main/resources/application.yml | 3 ++- 9 files changed, 10 insertions(+), 77 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/config/SpringConfig.kt b/src/main/kotlin/dev/usbharu/hideout/config/SpringConfig.kt index 63825da0..50ae6f03 100644 --- a/src/main/kotlin/dev/usbharu/hideout/config/SpringConfig.kt +++ b/src/main/kotlin/dev/usbharu/hideout/config/SpringConfig.kt @@ -1,9 +1,7 @@ package dev.usbharu.hideout.config -import org.jetbrains.exposed.sql.Database import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.context.properties.ConfigurationProperties -import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import java.net.URL @@ -15,16 +13,6 @@ class SpringConfig { @Autowired lateinit var config: ApplicationConfig - - @Bean - fun database(): Database { - return Database.connect( - url = dbConfig.url, - driver = dbConfig.driver, - user = dbConfig.user, - password = dbConfig.password - ) - } } @ConfigurationProperties("hideout") diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/MetaRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/MetaRepositoryImpl.kt index 07200178..03864cfb 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/MetaRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/MetaRepositoryImpl.kt @@ -1,20 +1,15 @@ package dev.usbharu.hideout.repository import dev.usbharu.hideout.domain.model.hideout.entity.Jwt -import org.jetbrains.exposed.sql.* -import org.jetbrains.exposed.sql.transactions.transaction +import org.jetbrains.exposed.sql.Table +import org.jetbrains.exposed.sql.insert +import org.jetbrains.exposed.sql.select +import org.jetbrains.exposed.sql.update import org.springframework.stereotype.Repository import java.util.* @Repository -class MetaRepositoryImpl(private val database: Database) : MetaRepository { - - init { - transaction(database) { - SchemaUtils.create(Meta) - SchemaUtils.createMissingTablesAndColumns(Meta) - } - } +class MetaRepositoryImpl : MetaRepository { override suspend fun save(meta: dev.usbharu.hideout.domain.model.hideout.entity.Meta) { if (Meta.select { Meta.id eq 1 }.empty()) { diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt index 08c7d279..e4aad089 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt @@ -6,18 +6,10 @@ import dev.usbharu.hideout.exception.FailedToGetResourcesException import dev.usbharu.hideout.service.core.IdGenerateService import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq -import org.jetbrains.exposed.sql.transactions.transaction import org.springframework.stereotype.Repository @Repository -class PostRepositoryImpl(database: Database, private val idGenerateService: IdGenerateService) : PostRepository { - - init { - transaction(database) { - SchemaUtils.create(Posts) - SchemaUtils.createMissingTablesAndColumns(Posts) - } - } +class PostRepositoryImpl(private val idGenerateService: IdGenerateService) : PostRepository { override suspend fun generateId(): Long = idGenerateService.generateId() diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/ReactionRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/ReactionRepositoryImpl.kt index 26fc3e52..866feaf3 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/ReactionRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/ReactionRepositoryImpl.kt @@ -5,21 +5,13 @@ import dev.usbharu.hideout.service.core.IdGenerateService import org.jetbrains.exposed.dao.id.LongIdTable import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq -import org.jetbrains.exposed.sql.transactions.transaction import org.springframework.stereotype.Repository @Repository class ReactionRepositoryImpl( - private val database: Database, private val idGenerateService: IdGenerateService ) : ReactionRepository { - init { - transaction(database) { - SchemaUtils.create(Reactions) - SchemaUtils.createMissingTablesAndColumns(Reactions) - } - } override suspend fun generateId(): Long = idGenerateService.generateId() diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/RegisteredClientRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/RegisteredClientRepositoryImpl.kt index 88aa66c2..5acfd3fc 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/RegisteredClientRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/RegisteredClientRepositoryImpl.kt @@ -10,7 +10,6 @@ import dev.usbharu.hideout.service.auth.ExposedOAuth2AuthorizationService import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.javatime.CurrentTimestamp import org.jetbrains.exposed.sql.javatime.timestamp -import org.jetbrains.exposed.sql.transactions.transaction import org.slf4j.LoggerFactory import org.springframework.security.jackson2.SecurityJackson2Modules import org.springframework.security.oauth2.core.AuthorizationGrantType @@ -27,14 +26,8 @@ import java.time.Instant import org.springframework.security.oauth2.server.authorization.client.RegisteredClient as SpringRegisteredClient @Repository -class RegisteredClientRepositoryImpl(private val database: Database) : RegisteredClientRepository { +class RegisteredClientRepositoryImpl : RegisteredClientRepository { - init { - transaction(database) { - SchemaUtils.create(RegisteredClient) - SchemaUtils.createMissingTablesAndColumns(RegisteredClient) - } - } override fun save(registeredClient: SpringRegisteredClient?) { requireNotNull(registeredClient) diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/UserRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/UserRepositoryImpl.kt index 8fdbebc1..a5cbb82c 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/UserRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/UserRepositoryImpl.kt @@ -6,23 +6,12 @@ import dev.usbharu.hideout.service.core.IdGenerateService import org.jetbrains.exposed.dao.id.LongIdTable import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq -import org.jetbrains.exposed.sql.transactions.transaction import org.springframework.stereotype.Repository import java.time.Instant @Repository -class UserRepositoryImpl(private val database: Database, private val idGenerateService: IdGenerateService) : +class UserRepositoryImpl(private val idGenerateService: IdGenerateService) : UserRepository { - init { - transaction(database) { - SchemaUtils.create(Users) - SchemaUtils.create(UsersFollowers) - SchemaUtils.createMissingTablesAndColumns(Users) - SchemaUtils.createMissingTablesAndColumns(UsersFollowers) - SchemaUtils.create(FollowRequests) - SchemaUtils.createMissingTablesAndColumns(FollowRequests) - } - } override suspend fun save(user: User): User { val singleOrNull = Users.select { Users.id eq user.id }.singleOrNull() diff --git a/src/main/kotlin/dev/usbharu/hideout/service/auth/ExposedOAuth2AuthorizationConsentService.kt b/src/main/kotlin/dev/usbharu/hideout/service/auth/ExposedOAuth2AuthorizationConsentService.kt index b3b0d420..469de633 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/auth/ExposedOAuth2AuthorizationConsentService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/auth/ExposedOAuth2AuthorizationConsentService.kt @@ -4,7 +4,6 @@ import dev.usbharu.hideout.service.core.Transaction import kotlinx.coroutines.runBlocking import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq -import org.jetbrains.exposed.sql.transactions.transaction import org.springframework.security.core.authority.SimpleGrantedAuthority import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsentService import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository @@ -15,17 +14,9 @@ import org.springframework.security.oauth2.server.authorization.OAuth2Authorizat class ExposedOAuth2AuthorizationConsentService( private val registeredClientRepository: RegisteredClientRepository, private val transaction: Transaction, - private val database: Database ) : OAuth2AuthorizationConsentService { - init { - transaction(database) { - SchemaUtils.create(OAuth2AuthorizationConsent) - SchemaUtils.createMissingTablesAndColumns(OAuth2AuthorizationConsent) - } - } - override fun save(authorizationConsent: AuthorizationConsent?) = runBlocking { requireNotNull(authorizationConsent) transaction.transaction { diff --git a/src/main/kotlin/dev/usbharu/hideout/service/auth/ExposedOAuth2AuthorizationService.kt b/src/main/kotlin/dev/usbharu/hideout/service/auth/ExposedOAuth2AuthorizationService.kt index 13ea2820..61e8d41d 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/auth/ExposedOAuth2AuthorizationService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/auth/ExposedOAuth2AuthorizationService.kt @@ -10,7 +10,6 @@ import kotlinx.coroutines.runBlocking import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.javatime.timestamp -import org.jetbrains.exposed.sql.transactions.transaction import org.springframework.security.jackson2.CoreJackson2Module import org.springframework.security.jackson2.SecurityJackson2Modules import org.springframework.security.oauth2.core.* @@ -29,16 +28,9 @@ import org.springframework.stereotype.Service class ExposedOAuth2AuthorizationService( private val registeredClientRepository: RegisteredClientRepository, private val transaction: Transaction, - private val database: Database ) : OAuth2AuthorizationService { - init { - transaction(database) { - SchemaUtils.create(Authorization) - SchemaUtils.createMissingTablesAndColumns(Authorization) - } - } @Suppress("LongMethod", "CyclomaticComplexMethod") override fun save(authorization: OAuth2Authorization?): Unit = runBlocking { diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 889d71f4..1e16cf88 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -33,8 +33,9 @@ spring: h2: console: enabled: true + exposed: + generate-ddl: true server: - tomcat: basedir: tomcat accesslog: