diff --git a/core/src/main/kotlin/com/insanusmokrassar/postssystem/core/post/api/ReadPostsAPI.kt b/core/src/main/kotlin/com/insanusmokrassar/postssystem/core/post/api/ReadPostsAPI.kt index b3bcbb9d..e3b8fb2f 100644 --- a/core/src/main/kotlin/com/insanusmokrassar/postssystem/core/post/api/ReadPostsAPI.kt +++ b/core/src/main/kotlin/com/insanusmokrassar/postssystem/core/post/api/ReadPostsAPI.kt @@ -3,6 +3,8 @@ package com.insanusmokrassar.postssystem.core.post.api import com.insanusmokrassar.postssystem.core.content.ContentId import com.insanusmokrassar.postssystem.core.post.PostId import com.insanusmokrassar.postssystem.core.post.RegisteredPost +import com.insanusmokrassar.postssystem.core.utils.MAX_DATE +import com.insanusmokrassar.postssystem.core.utils.MIN_DATE import com.insanusmokrassar.postssystem.core.utils.pagination.Pagination import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationResult import org.joda.time.DateTime @@ -24,10 +26,18 @@ interface ReadPostsAPI { /** * @return all [RegisteredPost]s between [from] and [to]. Range will be used INCLUSIVE, line \[[from], [to]\] */ - suspend fun getPostsByDates(from: DateTime? = null, to: DateTime? = null): List + suspend fun getPostsByDates(from: DateTime = MIN_DATE, to: DateTime = MAX_DATE): List /** * @return all posts by pages basing on their creation date */ suspend fun getPostsByPagination(pagination: Pagination): PaginationResult -} \ No newline at end of file +} + +suspend fun ReadPostsAPI.getPostsByDates( + from: DateTime? = null, + to: DateTime? = null +) = getPostsByDates( + from ?: MIN_DATE, + to ?: MAX_DATE +) diff --git a/core/src/main/kotlin/com/insanusmokrassar/postssystem/core/utils/DateTimeUtils.kt b/core/src/main/kotlin/com/insanusmokrassar/postssystem/core/utils/DateTimeUtils.kt index 3beef0cd..ef04ada1 100644 --- a/core/src/main/kotlin/com/insanusmokrassar/postssystem/core/utils/DateTimeUtils.kt +++ b/core/src/main/kotlin/com/insanusmokrassar/postssystem/core/utils/DateTimeUtils.kt @@ -2,8 +2,5 @@ package com.insanusmokrassar.postssystem.core.utils import org.joda.time.DateTime -fun DateTime?.orMin(): DateTime = this ?: MIN_DATE -fun DateTime?.orMax(): DateTime = this ?: MAX_DATE - -private val MIN_DATE = DateTime(Long.MIN_VALUE) -private val MAX_DATE = DateTime(Long.MAX_VALUE) +internal val MIN_DATE = DateTime(0) +internal val MAX_DATE = DateTime(Long.MAX_VALUE) diff --git a/core/src/test/kotlin/com/insanusmokrassar/postssystem/core/api/InMemoryPostsAPI.kt b/core/src/test/kotlin/com/insanusmokrassar/postssystem/core/api/InMemoryPostsAPI.kt index c9d5a317..16f74be8 100644 --- a/core/src/test/kotlin/com/insanusmokrassar/postssystem/core/api/InMemoryPostsAPI.kt +++ b/core/src/test/kotlin/com/insanusmokrassar/postssystem/core/api/InMemoryPostsAPI.kt @@ -45,13 +45,13 @@ class InMemoryPostsAPI( } override suspend fun deletePost(id: PostId): Boolean { - return posts.remove(id) ?.also { + return posts.remove(id)?.also { postDeletedBroadcastChannel.send(it) } != null } override suspend fun updatePostContent(postId: PostId, post: Post): Boolean { - return getPostById(postId) ?.also { dbPost -> + return getPostById(postId)?.also { dbPost -> val newPost = SimpleRegisteredPost( dbPost.id, post.content, @@ -65,22 +65,23 @@ class InMemoryPostsAPI( override suspend fun getPostById(id: PostId): RegisteredPost? = posts[id] - override suspend fun getPostsByContent(id: ContentId): List = posts.values.filter { post -> - id in post.content - } - override suspend fun getPostsByDates(from: DateTime?, to: DateTime?): List = posts.values.filter { - from ?.let { _ -> it.creationDate >= from } ?: true && to ?.let { _ -> it.creationDate <= to } ?: true - } + override suspend fun getPostsByContent(id: ContentId): List = + posts.values.filter { post -> id in post.content } + + override suspend fun getPostsByDates(from: DateTime, to: DateTime): List = + (from .. to).let { range -> + posts.values.filter { + it.creationDate in range + } + } + override suspend fun getPostsByPagination( pagination: Pagination - ): PaginationResult = sortedByDatePosts.subList( - pagination.firstIndex, - pagination.lastIndex - ).let { - PaginationResult( - pagination.page, - posts.size / pagination.size, - it + ): PaginationResult = pagination.createResult( + commonObjectsNumber = posts.size, + results = sortedByDatePosts.subList( + pagination.firstIndex, + pagination.lastIndex ) - } + ) }