refactor: AbstractRepositoryを改善

This commit is contained in:
usbharu 2024-09-15 11:29:14 +09:00
parent 2c10fc5330
commit 56a4e94e41
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
17 changed files with 21 additions and 71 deletions

View File

@ -29,9 +29,7 @@ import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
@Repository @Repository
class ExposedPrincipalQueryService : PrincipalQueryService, AbstractRepository() { class ExposedPrincipalQueryService : PrincipalQueryService, AbstractRepository(logger) {
override val logger: Logger
get() = Companion.logger
override suspend fun findByUserDetailId(userDetailId: UserDetailId): PrincipalDTO { override suspend fun findByUserDetailId(userDetailId: UserDetailId): PrincipalDTO {
return query { return query {

View File

@ -25,16 +25,13 @@ import dev.usbharu.hideout.core.infrastructure.exposedrepository.toCustomEmojiOr
import dev.usbharu.hideout.core.infrastructure.exposedrepository.toReaction import dev.usbharu.hideout.core.infrastructure.exposedrepository.toReaction
import dev.usbharu.hideout.core.query.reactions.ReactionsQueryService import dev.usbharu.hideout.core.query.reactions.ReactionsQueryService
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
import java.net.URI import java.net.URI
import dev.usbharu.hideout.core.infrastructure.exposedrepository.Reactions as ExposedrepositoryReactions import dev.usbharu.hideout.core.infrastructure.exposedrepository.Reactions as ExposedrepositoryReactions
@Repository @Repository
class ExposedReactionsQueryService : ReactionsQueryService, AbstractRepository() { class ExposedReactionsQueryService : ReactionsQueryService, AbstractRepository(logger) {
override val logger: Logger
get() = Companion.logger
override suspend fun findAllByPostId(postId: PostId): List<Reactions> { override suspend fun findAllByPostId(postId: PostId): List<Reactions> {
return query { return query {

View File

@ -25,17 +25,12 @@ import dev.usbharu.hideout.core.domain.model.support.principal.Principal
import dev.usbharu.hideout.core.infrastructure.exposedrepository.* import dev.usbharu.hideout.core.infrastructure.exposedrepository.*
import dev.usbharu.hideout.core.query.usertimeline.UserTimelineQueryService import dev.usbharu.hideout.core.query.usertimeline.UserTimelineQueryService
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
import java.net.URI import java.net.URI
@Repository @Repository
class ExposedUserTimelineQueryService : UserTimelineQueryService, AbstractRepository() { class ExposedUserTimelineQueryService : UserTimelineQueryService, AbstractRepository(logger) {
override val logger: Logger
get() = Companion.logger
protected fun authorizedQuery(principal: Principal? = null): QueryAlias { protected fun authorizedQuery(principal: Principal? = null): QueryAlias {
if (principal == null) { if (principal == null) {
return Posts return Posts
@ -58,10 +53,10 @@ class ExposedUserTimelineQueryService : UserTimelineQueryService, AbstractReposi
.select(Posts.columns) .select(Posts.columns)
.where { .where {
Posts.visibility eq Visibility.PUBLIC.name or Posts.visibility eq Visibility.PUBLIC.name or
(Posts.visibility eq Visibility.UNLISTED.name) or (Posts.visibility eq Visibility.UNLISTED.name) or
(Posts.visibility eq Visibility.DIRECT.name and (PostsVisibleActors.actorId eq principal.actorId.id)) or (Posts.visibility eq Visibility.DIRECT.name and (PostsVisibleActors.actorId eq principal.actorId.id)) or
(Posts.visibility eq Visibility.FOLLOWERS.name and (Relationships.blocking eq false and (relationshipsAlias[Relationships.following] eq true))) or (Posts.visibility eq Visibility.FOLLOWERS.name and (Relationships.blocking eq false and (relationshipsAlias[Relationships.following] eq true))) or
(Posts.actorId eq principal.actorId.id) (Posts.actorId eq principal.actorId.id)
} }
.alias("authorized_table") .alias("authorized_table")
} }

View File

@ -28,8 +28,7 @@ import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
import java.sql.SQLException import java.sql.SQLException
@Suppress("VarCouldBeVal") @Suppress("VarCouldBeVal")
abstract class AbstractRepository { abstract class AbstractRepository(protected val logger: Logger) {
protected abstract val logger: Logger
private val sqlErrorCodeSQLExceptionTranslator = SQLErrorCodeSQLExceptionTranslator() private val sqlErrorCodeSQLExceptionTranslator = SQLErrorCodeSQLExceptionTranslator()
private val springDataAccessExceptionSQLExceptionTranslator = SpringDataAccessExceptionSQLExceptionTranslator() private val springDataAccessExceptionSQLExceptionTranslator = SpringDataAccessExceptionSQLExceptionTranslator()

View File

@ -24,17 +24,14 @@ import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventPublisher
import dev.usbharu.hideout.core.domain.shared.repository.DomainEventPublishableRepository import dev.usbharu.hideout.core.domain.shared.repository.DomainEventPublishableRepository
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
@Repository @Repository
class ExposedActorInstanceRelationshipRepository(override val domainEventPublisher: DomainEventPublisher) : class ExposedActorInstanceRelationshipRepository(override val domainEventPublisher: DomainEventPublisher) :
ActorInstanceRelationshipRepository, ActorInstanceRelationshipRepository,
AbstractRepository(), AbstractRepository(logger),
DomainEventPublishableRepository<ActorInstanceRelationship> { DomainEventPublishableRepository<ActorInstanceRelationship> {
override val logger: Logger
get() = Companion.logger
override suspend fun save(actorInstanceRelationship: ActorInstanceRelationship): ActorInstanceRelationship { override suspend fun save(actorInstanceRelationship: ActorInstanceRelationship): ActorInstanceRelationship {
query { query {

View File

@ -25,7 +25,6 @@ import dev.usbharu.hideout.core.infrastructure.exposed.uri
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.javatime.timestamp import org.jetbrains.exposed.sql.javatime.timestamp
import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
@ -33,11 +32,9 @@ import org.springframework.stereotype.Repository
class ExposedActorRepository( class ExposedActorRepository(
private val actorQueryMapper: QueryMapper<Actor>, private val actorQueryMapper: QueryMapper<Actor>,
override val domainEventPublisher: DomainEventPublisher, override val domainEventPublisher: DomainEventPublisher,
) : AbstractRepository(), ) : AbstractRepository(logger),
DomainEventPublishableRepository<Actor>, DomainEventPublishableRepository<Actor>,
ActorRepository { ActorRepository {
override val logger: Logger
get() = Companion.logger
override suspend fun save(actor: Actor): Actor { override suspend fun save(actor: Actor): Actor {
query { query {

View File

@ -22,14 +22,11 @@ import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.Table import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.deleteWhere import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.upsert import org.jetbrains.exposed.sql.upsert
import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
@Repository @Repository
class ExposedApplicationRepository : ApplicationRepository, AbstractRepository() { class ExposedApplicationRepository : ApplicationRepository, AbstractRepository(logger) {
override val logger: Logger
get() = Companion.logger
override suspend fun save(application: Application) = query { override suspend fun save(application: Application) = query {
Applications.upsert { Applications.upsert {

View File

@ -25,15 +25,12 @@ import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.javatime.CurrentTimestamp import org.jetbrains.exposed.sql.javatime.CurrentTimestamp
import org.jetbrains.exposed.sql.javatime.timestamp import org.jetbrains.exposed.sql.javatime.timestamp
import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
import java.net.URI import java.net.URI
@Repository @Repository
class ExposedCustomEmojiRepository : CustomEmojiRepository, AbstractRepository() { class ExposedCustomEmojiRepository : CustomEmojiRepository, AbstractRepository(logger) {
override val logger: Logger
get() = Companion.logger
override suspend fun save(customEmoji: CustomEmoji): CustomEmoji = query { override suspend fun save(customEmoji: CustomEmoji): CustomEmoji = query {
CustomEmojis.upsert { CustomEmojis.upsert {

View File

@ -24,15 +24,12 @@ import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailId
import dev.usbharu.hideout.core.infrastructure.exposed.QueryMapper import dev.usbharu.hideout.core.infrastructure.exposed.QueryMapper
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
@Repository @Repository
class ExposedFilterRepository(private val filterQueryMapper: QueryMapper<Filter>) : FilterRepository, class ExposedFilterRepository(private val filterQueryMapper: QueryMapper<Filter>) : FilterRepository,
AbstractRepository() { AbstractRepository(logger) {
override val logger: Logger
get() = Companion.logger
override suspend fun save(filter: Filter): Filter = query { override suspend fun save(filter: Filter): Filter = query {
Filters.upsert { upsertStatement -> Filters.upsert { upsertStatement ->

View File

@ -21,7 +21,6 @@ import dev.usbharu.hideout.core.infrastructure.exposed.uri
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.javatime.timestamp import org.jetbrains.exposed.sql.javatime.timestamp
import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
import java.net.URI import java.net.URI
@ -29,9 +28,7 @@ import dev.usbharu.hideout.core.domain.model.instance.Instance as InstanceEntity
@Repository @Repository
class ExposedInstanceRepository : InstanceRepository, class ExposedInstanceRepository : InstanceRepository,
AbstractRepository() { AbstractRepository(logger) {
override val logger: Logger
get() = Companion.logger
override suspend fun save(instance: InstanceEntity): InstanceEntity = query { override suspend fun save(instance: InstanceEntity): InstanceEntity = query {
Instance.upsert { Instance.upsert {

View File

@ -21,15 +21,12 @@ import dev.usbharu.hideout.core.domain.model.media.*
import dev.usbharu.hideout.core.infrastructure.exposed.uri import dev.usbharu.hideout.core.infrastructure.exposed.uri
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
import dev.usbharu.hideout.core.domain.model.media.Media as EntityMedia import dev.usbharu.hideout.core.domain.model.media.Media as EntityMedia
@Repository @Repository
class ExposedMediaRepository : MediaRepository, AbstractRepository() { class ExposedMediaRepository : MediaRepository, AbstractRepository(logger) {
override val logger: Logger
get() = Companion.logger
override suspend fun save(media: EntityMedia): EntityMedia = query { override suspend fun save(media: EntityMedia): EntityMedia = query {
Media.upsert { Media.upsert {

View File

@ -43,7 +43,6 @@ import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList
import org.jetbrains.exposed.sql.javatime.timestamp import org.jetbrains.exposed.sql.javatime.timestamp
import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
@ -53,9 +52,8 @@ class ExposedPostRepository(
override val domainEventPublisher: DomainEventPublisher, override val domainEventPublisher: DomainEventPublisher,
) : ) :
PostRepository, PostRepository,
AbstractRepository(), AbstractRepository(logger),
DomainEventPublishableRepository<Post> { DomainEventPublishableRepository<Post> {
override val logger: Logger = Companion.logger
override suspend fun save(post: Post): Post { override suspend fun save(post: Post): Post {
query { query {

View File

@ -28,19 +28,15 @@ import dev.usbharu.hideout.core.domain.shared.repository.DomainEventPublishableR
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.javatime.timestamp import org.jetbrains.exposed.sql.javatime.timestamp
import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
@Repository @Repository
class ExposedReactionRepository(override val domainEventPublisher: DomainEventPublisher) : class ExposedReactionRepository(override val domainEventPublisher: DomainEventPublisher) :
ReactionRepository, ReactionRepository,
AbstractRepository(), AbstractRepository(logger),
DomainEventPublishableRepository<Reaction> { DomainEventPublishableRepository<Reaction> {
override val logger: Logger
get() = Companion.logger
override suspend fun save(reaction: Reaction): Reaction { override suspend fun save(reaction: Reaction): Reaction {
return query { return query {
Reactions.upsert { Reactions.upsert {

View File

@ -23,17 +23,14 @@ import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventPublisher
import dev.usbharu.hideout.core.domain.shared.repository.DomainEventPublishableRepository import dev.usbharu.hideout.core.domain.shared.repository.DomainEventPublishableRepository
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
@Repository @Repository
class ExposedRelationshipRepository(override val domainEventPublisher: DomainEventPublisher) : class ExposedRelationshipRepository(override val domainEventPublisher: DomainEventPublisher) :
RelationshipRepository, RelationshipRepository,
AbstractRepository(), AbstractRepository(logger),
DomainEventPublishableRepository<Relationship> { DomainEventPublishableRepository<Relationship> {
override val logger: Logger
get() = Companion.logger
override suspend fun save(relationship: Relationship): Relationship { override suspend fun save(relationship: Relationship): Relationship {
query { query {

View File

@ -24,14 +24,11 @@ import dev.usbharu.hideout.core.domain.model.timelinerelationship.TimelineRelati
import dev.usbharu.hideout.core.domain.model.timelinerelationship.Visible import dev.usbharu.hideout.core.domain.model.timelinerelationship.Visible
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
@Repository @Repository
class ExposedTimelineRelationshipRepository : AbstractRepository(), TimelineRelationshipRepository { class ExposedTimelineRelationshipRepository : AbstractRepository(logger), TimelineRelationshipRepository {
override val logger: Logger
get() = Companion.logger
override suspend fun save(timelineRelationship: TimelineRelationship): TimelineRelationship { override suspend fun save(timelineRelationship: TimelineRelationship): TimelineRelationship {
query { query {

View File

@ -22,17 +22,14 @@ import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventPublisher
import dev.usbharu.hideout.core.domain.shared.repository.DomainEventPublishableRepository import dev.usbharu.hideout.core.domain.shared.repository.DomainEventPublishableRepository
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
@Repository @Repository
class ExposedTimelineRepository(override val domainEventPublisher: DomainEventPublisher) : class ExposedTimelineRepository(override val domainEventPublisher: DomainEventPublisher) :
TimelineRepository, TimelineRepository,
AbstractRepository(), AbstractRepository(logger),
DomainEventPublishableRepository<Timeline> { DomainEventPublishableRepository<Timeline> {
override val logger: Logger
get() = Companion.logger
override suspend fun save(timeline: Timeline): Timeline { override suspend fun save(timeline: Timeline): Timeline {
query { query {

View File

@ -27,17 +27,14 @@ import dev.usbharu.hideout.core.domain.shared.repository.DomainEventPublishableR
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.javatime.timestamp import org.jetbrains.exposed.sql.javatime.timestamp
import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
@Repository @Repository
class ExposedUserDetailRepository(override val domainEventPublisher: DomainEventPublisher) : class ExposedUserDetailRepository(override val domainEventPublisher: DomainEventPublisher) :
UserDetailRepository, UserDetailRepository,
AbstractRepository(), AbstractRepository(logger),
DomainEventPublishableRepository<UserDetail> { DomainEventPublishableRepository<UserDetail> {
override val logger: Logger
get() = Companion.logger
override suspend fun save(userDetail: UserDetail): UserDetail = query { override suspend fun save(userDetail: UserDetail): UserDetail = query {
UserDetails.upsert { UserDetails.upsert {