started to fill InMemoryPostsAPI

This commit is contained in:
InsanusMokrassar 2019-11-03 00:32:51 +06:00
parent 2c9e615ea6
commit 4760617808
3 changed files with 77 additions and 1 deletions

View File

@ -7,10 +7,27 @@ import com.insanusmokrassar.postssystem.core.utils.pagination.Pagination
import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationResult
import org.joda.time.DateTime
/**
* Simple read API by different properties
*/
interface ReadPostsAPI {
/**
* @return [RegisteredPost] if it is available by [id]
*/
suspend fun getPostById(id: PostId): RegisteredPost?
/**
* @return all [RegisteredPost]s which contains content with specified content [id]
*/
suspend fun getPostsByContent(id: ContentId): List<RegisteredPost>
/**
* @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>
/**
* @return all posts by pages basing on their creation date
*/
suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<out RegisteredPost>
}

View File

@ -18,3 +18,18 @@ interface Pagination {
*/
val size: Int
}
/**
* First number in index of objects. It can be used as offset for databases or other data sources
*/
val Pagination.firstIndex: Int
get() = page * size
/**
* Last number in index of objects. In fact, one [Pagination] object represent data in next range:
*
* [[firstIndex], [lastIndex]]; That means, that for [Pagination] with [Pagination.size] == 10 and [Pagination.page] == 1
* you will retrieve [Pagination.firstIndex] == 10 and [Pagination.lastIndex] == 19.
*/
val Pagination.lastIndex: Int
get() = firstIndex + size - 1

View File

@ -0,0 +1,44 @@
package com.insanusmokrassar.postssystem.core.api
import com.insanusmokrassar.postssystem.core.content.ContentId
import com.insanusmokrassar.postssystem.core.content.RegisteredContent
import com.insanusmokrassar.postssystem.core.post.PostId
import com.insanusmokrassar.postssystem.core.post.RegisteredPost
import com.insanusmokrassar.postssystem.core.utils.pagination.*
import org.joda.time.DateTime
class InMemoryPostsAPI(
initialPosts: List<RegisteredPost> = emptyList()
) : PostsAPI {
private val posts: MutableMap<PostId, RegisteredPost> = initialPosts.associateBy { it.id }.toMutableMap()
private val content: MutableMap<ContentId, RegisteredContent> = initialPosts.asSequence().flatMap {
it.content.asSequence()
}.associateBy {
it.id
}.toMutableMap()
private val sortedByDatePosts: List<RegisteredPost>
get() = posts.values.sortedBy { it.creationDate }
override suspend fun getPostById(id: PostId): RegisteredPost? = posts[id]
override suspend fun getPostsByContent(id: ContentId): List<RegisteredPost> = posts.values.filter { post ->
post.content.any { it.id == id }
}
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(
pagination: Pagination
): PaginationResult<out RegisteredPost> = sortedByDatePosts.subList(
pagination.firstIndex,
pagination.lastIndex
).let {
PaginationResult(
pagination.page,
posts.size / pagination.size,
it
)
}
}