From 982a29f1e0f9462d484b8d1c10944dd8f788b453 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Tue, 30 Jul 2024 01:01:26 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E3=83=9A=E3=83=BC=E3=82=B8=E3=83=B3?= =?UTF-8?q?=E3=82=B0=E3=81=AB=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MongoInternalTimelineObjectRepository.kt | 40 +++++++++++++++++-- .../InternalTimelineObjectRepository.kt | 4 +- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/mongorepository/MongoInternalTimelineObjectRepository.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/mongorepository/MongoInternalTimelineObjectRepository.kt index 71404c27..d396f5f6 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/mongorepository/MongoInternalTimelineObjectRepository.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/mongorepository/MongoInternalTimelineObjectRepository.kt @@ -6,24 +6,32 @@ import dev.usbharu.hideout.core.domain.model.filter.FilterId import dev.usbharu.hideout.core.domain.model.media.MediaId import dev.usbharu.hideout.core.domain.model.post.PostId import dev.usbharu.hideout.core.domain.model.post.Visibility +import dev.usbharu.hideout.core.domain.model.support.page.Page +import dev.usbharu.hideout.core.domain.model.support.page.PaginationList import dev.usbharu.hideout.core.domain.model.timeline.TimelineId import dev.usbharu.hideout.core.domain.model.timelineobject.TimelineObject import dev.usbharu.hideout.core.domain.model.timelineobject.TimelineObjectId import dev.usbharu.hideout.core.domain.model.timelineobject.TimelineObjectWarnFilter import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailId +import dev.usbharu.hideout.core.infrastructure.timeline.InternalTimelineObjectOption import dev.usbharu.hideout.core.infrastructure.timeline.InternalTimelineObjectRepository import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.toList +import org.springframework.data.domain.Sort +import org.springframework.data.mongodb.core.MongoTemplate import org.springframework.data.mongodb.core.mapping.Document +import org.springframework.data.mongodb.core.query.Criteria +import org.springframework.data.mongodb.core.query.Query import org.springframework.data.repository.kotlin.CoroutineCrudRepository import org.springframework.stereotype.Repository import java.time.Instant @Repository class MongoInternalTimelineObjectRepository( - private val springDataMongoTimelineObjectRepository: SpringDataMongoTimelineObjectRepository + private val springDataMongoTimelineObjectRepository: SpringDataMongoTimelineObjectRepository, + private val mongoTemplate: MongoTemplate ) : InternalTimelineObjectRepository { override suspend fun save(timelineObject: TimelineObject): TimelineObject { @@ -53,9 +61,33 @@ class MongoInternalTimelineObjectRepository( springDataMongoTimelineObjectRepository.deleteByTimelineId(timelineId.value) } - override suspend fun findByTimelineId(timelineId: TimelineId): List { - return springDataMongoTimelineObjectRepository.findByTimelineId(timelineId).map { it.toTimelineObject() } - .toList() + override suspend fun findByTimelineId( + timelineId: TimelineId, + internalTimelineObjectOption: InternalTimelineObjectOption?, + page: Page? + ): PaginationList { + val query = Query() + + if (page?.minId != null) { + query.with(Sort.by(Sort.Direction.ASC, "postCreatedAt")) + page.minId?.let { query.addCriteria(Criteria.where("id").gt(it)) } + page.maxId?.let { query.addCriteria(Criteria.where("id").lt(it)) } + } else { + query.with(Sort.by(Sort.Direction.DESC, "postCreatedAt")) + page?.sinceId?.let { query.addCriteria(Criteria.where("id").gt(it)) } + page?.maxId?.let { query.addCriteria(Criteria.where("id").lt(it)) } + } + + page?.limit?.let { query.limit(it) } + + val timelineObjects = + mongoTemplate.find(query, SpringDataMongoTimelineObject::class.java).map { it.toTimelineObject() } + + return PaginationList( + timelineObjects, + timelineObjects.lastOrNull()?.id, + timelineObjects.firstOrNull()?.id + ) } } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/timeline/InternalTimelineObjectRepository.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/timeline/InternalTimelineObjectRepository.kt index 23804c04..f13d10b9 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/timeline/InternalTimelineObjectRepository.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/timeline/InternalTimelineObjectRepository.kt @@ -3,8 +3,10 @@ package dev.usbharu.hideout.core.infrastructure.timeline import dev.usbharu.hideout.core.domain.model.actor.ActorId import dev.usbharu.hideout.core.domain.model.post.PostId import dev.usbharu.hideout.core.domain.model.support.page.Page +import dev.usbharu.hideout.core.domain.model.support.page.PaginationList import dev.usbharu.hideout.core.domain.model.timeline.TimelineId import dev.usbharu.hideout.core.domain.model.timelineobject.TimelineObject +import dev.usbharu.hideout.core.domain.model.timelineobject.TimelineObjectId interface InternalTimelineObjectRepository { suspend fun save(timelineObject: TimelineObject): TimelineObject @@ -22,7 +24,7 @@ interface InternalTimelineObjectRepository { timelineId: TimelineId, internalTimelineObjectOption: InternalTimelineObjectOption? = null, page: Page? = null - ): List + ): PaginationList } data class InternalTimelineObjectOption(