start fill server part
This commit is contained in:
parent
631cfe9c28
commit
0178229175
@ -1,10 +1,10 @@
|
|||||||
package com.insanusmokrassar.postssystem.core.client
|
package com.insanusmokrassar.postssystem.core.client
|
||||||
|
|
||||||
import com.insanusmokrassar.postssystem.core.api.ReadPostsAPI
|
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.clientserver.common.*
|
||||||
import com.insanusmokrassar.postssystem.core.content.ContentId
|
import com.insanusmokrassar.postssystem.core.content.ContentId
|
||||||
import com.insanusmokrassar.postssystem.core.post.Post
|
import com.insanusmokrassar.postssystem.core.post.*
|
||||||
import com.insanusmokrassar.postssystem.core.post.PostId
|
|
||||||
import com.insanusmokrassar.postssystem.core.utils.pagination.Pagination
|
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 io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
@ -13,7 +13,7 @@ import org.joda.time.DateTime
|
|||||||
|
|
||||||
class ReadableHttpPostsAPI(
|
class ReadableHttpPostsAPI(
|
||||||
private val client: HttpClient = HttpClient(),
|
private val client: HttpClient = HttpClient(),
|
||||||
private val baseAddress: String
|
baseAddress: String
|
||||||
) : ReadPostsAPI {
|
) : ReadPostsAPI {
|
||||||
private val postByIdAddress = "$baseAddress/$getPostByIdAddress"
|
private val postByIdAddress = "$baseAddress/$getPostByIdAddress"
|
||||||
private val postsByContentIdAddress = "$baseAddress/$getPostsByContentIdAddress"
|
private val postsByContentIdAddress = "$baseAddress/$getPostsByContentIdAddress"
|
||||||
@ -21,18 +21,29 @@ class ReadableHttpPostsAPI(
|
|||||||
private val postsByPaginationAddress = "$baseAddress/$getPostsByPaginationAddress"
|
private val postsByPaginationAddress = "$baseAddress/$getPostsByPaginationAddress"
|
||||||
|
|
||||||
override suspend fun getPostById(id: PostId): Post? {
|
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> {
|
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> {
|
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> {
|
override suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<out Post> {
|
||||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
return client.post<PaginationResult<SimplePost>>(postsByPaginationAddress) {
|
||||||
|
body = pagination
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,7 +4,9 @@ import com.insanusmokrassar.postssystem.core.api.WritePostsAPI
|
|||||||
import com.insanusmokrassar.postssystem.core.post.*
|
import com.insanusmokrassar.postssystem.core.post.*
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
class WritableHttpPostsAPI : WritePostsAPI {
|
class WritableHttpPostsAPI(
|
||||||
|
|
||||||
|
) : WritePostsAPI {
|
||||||
override val postCreatedFlow: Flow<Post>
|
override val postCreatedFlow: Flow<Post>
|
||||||
get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
|
get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
|
||||||
override val postDeletedFlow: Flow<Post>
|
override val postDeletedFlow: Flow<Post>
|
||||||
|
@ -4,3 +4,12 @@ const val getPostByIdAddress = "core/posts/get/id"
|
|||||||
const val getPostsByContentIdAddress = "core/posts/get/content_id"
|
const val getPostsByContentIdAddress = "core/posts/get/content_id"
|
||||||
const val getPostsByDatesAddress = "core/posts/get/dates"
|
const val getPostsByDatesAddress = "core/posts/get/dates"
|
||||||
const val getPostsByPaginationAddress = "core/posts/get/pagination"
|
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"
|
||||||
|
//}
|
||||||
|
@ -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?
|
||||||
|
)
|
@ -13,5 +13,5 @@ interface ReadPostsAPI {
|
|||||||
suspend fun getPostsByContent(id: ContentId): List<Post>
|
suspend fun getPostsByContent(id: ContentId): List<Post>
|
||||||
suspend fun getPostsByDates(from: DateTime? = null, to: DateTime? = null): 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>
|
||||||
}
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package com.insanusmokrassar.postssystem.core.post
|
package com.insanusmokrassar.postssystem.core.post
|
||||||
|
|
||||||
import com.insanusmokrassar.postssystem.core.content.Content
|
import com.insanusmokrassar.postssystem.core.content.Content
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
typealias PostId = String
|
typealias PostId = String
|
||||||
typealias PostContents = List<Content>
|
typealias PostContents = List<Content>
|
||||||
@ -10,4 +11,11 @@ interface Post {
|
|||||||
val content: PostContents
|
val content: PostContents
|
||||||
|
|
||||||
val meta: PostMetaInfo
|
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
42
ServerPart/build.gradle
Normal 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 ]
|
||||||
|
}
|
||||||
|
}
|
1
ServerPart/gradle.properties
Normal file
1
ServerPart/gradle.properties
Normal file
@ -0,0 +1 @@
|
|||||||
|
ktor_version=1.2.4
|
1
ServerPart/settings.gradle
Normal file
1
ServerPart/settings.gradle
Normal file
@ -0,0 +1 @@
|
|||||||
|
rootProject.name = 'postssystem.core.server'
|
@ -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)
|
||||||
|
}
|
@ -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")
|
||||||
|
}
|
||||||
|
}
|
@ -15,6 +15,7 @@ include 'api'
|
|||||||
include 'services:webservice'
|
include 'services:webservice'
|
||||||
*/
|
*/
|
||||||
|
|
||||||
include ':ClientPart'
|
|
||||||
include ':Core'
|
include ':Core'
|
||||||
include ':ClientServerCommon'
|
include ':ClientServerCommon'
|
||||||
|
include ':ClientPart'
|
||||||
|
include ':ServerPart'
|
||||||
|
Loading…
Reference in New Issue
Block a user