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
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"
}
project.version = "0.2.0"
project.version = "$core_version"
project.group = "com.insanusmokrassar"
apply from: "publish.gradle"

View File

@ -36,6 +36,12 @@ val Pagination.firstIndex: Int
val Pagination.lastIndex: Int
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]
*/

View File

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

View File

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

View File

@ -42,9 +42,9 @@ class InMemoryContentAPI(
override suspend fun getContentById(id: ContentId): RegisteredContent? = contents[id]
override suspend fun getContentByPagination(pagination: Pagination): PaginationResult<out RegisteredContent> {
return pagination.createResult(
commonObjectsNumber = contents.size,
results = contents.values.asSequence().drop(pagination.firstIndex).take(pagination.size).toList()
return contents.values.asSequence().drop(pagination.firstIndex).take(pagination.size).toList().createPaginationResult(
pagination,
commonObjectsNumber = contents.size.toLong()
)
}

View File

@ -76,11 +76,11 @@ class InMemoryPostsAPI(
override suspend fun getPostsByPagination(
pagination: Pagination
): PaginationResult<RegisteredPost> = pagination.createResult(
commonObjectsNumber = posts.size,
results = sortedByDatePosts.subList(
pagination.firstIndex,
pagination.lastIndex
)
): PaginationResult<RegisteredPost> = sortedByDatePosts.subList(
pagination.firstIndex,
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"
buildscript {
@ -30,10 +30,16 @@ repositories {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
api "com.insanusmokrassar:postssystem.core:$core_version"
api "org.jetbrains.exposed:exposed-core:$exposed_version"
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.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_junit_version=5.5.2
core_version=0.2.0

View File

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