started to fill InMemoryPostsAPI
This commit is contained in:
parent
2c9e615ea6
commit
4760617808
@ -7,10 +7,27 @@ 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
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple read API by different properties
|
||||||
|
*/
|
||||||
interface ReadPostsAPI {
|
interface ReadPostsAPI {
|
||||||
|
/**
|
||||||
|
* @return [RegisteredPost] if it is available by [id]
|
||||||
|
*/
|
||||||
suspend fun getPostById(id: PostId): RegisteredPost?
|
suspend fun getPostById(id: PostId): RegisteredPost?
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return all [RegisteredPost]s which contains content with specified content [id]
|
||||||
|
*/
|
||||||
suspend fun getPostsByContent(id: ContentId): List<RegisteredPost>
|
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>
|
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>
|
suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<out RegisteredPost>
|
||||||
}
|
}
|
@ -17,4 +17,19 @@ interface Pagination {
|
|||||||
* Size of current page. Offset can be calculated as [page] * [size]
|
* Size of current page. Offset can be calculated as [page] * [size]
|
||||||
*/
|
*/
|
||||||
val size: Int
|
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
|
||||||
|
@ -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
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user