This commit is contained in:
InsanusMokrassar 2020-04-10 12:54:06 +06:00
parent 42eda90a6b
commit 15157fa4c3
12 changed files with 56 additions and 27 deletions

View File

@ -7,3 +7,5 @@ klockVersion=1.10.3
uuidVersion=0.1.0 uuidVersion=0.1.0
gradle_bintray_plugin_version=1.8.4 gradle_bintray_plugin_version=1.8.4
core_version=0.3.0

View File

@ -17,7 +17,7 @@ plugins {
id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version" id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version"
} }
project.version = "0.2.0" project.version = "$core_version"
project.group = "com.insanusmokrassar" project.group = "com.insanusmokrassar"
apply from: "publish.gradle" apply from: "publish.gradle"

View File

@ -36,6 +36,12 @@ val Pagination.firstIndex: Int
val Pagination.lastIndex: Int val Pagination.lastIndex: Int
get() = firstIndex + size - 1 get() = firstIndex + size - 1
/**
* Calculates pages count for given [datasetSize]
*/
fun calculatePagesNumber(datasetSize: Long, pageSize: Int): Int {
return ceil(datasetSize.toDouble() / pageSize).toInt()
}
/** /**
* Calculates pages count for given [datasetSize] * Calculates pages count for given [datasetSize]
*/ */

View File

@ -10,12 +10,12 @@ data class PaginationResult<T>(
override val size: Int override val size: Int
) : Pagination ) : Pagination
fun <T> Pagination.createResult( fun <T> List<T>.createPaginationResult(
commonObjectsNumber: Int, pagination: Pagination,
results: List<T> commonObjectsNumber: Long
) = PaginationResult( ) = PaginationResult(
page, pagination.page,
calculatePagesNumber(commonObjectsNumber), calculatePagesNumber(commonObjectsNumber, size),
results, this,
size size
) )

View File

@ -7,11 +7,18 @@ const val defaultMediumPageSize = 5
const val defaultLargePageSize = 10 const val defaultLargePageSize = 10
const val defaultExtraLargePageSize = 15 const val defaultExtraLargePageSize = 15
@Suppress("NOTHING_TO_INLINE", "FunctionName")
inline fun FirstPagePagination(size: Int = defaultMediumPageSize) = SimplePagination( inline fun FirstPagePagination(size: Int = defaultMediumPageSize) = SimplePagination(
page = 0, page = 0,
size = defaultMediumPageSize size = defaultMediumPageSize
) )
@Suppress("NOTHING_TO_INLINE")
inline fun Pagination.nextPage() = SimplePagination(
page + 1,
size
)
@Serializable @Serializable
data class SimplePagination( data class SimplePagination(
override val page: Int, override val page: Int,

View File

@ -42,9 +42,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 pagination.createResult( return contents.values.asSequence().drop(pagination.firstIndex).take(pagination.size).toList().createPaginationResult(
commonObjectsNumber = contents.size, pagination,
results = contents.values.asSequence().drop(pagination.firstIndex).take(pagination.size).toList() commonObjectsNumber = contents.size.toLong()
) )
} }

View File

@ -76,11 +76,11 @@ class InMemoryPostsAPI(
override suspend fun getPostsByPagination( override suspend fun getPostsByPagination(
pagination: Pagination pagination: Pagination
): PaginationResult<RegisteredPost> = pagination.createResult( ): PaginationResult<RegisteredPost> = sortedByDatePosts.subList(
commonObjectsNumber = posts.size,
results = sortedByDatePosts.subList(
pagination.firstIndex, pagination.firstIndex,
pagination.lastIndex pagination.lastIndex
) ).createPaginationResult(
pagination,
commonObjectsNumber = posts.size.toLong()
) )
} }

View File

@ -1,4 +1,4 @@
project.version = "0.2.0" project.version = "$core_version"
project.group = "com.insanusmokrassar" project.group = "com.insanusmokrassar"
buildscript { buildscript {
@ -30,10 +30,16 @@ repositories {
dependencies { dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
api "com.insanusmokrassar:postssystem.core:$core_version"
api "org.jetbrains.exposed:exposed-core:$exposed_version" api "org.jetbrains.exposed:exposed-core:$exposed_version"
api "com.soywiz.korlibs.klock:klock:$klockVersion" api "com.soywiz.korlibs.klock:klock:$klockVersion"
if ((project.hasProperty('RELEASE_MODE') && project.property('RELEASE_MODE') == "true") || System.getenv('RELEASE_MODE') == "true") {
api "com.insanusmokrassar:postssystem.core:$core_version"
} else {
implementation project(":postssystem.core")
}
testImplementation "org.xerial:sqlite-jdbc:$test_sqlite_version" testImplementation "org.xerial:sqlite-jdbc:$test_sqlite_version"
testImplementation "org.junit.jupiter:junit-jupiter-api:$test_junit_version" testImplementation "org.junit.jupiter:junit-jupiter-api:$test_junit_version"
} }

View File

@ -1,5 +1,3 @@
exposed_version=0.20.3 exposed_version=0.23.1
test_sqlite_version=3.30.1 test_sqlite_version=3.30.1
test_junit_version=5.5.2 test_junit_version=5.5.2
core_version=0.2.0

View File

@ -32,7 +32,7 @@ private class ContentAPIDatabaseTable(
return transaction(database) { return transaction(database) {
insert { insert {
it[idColumn] = generateContentId() it[idColumn] = generateContentId()
it[dataColumn] = Json.plain.stringify(Content.serializer(), content) it[dataColumn] = Json.stringify(Content.serializer(), content)
}.getOrNull(idColumn) ?.let { id -> }.getOrNull(idColumn) ?.let { id ->
RegisteredContent( RegisteredContent(
id, id,
@ -58,7 +58,7 @@ private class ContentAPIDatabaseTable(
private fun ResultRow.asRegisteredContent(): RegisteredContent = RegisteredContent( private fun ResultRow.asRegisteredContent(): RegisteredContent = RegisteredContent(
get(idColumn), get(idColumn),
Json.plain.parse(Content.serializer(), get(dataColumn)) Json.parse(Content.serializer(), get(dataColumn))
) )
override suspend fun getContentsIds(): Set<ContentId> { override suspend fun getContentsIds(): Set<ContentId> {
@ -73,9 +73,12 @@ private class ContentAPIDatabaseTable(
} }
override suspend fun getContentByPagination(pagination: Pagination): PaginationResult<out RegisteredContent> { override suspend fun getContentByPagination(pagination: Pagination): PaginationResult<out RegisteredContent> {
return transaction(database) { return transaction(database) {
selectAll().count() to selectAll().limit(n = pagination.size, offset = pagination.firstIndex).map { it.asRegisteredContent() } selectAll().count() to selectAll().paginate(pagination).map { it.asRegisteredContent() }
}.let { (count, results) -> }.let { (count, results) ->
pagination.createResult(count, results) results.createPaginationResult(
pagination,
count
)
} }
} }
} }

View File

@ -160,11 +160,11 @@ private class PostsAPIDatabaseTable(
override suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<RegisteredPost> { override suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<RegisteredPost> {
return transaction(database) { return transaction(database) {
val posts = selectAll().limit(pagination.size, pagination.firstIndex).orderBy(creationDateColumn).map { val posts = selectAll().paginate(pagination).orderBy(creationDateColumn).map {
it.toRegisteredPost() it.toRegisteredPost()
} }
val postsNumber = selectAll().count() val postsNumber = selectAll().count()
pagination.createResult(postsNumber, posts) posts.createPaginationResult(pagination, postsNumber)
} }
} }

View File

@ -0,0 +1,7 @@
package com.insanusmokrassar.postssystem.core.exposed
import com.insanusmokrassar.postssystem.core.utils.pagination.Pagination
import com.insanusmokrassar.postssystem.core.utils.pagination.firstIndex
import org.jetbrains.exposed.sql.Query
fun Query.paginate(pagination: Pagination) = limit(pagination.size, pagination.firstIndex.toLong())