refactor
This commit is contained in:
parent
93c3ebd870
commit
44d89b3a47
@ -3,6 +3,8 @@ package com.insanusmokrassar.postssystem.core.post.api
|
|||||||
import com.insanusmokrassar.postssystem.core.content.ContentId
|
import com.insanusmokrassar.postssystem.core.content.ContentId
|
||||||
import com.insanusmokrassar.postssystem.core.post.PostId
|
import com.insanusmokrassar.postssystem.core.post.PostId
|
||||||
import com.insanusmokrassar.postssystem.core.post.RegisteredPost
|
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.Pagination
|
||||||
import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationResult
|
import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationResult
|
||||||
import org.joda.time.DateTime
|
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]\]
|
* @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<RegisteredPost>
|
suspend fun getPostsByDates(from: DateTime = MIN_DATE, to: DateTime = MAX_DATE): List<RegisteredPost>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return all posts by pages basing on their creation date
|
* @return all posts by pages basing on their creation date
|
||||||
*/
|
*/
|
||||||
suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<out RegisteredPost>
|
suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<out RegisteredPost>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
suspend fun ReadPostsAPI.getPostsByDates(
|
||||||
|
from: DateTime? = null,
|
||||||
|
to: DateTime? = null
|
||||||
|
) = getPostsByDates(
|
||||||
|
from ?: MIN_DATE,
|
||||||
|
to ?: MAX_DATE
|
||||||
|
)
|
||||||
|
@ -2,8 +2,5 @@ package com.insanusmokrassar.postssystem.core.utils
|
|||||||
|
|
||||||
import org.joda.time.DateTime
|
import org.joda.time.DateTime
|
||||||
|
|
||||||
fun DateTime?.orMin(): DateTime = this ?: MIN_DATE
|
internal val MIN_DATE = DateTime(0)
|
||||||
fun DateTime?.orMax(): DateTime = this ?: MAX_DATE
|
internal val MAX_DATE = DateTime(Long.MAX_VALUE)
|
||||||
|
|
||||||
private val MIN_DATE = DateTime(Long.MIN_VALUE)
|
|
||||||
private val MAX_DATE = DateTime(Long.MAX_VALUE)
|
|
||||||
|
@ -65,22 +65,23 @@ class InMemoryPostsAPI(
|
|||||||
|
|
||||||
|
|
||||||
override suspend fun getPostById(id: PostId): RegisteredPost? = posts[id]
|
override suspend fun getPostById(id: PostId): RegisteredPost? = posts[id]
|
||||||
override suspend fun getPostsByContent(id: ContentId): List<RegisteredPost> = posts.values.filter { post ->
|
override suspend fun getPostsByContent(id: ContentId): List<RegisteredPost> =
|
||||||
id in post.content
|
posts.values.filter { post -> id in post.content }
|
||||||
|
|
||||||
|
override suspend fun getPostsByDates(from: DateTime, to: DateTime): List<RegisteredPost> =
|
||||||
|
(from .. to).let { range ->
|
||||||
|
posts.values.filter {
|
||||||
|
it.creationDate in range
|
||||||
}
|
}
|
||||||
override suspend fun getPostsByDates(from: DateTime?, to: DateTime?): List<RegisteredPost> = posts.values.filter {
|
|
||||||
from ?.let { _ -> it.creationDate >= from } ?: true && to ?.let { _ -> it.creationDate <= to } ?: true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getPostsByPagination(
|
override suspend fun getPostsByPagination(
|
||||||
pagination: Pagination
|
pagination: Pagination
|
||||||
): PaginationResult<out RegisteredPost> = sortedByDatePosts.subList(
|
): PaginationResult<out RegisteredPost> = pagination.createResult(
|
||||||
|
commonObjectsNumber = posts.size,
|
||||||
|
results = sortedByDatePosts.subList(
|
||||||
pagination.firstIndex,
|
pagination.firstIndex,
|
||||||
pagination.lastIndex
|
pagination.lastIndex
|
||||||
).let {
|
)
|
||||||
PaginationResult(
|
|
||||||
pagination.page,
|
|
||||||
posts.size / pagination.size,
|
|
||||||
it
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user