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
@Repository
class ExposedPrincipalQueryService : PrincipalQueryService, AbstractRepository() {
override val logger: Logger
get() = Companion.logger
class ExposedPrincipalQueryService : PrincipalQueryService, AbstractRepository(logger) {
override suspend fun findByUserDetailId(userDetailId: UserDetailId): PrincipalDTO {
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.query.reactions.ReactionsQueryService
import org.jetbrains.exposed.sql.*
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository
import java.net.URI
import dev.usbharu.hideout.core.infrastructure.exposedrepository.Reactions as ExposedrepositoryReactions
@Repository
class ExposedReactionsQueryService : ReactionsQueryService, AbstractRepository() {
override val logger: Logger
get() = Companion.logger
class ExposedReactionsQueryService : ReactionsQueryService, AbstractRepository(logger) {
override suspend fun findAllByPostId(postId: PostId): List<Reactions> {
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.query.usertimeline.UserTimelineQueryService
import org.jetbrains.exposed.sql.*
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository
import java.net.URI
@Repository
class ExposedUserTimelineQueryService : UserTimelineQueryService, AbstractRepository() {
override val logger: Logger
get() = Companion.logger
class ExposedUserTimelineQueryService : UserTimelineQueryService, AbstractRepository(logger) {
protected fun authorizedQuery(principal: Principal? = null): QueryAlias {
if (principal == null) {
return Posts
@ -58,10 +53,10 @@ class ExposedUserTimelineQueryService : UserTimelineQueryService, AbstractReposi
.select(Posts.columns)
.where {
Posts.visibility eq Visibility.PUBLIC.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.FOLLOWERS.name and (Relationships.blocking eq false and (relationshipsAlias[Relationships.following] eq true))) or
(Posts.actorId eq principal.actorId.id)
(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.FOLLOWERS.name and (Relationships.blocking eq false and (relationshipsAlias[Relationships.following] eq true))) or
(Posts.actorId eq principal.actorId.id)
}
.alias("authorized_table")
}

View File

@ -28,8 +28,7 @@ import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
import java.sql.SQLException
@Suppress("VarCouldBeVal")
abstract class AbstractRepository {
protected abstract val logger: Logger
abstract class AbstractRepository(protected val logger: Logger) {
private val sqlErrorCodeSQLExceptionTranslator = SQLErrorCodeSQLExceptionTranslator()
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 org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository
@Repository
class ExposedActorInstanceRelationshipRepository(override val domainEventPublisher: DomainEventPublisher) :
ActorInstanceRelationshipRepository,
AbstractRepository(),
AbstractRepository(logger),
DomainEventPublishableRepository<ActorInstanceRelationship> {
override val logger: Logger
get() = Companion.logger
override suspend fun save(actorInstanceRelationship: ActorInstanceRelationship): ActorInstanceRelationship {
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.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.javatime.timestamp
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository
@ -33,11 +32,9 @@ import org.springframework.stereotype.Repository
class ExposedActorRepository(
private val actorQueryMapper: QueryMapper<Actor>,
override val domainEventPublisher: DomainEventPublisher,
) : AbstractRepository(),
) : AbstractRepository(logger),
DomainEventPublishableRepository<Actor>,
ActorRepository {
override val logger: Logger
get() = Companion.logger
override suspend fun save(actor: Actor): Actor {
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.deleteWhere
import org.jetbrains.exposed.sql.upsert
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository
@Repository
class ExposedApplicationRepository : ApplicationRepository, AbstractRepository() {
override val logger: Logger
get() = Companion.logger
class ExposedApplicationRepository : ApplicationRepository, AbstractRepository(logger) {
override suspend fun save(application: Application) = query {
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.javatime.CurrentTimestamp
import org.jetbrains.exposed.sql.javatime.timestamp
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository
import java.net.URI
@Repository
class ExposedCustomEmojiRepository : CustomEmojiRepository, AbstractRepository() {
override val logger: Logger
get() = Companion.logger
class ExposedCustomEmojiRepository : CustomEmojiRepository, AbstractRepository(logger) {
override suspend fun save(customEmoji: CustomEmoji): CustomEmoji = query {
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 org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository
@Repository
class ExposedFilterRepository(private val filterQueryMapper: QueryMapper<Filter>) : FilterRepository,
AbstractRepository() {
override val logger: Logger
get() = Companion.logger
AbstractRepository(logger) {
override suspend fun save(filter: Filter): Filter = query {
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.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.javatime.timestamp
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository
import java.net.URI
@ -29,9 +28,7 @@ import dev.usbharu.hideout.core.domain.model.instance.Instance as InstanceEntity
@Repository
class ExposedInstanceRepository : InstanceRepository,
AbstractRepository() {
override val logger: Logger
get() = Companion.logger
AbstractRepository(logger) {
override suspend fun save(instance: InstanceEntity): InstanceEntity = query {
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 org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository
import dev.usbharu.hideout.core.domain.model.media.Media as EntityMedia
@Repository
class ExposedMediaRepository : MediaRepository, AbstractRepository() {
override val logger: Logger
get() = Companion.logger
class ExposedMediaRepository : MediaRepository, AbstractRepository(logger) {
override suspend fun save(media: EntityMedia): EntityMedia = query {
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.inList
import org.jetbrains.exposed.sql.javatime.timestamp
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository
@ -53,9 +52,8 @@ class ExposedPostRepository(
override val domainEventPublisher: DomainEventPublisher,
) :
PostRepository,
AbstractRepository(),
AbstractRepository(logger),
DomainEventPublishableRepository<Post> {
override val logger: Logger = Companion.logger
override suspend fun save(post: Post): Post {
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.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.javatime.timestamp
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository
@Repository
class ExposedReactionRepository(override val domainEventPublisher: DomainEventPublisher) :
ReactionRepository,
AbstractRepository(),
AbstractRepository(logger),
DomainEventPublishableRepository<Reaction> {
override val logger: Logger
get() = Companion.logger
override suspend fun save(reaction: Reaction): Reaction {
return query {
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 org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository
@Repository
class ExposedRelationshipRepository(override val domainEventPublisher: DomainEventPublisher) :
RelationshipRepository,
AbstractRepository(),
AbstractRepository(logger),
DomainEventPublishableRepository<Relationship> {
override val logger: Logger
get() = Companion.logger
override suspend fun save(relationship: Relationship): Relationship {
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 org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository
@Repository
class ExposedTimelineRelationshipRepository : AbstractRepository(), TimelineRelationshipRepository {
override val logger: Logger
get() = Companion.logger
class ExposedTimelineRelationshipRepository : AbstractRepository(logger), TimelineRelationshipRepository {
override suspend fun save(timelineRelationship: TimelineRelationship): TimelineRelationship {
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 org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository
@Repository
class ExposedTimelineRepository(override val domainEventPublisher: DomainEventPublisher) :
TimelineRepository,
AbstractRepository(),
AbstractRepository(logger),
DomainEventPublishableRepository<Timeline> {
override val logger: Logger
get() = Companion.logger
override suspend fun save(timeline: Timeline): Timeline {
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.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.javatime.timestamp
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository
@Repository
class ExposedUserDetailRepository(override val domainEventPublisher: DomainEventPublisher) :
UserDetailRepository,
AbstractRepository(),
AbstractRepository(logger),
DomainEventPublishableRepository<UserDetail> {
override val logger: Logger
get() = Companion.logger
override suspend fun save(userDetail: UserDetail): UserDetail = query {
UserDetails.upsert {