start fill server part

This commit is contained in:
InsanusMokrassar 2019-10-17 17:35:48 +06:00
parent 631cfe9c28
commit 0178229175
12 changed files with 183 additions and 12 deletions

View File

@ -1,10 +1,10 @@
package com.insanusmokrassar.postssystem.core.client
import com.insanusmokrassar.postssystem.core.api.ReadPostsAPI
import com.insanusmokrassar.postssystem.core.clientserver.common.models.DateTimeRequest
import com.insanusmokrassar.postssystem.core.clientserver.common.*
import com.insanusmokrassar.postssystem.core.content.ContentId
import com.insanusmokrassar.postssystem.core.post.Post
import com.insanusmokrassar.postssystem.core.post.PostId
import com.insanusmokrassar.postssystem.core.post.*
import com.insanusmokrassar.postssystem.core.utils.pagination.Pagination
import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationResult
import io.ktor.client.HttpClient
@ -13,7 +13,7 @@ import org.joda.time.DateTime
class ReadableHttpPostsAPI(
private val client: HttpClient = HttpClient(),
private val baseAddress: String
baseAddress: String
) : ReadPostsAPI {
private val postByIdAddress = "$baseAddress/$getPostByIdAddress"
private val postsByContentIdAddress = "$baseAddress/$getPostsByContentIdAddress"
@ -21,18 +21,29 @@ class ReadableHttpPostsAPI(
private val postsByPaginationAddress = "$baseAddress/$getPostsByPaginationAddress"
override suspend fun getPostById(id: PostId): Post? {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
return client.post<SimplePost>(postByIdAddress) {
body = id
}
}
override suspend fun getPostsByContent(id: ContentId): List<Post> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
return client.post<List<SimplePost>>(postsByContentIdAddress) {
body = id
}
}
override suspend fun getPostsByDates(from: DateTime?, to: DateTime?): List<Post> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
return client.post<List<SimplePost>>(postsByDatesAddress) {
body = DateTimeRequest(
from ?.millis,
to ?.millis
)
}
}
override suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<Post> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
override suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<out Post> {
return client.post<PaginationResult<SimplePost>>(postsByPaginationAddress) {
body = pagination
}
}
}

View File

@ -4,7 +4,9 @@ import com.insanusmokrassar.postssystem.core.api.WritePostsAPI
import com.insanusmokrassar.postssystem.core.post.*
import kotlinx.coroutines.flow.Flow
class WritableHttpPostsAPI : WritePostsAPI {
class WritableHttpPostsAPI(
) : WritePostsAPI {
override val postCreatedFlow: Flow<Post>
get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
override val postDeletedFlow: Flow<Post>

View File

@ -4,3 +4,12 @@ const val getPostByIdAddress = "core/posts/get/id"
const val getPostsByContentIdAddress = "core/posts/get/content_id"
const val getPostsByDatesAddress = "core/posts/get/dates"
const val getPostsByPaginationAddress = "core/posts/get/pagination"
//class ReadPaths(
// val baseAddress: String
//) {
// val postByIdAddress = "$baseAddress/$getPostByIdAddress"
// val postsByContentIdAddress = "$baseAddress/$getPostsByContentIdAddress"
// val postsByDatesAddress = "$baseAddress/$getPostsByDatesAddress"
// val postsByPaginationAddress = "$baseAddress/$getPostsByPaginationAddress"
//}

View File

@ -0,0 +1,9 @@
package com.insanusmokrassar.postssystem.core.clientserver.common.models
import kotlinx.serialization.Serializable
@Serializable
data class DateTimeRequest (
val from: Long?,
val to: Long?
)

View File

@ -13,5 +13,5 @@ interface ReadPostsAPI {
suspend fun getPostsByContent(id: ContentId): List<Post>
suspend fun getPostsByDates(from: DateTime? = null, to: DateTime? = null): List<Post>
suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<Post>
suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<out Post>
}

View File

@ -1,6 +1,7 @@
package com.insanusmokrassar.postssystem.core.post
import com.insanusmokrassar.postssystem.core.content.Content
import kotlinx.serialization.Serializable
typealias PostId = String
typealias PostContents = List<Content>
@ -10,4 +11,11 @@ interface Post {
val content: PostContents
val meta: PostMetaInfo
}
}
@Serializable
data class SimplePost(
override val id: PostId,
override val content: PostContents,
override val meta: PostMetaInfo
) : Post

42
ServerPart/build.gradle Normal file
View File

@ -0,0 +1,42 @@
project.version = "0.1.0"
project.group = "com.insanusmokrassar"
buildscript {
repositories {
mavenLocal()
jcenter()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:$gradle_bintray_plugin_version"
}
}
apply plugin: 'java-library'
apply plugin: 'kotlin'
apply plugin: 'kotlinx-serialization'
repositories {
mavenLocal()
jcenter()
mavenCentral()
maven { url "https://kotlin.bintray.com/kotlinx" }
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
api project(":Core")
api project(":ClientServerCommon")
api "io.ktor:ktor-server:$ktor_version"
api "io.ktor:ktor-server-core:$ktor_version"
}
compileKotlin {
kotlinOptions {
freeCompilerArgs = [ disableImplicitReflectionSerializerAnnotation ]
}
}

View File

@ -0,0 +1 @@
ktor_version=1.2.4

View File

@ -0,0 +1 @@
rootProject.name = 'postssystem.core.server'

View File

@ -0,0 +1,21 @@
package com.insanusmokrassar.postssystem.core.server
import com.insanusmokrassar.postssystem.core.post.Post
import com.insanusmokrassar.postssystem.core.post.SimplePost
import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationResult
import kotlinx.serialization.internal.ArrayListSerializer
internal val postsSerializer = ArrayListSerializer(SimplePost.serializer())
internal val paginationResultSerializer = PaginationResult.serializer(SimplePost.serializer())
internal val List<Post>.asSimplePostList
get() = map { post ->
post.asSimplePost
}
internal val Post.asSimplePost
get() = when (this) {
is SimplePost -> this
else -> SimplePost(id, content, meta)
}

View File

@ -0,0 +1,66 @@
package com.insanusmokrassar.postssystem.core.server
import com.insanusmokrassar.postssystem.core.api.ReadPostsAPI
import com.insanusmokrassar.postssystem.core.clientserver.common.*
import com.insanusmokrassar.postssystem.core.clientserver.common.models.DateTimeRequest
import com.insanusmokrassar.postssystem.core.content.ContentId
import com.insanusmokrassar.postssystem.core.post.*
import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationRequest
import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationResult
import io.ktor.application.call
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.request.receiveOrNull
import io.ktor.response.respond
import io.ktor.response.respondText
import io.ktor.routing.Route
import io.ktor.routing.post
import kotlinx.serialization.json.Json
import org.joda.time.DateTime
fun Route.includePostsCoreReadModules(readPostsAPI: ReadPostsAPI) {
post(getPostByIdAddress) {
call.receiveOrNull(PostId::class) ?.also { id ->
val post = readPostsAPI.getPostById(id)
post ?.let {
val simplePost = post.asSimplePost
call.respondText(ContentType.Application.Json) {
Json.plain.stringify(SimplePost.serializer(), simplePost)
}
} ?: call.respond(HttpStatusCode.NotFound, "Not found")
} ?: call.respond(HttpStatusCode.BadRequest, "Id of post must be provided as body of request")
}
post(getPostsByContentIdAddress) {
call.receiveOrNull(ContentId::class) ?.also { id ->
val posts = readPostsAPI.getPostsByContent(id)
val simplePosts = posts.asSimplePostList
call.respondText(ContentType.Application.Json) {
Json.plain.stringify(postsSerializer, simplePosts)
}
} ?: call.respond(HttpStatusCode.BadRequest, "Id of content must be provided as body of request")
}
post(getPostsByDatesAddress) {
call.receiveOrNull<DateTimeRequest>() ?.also { (from, to) ->
val posts = readPostsAPI.getPostsByDates(from ?.let { DateTime(it) }, to ?.let { DateTime(it) })
val simplePosts = posts.asSimplePostList
call.respondText(ContentType.Application.Json) {
Json.plain.stringify(postsSerializer, simplePosts)
}
} ?: call.respond(HttpStatusCode.BadRequest, "Object \"DateTimeRequest\" must be provided as body of request")
}
post(getPostsByPaginationAddress) {
call.receiveOrNull<PaginationRequest>() ?.also { pagination ->
val paginationResult = readPostsAPI.getPostsByPagination(pagination).let {
val simplePosts = it.results.asSimplePostList
PaginationResult(
it.page,
it.pagesNumber,
simplePosts
)
}
call.respondText(ContentType.Application.Json) {
Json.plain.stringify(paginationResultSerializer, paginationResult)
}
} ?: call.respond(HttpStatusCode.BadRequest, "Object \"PaginationRequest\" must be provided as body of request")
}
}

View File

@ -15,6 +15,7 @@ include 'api'
include 'services:webservice'
*/
include ':ClientPart'
include ':Core'
include ':ClientServerCommon'
include ':ClientPart'
include ':ServerPart'