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)
|
|
||||||
|
@ -45,13 +45,13 @@ class InMemoryPostsAPI(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun deletePost(id: PostId): Boolean {
|
override suspend fun deletePost(id: PostId): Boolean {
|
||||||
return posts.remove(id) ?.also {
|
return posts.remove(id)?.also {
|
||||||
postDeletedBroadcastChannel.send(it)
|
postDeletedBroadcastChannel.send(it)
|
||||||
} != null
|
} != null
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun updatePostContent(postId: PostId, post: Post): Boolean {
|
override suspend fun updatePostContent(postId: PostId, post: Post): Boolean {
|
||||||
return getPostById(postId) ?.also { dbPost ->
|
return getPostById(postId)?.also { dbPost ->
|
||||||
val newPost = SimpleRegisteredPost(
|
val newPost = SimpleRegisteredPost(
|
||||||
dbPost.id,
|
dbPost.id,
|
||||||
post.content,
|
post.content,
|
||||||
@ -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> = posts.values.filter {
|
override suspend fun getPostsByDates(from: DateTime, to: DateTime): List<RegisteredPost> =
|
||||||
from ?.let { _ -> it.creationDate >= from } ?: true && to ?.let { _ -> it.creationDate <= to } ?: true
|
(from .. to).let { range ->
|
||||||
}
|
posts.values.filter {
|
||||||
|
it.creationDate in range
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun getPostsByPagination(
|
override suspend fun getPostsByPagination(
|
||||||
pagination: Pagination
|
pagination: Pagination
|
||||||
): PaginationResult<out RegisteredPost> = sortedByDatePosts.subList(
|
): PaginationResult<out RegisteredPost> = pagination.createResult(
|
||||||
pagination.firstIndex,
|
commonObjectsNumber = posts.size,
|
||||||
pagination.lastIndex
|
results = sortedByDatePosts.subList(
|
||||||
).let {
|
pagination.firstIndex,
|
||||||
PaginationResult(
|
pagination.lastIndex
|
||||||
pagination.page,
|
|
||||||
posts.size / pagination.size,
|
|
||||||
it
|
|
||||||
)
|
)
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user