mirror of https://github.com/usbharu/Hideout.git
feat: ページングに対応
This commit is contained in:
parent
175c0b38da
commit
982a29f1e0
|
@ -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.media.MediaId
|
||||||
import dev.usbharu.hideout.core.domain.model.post.PostId
|
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.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.timeline.TimelineId
|
||||||
import dev.usbharu.hideout.core.domain.model.timelineobject.TimelineObject
|
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.TimelineObjectId
|
||||||
import dev.usbharu.hideout.core.domain.model.timelineobject.TimelineObjectWarnFilter
|
import dev.usbharu.hideout.core.domain.model.timelineobject.TimelineObjectWarnFilter
|
||||||
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailId
|
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 dev.usbharu.hideout.core.infrastructure.timeline.InternalTimelineObjectRepository
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.collect
|
import kotlinx.coroutines.flow.collect
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.flow.toList
|
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.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.data.repository.kotlin.CoroutineCrudRepository
|
||||||
import org.springframework.stereotype.Repository
|
import org.springframework.stereotype.Repository
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
class MongoInternalTimelineObjectRepository(
|
class MongoInternalTimelineObjectRepository(
|
||||||
private val springDataMongoTimelineObjectRepository: SpringDataMongoTimelineObjectRepository
|
private val springDataMongoTimelineObjectRepository: SpringDataMongoTimelineObjectRepository,
|
||||||
|
private val mongoTemplate: MongoTemplate
|
||||||
) :
|
) :
|
||||||
InternalTimelineObjectRepository {
|
InternalTimelineObjectRepository {
|
||||||
override suspend fun save(timelineObject: TimelineObject): TimelineObject {
|
override suspend fun save(timelineObject: TimelineObject): TimelineObject {
|
||||||
|
@ -53,9 +61,33 @@ class MongoInternalTimelineObjectRepository(
|
||||||
springDataMongoTimelineObjectRepository.deleteByTimelineId(timelineId.value)
|
springDataMongoTimelineObjectRepository.deleteByTimelineId(timelineId.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun findByTimelineId(timelineId: TimelineId): List<TimelineObject> {
|
override suspend fun findByTimelineId(
|
||||||
return springDataMongoTimelineObjectRepository.findByTimelineId(timelineId).map { it.toTimelineObject() }
|
timelineId: TimelineId,
|
||||||
.toList()
|
internalTimelineObjectOption: InternalTimelineObjectOption?,
|
||||||
|
page: Page?
|
||||||
|
): PaginationList<TimelineObject, TimelineObjectId> {
|
||||||
|
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
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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.actor.ActorId
|
||||||
import dev.usbharu.hideout.core.domain.model.post.PostId
|
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.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.timeline.TimelineId
|
||||||
import dev.usbharu.hideout.core.domain.model.timelineobject.TimelineObject
|
import dev.usbharu.hideout.core.domain.model.timelineobject.TimelineObject
|
||||||
|
import dev.usbharu.hideout.core.domain.model.timelineobject.TimelineObjectId
|
||||||
|
|
||||||
interface InternalTimelineObjectRepository {
|
interface InternalTimelineObjectRepository {
|
||||||
suspend fun save(timelineObject: TimelineObject): TimelineObject
|
suspend fun save(timelineObject: TimelineObject): TimelineObject
|
||||||
|
@ -22,7 +24,7 @@ interface InternalTimelineObjectRepository {
|
||||||
timelineId: TimelineId,
|
timelineId: TimelineId,
|
||||||
internalTimelineObjectOption: InternalTimelineObjectOption? = null,
|
internalTimelineObjectOption: InternalTimelineObjectOption? = null,
|
||||||
page: Page? = null
|
page: Page? = null
|
||||||
): List<TimelineObject>
|
): PaginationList<TimelineObject, TimelineObjectId>
|
||||||
}
|
}
|
||||||
|
|
||||||
data class InternalTimelineObjectOption(
|
data class InternalTimelineObjectOption(
|
||||||
|
|
Loading…
Reference in New Issue