diff --git a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedFilterRepository.kt b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedFilterRepository.kt index 95444fb3..ce612540 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedFilterRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedFilterRepository.kt @@ -46,6 +46,11 @@ class ExposedFilterRepository(private val idGenerateService: IdGenerateService) return@query Filters.selectAll().where { Filters.id eq id }.singleOrNull()?.toFilter() } + override suspend fun findByUserIdAndId(userId: Long, id: Long): Filter? = query { + return@query Filters.selectAll().where { Filters.userId eq userId and (Filters.id eq id) }.singleOrNull() + ?.toFilter() + } + override suspend fun findByUserIdAndType(userId: Long, types: List): List = query { return@query Filters.selectAll().where { Filters.userId eq userId }.map { it.toFilter() } .filter { it.context.containsAll(types) } @@ -55,6 +60,10 @@ class ExposedFilterRepository(private val idGenerateService: IdGenerateService) Filters.deleteWhere { Filters.id eq id } } + override suspend fun deleteByUserIdAndId(userId: Long, id: Long) { + Filters.deleteWhere { Filters.userId eq userId and (Filters.id eq id) } + } + companion object { private val logger = LoggerFactory.getLogger(ExposedFilterRepository::class.java) } @@ -76,4 +85,4 @@ object Filters : Table() { val filterAction = varchar("action", 255) override val primaryKey: PrimaryKey = PrimaryKey(id) -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/usbharu/hideout/core/query/model/ExposedFilterQueryService.kt b/src/main/kotlin/dev/usbharu/hideout/core/query/model/ExposedFilterQueryService.kt index d7bda019..3be528f2 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/query/model/ExposedFilterQueryService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/query/model/ExposedFilterQueryService.kt @@ -6,6 +6,7 @@ import dev.usbharu.hideout.core.infrastructure.exposedrepository.Filters import dev.usbharu.hideout.core.infrastructure.exposedrepository.toFilter import dev.usbharu.hideout.core.infrastructure.exposedrepository.toFilterKeyword import org.jetbrains.exposed.sql.Query +import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.selectAll import org.springframework.stereotype.Repository @@ -19,6 +20,32 @@ class ExposedFilterQueryService : FilterQueryService { .toFilterQueryModel() } + override suspend fun findByUserId(userId: Long): List { + return Filters + .rightJoin(FilterKeywords) + .selectAll() + .where { Filters.userId eq userId } + .toFilterQueryModel() + } + + override suspend fun findByUserIdAndId(userId: Long, id: Long): FilterQueryModel? { + return Filters + .leftJoin(FilterKeywords) + .selectAll() + .where { Filters.userId eq userId and (Filters.id eq id) } + .toFilterQueryModel() + .firstOrNull() + } + + override suspend fun findByUserIdAndKeywordId(userId: Long, keywordId: Long): FilterQueryModel? { + return Filters + .leftJoin(FilterKeywords) + .selectAll() + .where { Filters.userId eq userId and (FilterKeywords.id eq keywordId) } + .toFilterQueryModel() + .firstOrNull() + } + private fun Query.toFilterQueryModel(): List { return this .groupBy { it[Filters.id] } @@ -30,4 +57,4 @@ class ExposedFilterQueryService : FilterQueryService { ) } } -} \ No newline at end of file +}