small refactoring

This commit is contained in:
InsanusMokrassar 2019-11-04 20:35:24 +06:00
parent 7a5fd177c8
commit f2d66b2fe9
3 changed files with 25 additions and 18 deletions

View File

@ -39,6 +39,11 @@ val Pagination.lastIndex: Int
/** /**
* Calculates pages count for given [datasetSize] * Calculates pages count for given [datasetSize]
*/ */
fun Pagination.pagesCount(datasetSize: Int): Int { fun calculatePagesNumber(datasetSize: Int, pageSize: Int): Int {
return ceil(datasetSize.toDouble() / size).toInt() return ceil(datasetSize.toDouble() / pageSize).toInt()
} }
/**
* Calculates pages count for given [datasetSize]
*/
inline fun Pagination.calculatePagesNumber(datasetSize: Int): Int = calculatePagesNumber(datasetSize, size)

View File

@ -1,14 +1,21 @@
package com.insanusmokrassar.postssystem.core.utils.pagination package com.insanusmokrassar.postssystem.core.utils.pagination
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
@Serializable @Serializable
data class PaginationResult<T>( data class PaginationResult<T>(
override val page: Int, override val page: Int,
val pagesNumber: Int, val pagesNumber: Int,
val results: List<T> val results: List<T>,
) : Pagination {
@Transient
override val size: Int = results.size override val size: Int = results.size
} ) : Pagination
fun <T> Pagination.createResult(
commonObjectsNumber: Int,
results: List<T>
) = PaginationResult(
page,
calculatePagesNumber(commonObjectsNumber),
results,
size
)

View File

@ -2,10 +2,7 @@ package com.insanusmokrassar.postssystem.core.api
import com.insanusmokrassar.postssystem.core.content.* import com.insanusmokrassar.postssystem.core.content.*
import com.insanusmokrassar.postssystem.core.content.api.ContentAPI import com.insanusmokrassar.postssystem.core.content.api.ContentAPI
import com.insanusmokrassar.postssystem.core.utils.pagination.Pagination import com.insanusmokrassar.postssystem.core.utils.pagination.*
import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationResult
import com.insanusmokrassar.postssystem.core.utils.pagination.firstIndex
import com.insanusmokrassar.postssystem.core.utils.pagination.pagesCount
import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
@ -20,6 +17,8 @@ private fun generateId(): ContentId = UUID.randomUUID().toString()
class InMemoryContentAPI( class InMemoryContentAPI(
initialContent: List<RegisteredContent> = emptyList() initialContent: List<RegisteredContent> = emptyList()
): ContentAPI { ): ContentAPI {
private val contentCreatedBroadcastChannel = BroadcastChannel<RegisteredContent>(Channel.BUFFERED)
private val contentDeletedBroadcastChannel = BroadcastChannel<RegisteredContent>(Channel.BUFFERED)
override val contentCreatedFlow: Flow<RegisteredContent> override val contentCreatedFlow: Flow<RegisteredContent>
get() = contentCreatedBroadcastChannel.asFlow() get() = contentCreatedBroadcastChannel.asFlow()
@ -31,9 +30,6 @@ class InMemoryContentAPI(
.associateBy(RegisteredContent::id) .associateBy(RegisteredContent::id)
.toMutableMap() .toMutableMap()
private val contentCreatedBroadcastChannel = BroadcastChannel<RegisteredContent>(Channel.BUFFERED)
private val contentDeletedBroadcastChannel = BroadcastChannel<RegisteredContent>(Channel.BUFFERED)
override suspend fun createContent(content: Content): RegisteredContent? { override suspend fun createContent(content: Content): RegisteredContent? {
return content.createRegisteredEntity(generateId())?.also { registeredContent -> return content.createRegisteredEntity(generateId())?.also { registeredContent ->
contents[registeredContent.id] = registeredContent contents[registeredContent.id] = registeredContent
@ -44,10 +40,9 @@ class InMemoryContentAPI(
override suspend fun getContentById(id: ContentId): RegisteredContent? = contents[id] override suspend fun getContentById(id: ContentId): RegisteredContent? = contents[id]
override suspend fun getContentByPagination(pagination: Pagination): PaginationResult<out RegisteredContent> { override suspend fun getContentByPagination(pagination: Pagination): PaginationResult<out RegisteredContent> {
return PaginationResult( return pagination.createResult(
page = pagination.page, commonObjectsNumber = contents.size,
pagesNumber = pagination.pagesCount(contents.size), results = contents.values.asSequence().drop(pagination.firstIndex).take(pagination.size).toList()
results = contents.values.drop(pagination.firstIndex).take(pagination.size)
) )
} }