exclude common parts from read modules

This commit is contained in:
InsanusMokrassar 2019-10-17 17:43:51 +06:00
parent 0178229175
commit 2fb346f6c7
2 changed files with 26 additions and 16 deletions

View File

@ -3,7 +3,14 @@ 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 io.ktor.application.ApplicationCall
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.response.respond
import io.ktor.response.respondText
import kotlinx.serialization.KSerializer
import kotlinx.serialization.internal.ArrayListSerializer
import kotlinx.serialization.json.Json
internal val postsSerializer = ArrayListSerializer(SimplePost.serializer())
@ -19,3 +26,14 @@ internal val Post.asSimplePost
is SimplePost -> this
else -> SimplePost(id, content, meta)
}
internal suspend fun <T> ApplicationCall.answer(
serializer: KSerializer<T>,
answerObject: T
) = respondText(ContentType.Application.Json) {
Json.plain.stringify(serializer, answerObject)
}
internal suspend fun ApplicationCall.answerBadRequest(
mustBeProvided: String
) = respond(HttpStatusCode.BadRequest, "$mustBeProvided must be provided as body of request")

View File

@ -24,29 +24,23 @@ fun Route.includePostsCoreReadModules(readPostsAPI: ReadPostsAPI) {
val post = readPostsAPI.getPostById(id)
post ?.let {
val simplePost = post.asSimplePost
call.respondText(ContentType.Application.Json) {
Json.plain.stringify(SimplePost.serializer(), simplePost)
}
call.answer(SimplePost.serializer(), simplePost)
} ?: call.respond(HttpStatusCode.NotFound, "Not found")
} ?: call.respond(HttpStatusCode.BadRequest, "Id of post must be provided as body of request")
} ?: call.answerBadRequest("Id of post")
}
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")
call.answer(postsSerializer, simplePosts)
} ?: call.answerBadRequest("Id of content")
}
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")
call.answer(postsSerializer, simplePosts)
} ?: call.answerBadRequest("Object \"DateTimeRequest\"")
}
post(getPostsByPaginationAddress) {
call.receiveOrNull<PaginationRequest>() ?.also { pagination ->
@ -58,9 +52,7 @@ fun Route.includePostsCoreReadModules(readPostsAPI: ReadPostsAPI) {
simplePosts
)
}
call.respondText(ContentType.Application.Json) {
Json.plain.stringify(paginationResultSerializer, paginationResult)
}
} ?: call.respond(HttpStatusCode.BadRequest, "Object \"PaginationRequest\" must be provided as body of request")
call.answer(paginationResultSerializer, paginationResult)
} ?: call.answerBadRequest("Object \"PaginationRequest\"")
}
}