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.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<RegisteredPost>
|
||||
suspend fun getPostsByDates(from: DateTime = MIN_DATE, to: DateTime = MAX_DATE): List<RegisteredPost>
|
||||
|
||||
/**
|
||||
* @return all posts by pages basing on their creation date
|
||||
*/
|
||||
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
|
||||
|
||||
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)
|
||||
|
@ -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<RegisteredPost> = posts.values.filter { post ->
|
||||
id in post.content
|
||||
}
|
||||
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 getPostsByContent(id: ContentId): List<RegisteredPost> =
|
||||
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 getPostsByPagination(
|
||||
pagination: Pagination
|
||||
): PaginationResult<out RegisteredPost> = sortedByDatePosts.subList(
|
||||
pagination.firstIndex,
|
||||
pagination.lastIndex
|
||||
).let {
|
||||
PaginationResult(
|
||||
pagination.page,
|
||||
posts.size / pagination.size,
|
||||
it
|
||||
): PaginationResult<out RegisteredPost> = pagination.createResult(
|
||||
commonObjectsNumber = posts.size,
|
||||
results = sortedByDatePosts.subList(
|
||||
pagination.firstIndex,
|
||||
pagination.lastIndex
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user