Merge branch 'master' into posting-ktor-common

This commit is contained in:
000Sanya 2020-08-15 13:37:58 +10:00
commit 544a74588c
5 changed files with 93 additions and 111 deletions

View File

@ -4,9 +4,8 @@ import com.insanusmokrassar.postssystem.core.content.ContentId
import com.insanusmokrassar.postssystem.core.content.RegisteredContent import com.insanusmokrassar.postssystem.core.content.RegisteredContent
import com.insanusmokrassar.postssystem.core.content.api.ReadContentRepo import com.insanusmokrassar.postssystem.core.content.api.ReadContentRepo
import com.insanusmokrassar.postssystem.core.ktor.* import com.insanusmokrassar.postssystem.core.ktor.*
import com.insanusmokrassar.postssystem.ktor.server.*
import com.insanusmokrassar.postssystem.ktor.server.configurators.ApplicationRoutingConfigurator import com.insanusmokrassar.postssystem.ktor.server.configurators.ApplicationRoutingConfigurator
import com.insanusmokrassar.postssystem.ktor.server.extractPagination
import com.insanusmokrassar.postssystem.ktor.server.standardKtorSerialFormatContentType
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import io.ktor.application.call import io.ktor.application.call
import io.ktor.http.HttpStatusCode import io.ktor.http.HttpStatusCode
@ -19,31 +18,26 @@ fun Route.configureReadContentRepoRoutes(
proxyTo: ReadContentRepo proxyTo: ReadContentRepo
) { ) {
get(getContentsIdsRoute) { get(getContentsIdsRoute) {
call.respondBytes( call.unianswer(
standardKtorSerialFormat.dump(contentIdsSerializer, proxyTo.getContentsIds()), contentIdsSerializer,
standardKtorSerialFormatContentType proxyTo.getContentsIds()
) )
} }
get("$getContentByIdRoute/{id}") { get("$getContentByIdRoute/{id}") {
val id: ContentId = call.parameters["id"].also { val id: ContentId = call.getParameterOrSendError("id") ?: return@get
if (it == null) call.respond(HttpStatusCode.BadRequest, "Request must contains id in route")
} ?: return@get call.unianswer(
call.respondBytes(
standardKtorSerialFormat.dump(
RegisteredContent.serializer().nullable, RegisteredContent.serializer().nullable,
proxyTo.getContentById(id) proxyTo.getContentById(id)
),
standardKtorSerialFormatContentType
) )
} }
get(getContentByPaginationRoute) { get(getContentByPaginationRoute) {
val pagination = call.request.queryParameters.extractPagination val pagination = call.request.queryParameters.extractPagination
call.respondBytes(
standardKtorSerialFormat.dump( call.unianswer(
registeredContentPaginationResultSerializer, registeredContentPaginationResultSerializer,
proxyTo.getContentByPagination(pagination) proxyTo.getContentByPagination(pagination)
) )
)
} }
} }

View File

@ -3,9 +3,8 @@ package com.insanusmokrassar.postssystem.core.ktor.server.content
import com.insanusmokrassar.postssystem.core.content.* import com.insanusmokrassar.postssystem.core.content.*
import com.insanusmokrassar.postssystem.core.content.api.WriteContentRepo import com.insanusmokrassar.postssystem.core.content.api.WriteContentRepo
import com.insanusmokrassar.postssystem.core.ktor.* import com.insanusmokrassar.postssystem.core.ktor.*
import com.insanusmokrassar.postssystem.ktor.server.*
import com.insanusmokrassar.postssystem.ktor.server.configurators.ApplicationRoutingConfigurator import com.insanusmokrassar.postssystem.ktor.server.configurators.ApplicationRoutingConfigurator
import com.insanusmokrassar.postssystem.ktor.server.includeWebsocketHandling
import com.insanusmokrassar.postssystem.ktor.server.standardKtorSerialFormatContentType
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import io.ktor.application.call import io.ktor.application.call
import io.ktor.response.respondBytes import io.ktor.response.respondBytes
@ -25,31 +24,19 @@ fun Route.configureWriteContentRepoRoutes(
standardKtorSerialFormat.dump(RegisteredContent.serializer(), it) standardKtorSerialFormat.dump(RegisteredContent.serializer(), it)
} }
post(registerContentRoute) { post(registerContentRoute) {
val content = standardKtorSerialFormat.load( val content = call.uniload(Content.serializer())
Content.serializer(),
call.request.receiveChannel().toByteArray()
)
val registered = proxyTo.registerContent(content) val registered = proxyTo.registerContent(content)
call.respondBytes( call.unianswer(
standardKtorSerialFormat.dump(
RegisteredContent.serializer().nullable, RegisteredContent.serializer().nullable,
registered registered
),
standardKtorSerialFormatContentType
) )
} }
post(deleteContentRoute) { post(deleteContentRoute) {
val contentId = standardKtorSerialFormat.load( val contentId = call.uniload(ContentId.serializer())
ContentId.serializer(),
call.request.receiveChannel().toByteArray()
)
val isDeleted = proxyTo.deleteContent(contentId) val isDeleted = proxyTo.deleteContent(contentId)
call.respondBytes( call.unianswer(
standardKtorSerialFormat.dump(
Boolean.serializer(), Boolean.serializer(),
isDeleted isDeleted
),
standardKtorSerialFormatContentType
) )
} }
} }

View File

@ -22,64 +22,44 @@ fun Route.configureReadPostsRepoRoutes(
proxyTo: ReadPostsRepo proxyTo: ReadPostsRepo
) { ) {
get(getPostsIdsRoute) { get(getPostsIdsRoute) {
call.respondBytes( call.unianswer(
standardKtorSerialFormat.dump(postIdsSerializer, proxyTo.getPostsIds()), postIdsSerializer,
standardKtorSerialFormatContentType proxyTo.getPostsIds()
) )
} }
get("$getPostByIdRoute/{id}") { get("$getPostByIdRoute/{id}") {
val id: PostId = call.parameters["id"].also { val id: PostId = call.getParameterOrSendError("id") ?: return@get
if (it == null) { call.unianswer(
call.respond(HttpStatusCode.BadRequest, "request must contains id")
}
} ?: return@get
call.respondBytes(
standardKtorSerialFormat.dump(
RegisteredPost.serializer().nullable, RegisteredPost.serializer().nullable,
proxyTo.getPostById(id) proxyTo.getPostById(id)
),
standardKtorSerialFormatContentType
) )
} }
get("$getPostsByContentRoute/{id}") { get("$getPostsByContentRoute/{id}") {
val id: ContentId = call.parameters["id"].also { val id: ContentId = call.getParameterOrSendError("id") ?: return@get
if (it == null) { call.unianswer(
call.respond(HttpStatusCode.BadRequest, "request must contains id")
}
} ?: return@get
call.respondBytes(
standardKtorSerialFormat.dump(
registeredPostsListSerializer, registeredPostsListSerializer,
proxyTo.getPostsByContent(id) proxyTo.getPostsByContent(id)
),
standardKtorSerialFormatContentType
) )
} }
get(getPostsByCreatingDatesRoute) { get(getPostsByCreatingDatesRoute) {
val fromToDateTime = call.request.queryParameters.extractFromToDateTime val fromToDateTime = call.request.queryParameters.extractFromToDateTime
val pagination = call.request.queryParameters.extractPagination val pagination = call.request.queryParameters.extractPagination
call.respondBytes( call.unianswer(
standardKtorSerialFormat.dump(
registeredPostsPaginationResultSerializer, registeredPostsPaginationResultSerializer,
proxyTo.getPostsByCreatingDates( proxyTo.getPostsByCreatingDates(
fromToDateTime.first ?: MIN_DATE, fromToDateTime.first ?: MIN_DATE,
fromToDateTime.second ?: MAX_DATE, fromToDateTime.second ?: MAX_DATE,
pagination pagination
) )
),
standardKtorSerialFormatContentType
) )
} }
get(getPostsByPaginationRoute) { get(getPostsByPaginationRoute) {
val pagination = call.request.queryParameters.extractPagination val pagination = call.request.queryParameters.extractPagination
call.respondBytes( call.unianswer(
standardKtorSerialFormat.dump(
registeredPostsPaginationResultSerializer, registeredPostsPaginationResultSerializer,
proxyTo.getPostsByPagination(pagination) proxyTo.getPostsByPagination(pagination)
),
standardKtorSerialFormatContentType
) )
} }
} }

View File

@ -3,8 +3,8 @@ package com.insanusmokrassar.postssystem.core.ktor.server.post
import com.insanusmokrassar.postssystem.core.ktor.* import com.insanusmokrassar.postssystem.core.ktor.*
import com.insanusmokrassar.postssystem.core.post.* import com.insanusmokrassar.postssystem.core.post.*
import com.insanusmokrassar.postssystem.core.post.repo.WritePostsRepo import com.insanusmokrassar.postssystem.core.post.repo.WritePostsRepo
import com.insanusmokrassar.postssystem.ktor.server.*
import com.insanusmokrassar.postssystem.ktor.server.configurators.ApplicationRoutingConfigurator import com.insanusmokrassar.postssystem.ktor.server.configurators.ApplicationRoutingConfigurator
import com.insanusmokrassar.postssystem.ktor.server.includeWebsocketHandling
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import io.ktor.application.call import io.ktor.application.call
import io.ktor.response.respondBytes import io.ktor.response.respondBytes
@ -27,43 +27,29 @@ fun Route.configureWritePostsRepoRoutes(
} }
post(createPostRoute) { post(createPostRoute) {
val post = standardKtorSerialFormat.load( call.unianswer(
Post.serializer(),
call.request.receiveChannel().toByteArray()
)
call.respondBytes(
standardKtorSerialFormat.dump(
RegisteredPost.serializer().nullable, RegisteredPost.serializer().nullable,
proxyTo.createPost(post) proxyTo.createPost(
call.uniload(Post.serializer())
) )
) )
} }
post(deletePostRoute) { post(deletePostRoute) {
val postId = standardKtorSerialFormat.load( call.unianswer(
PostId.serializer(),
call.request.receiveChannel().toByteArray()
)
call.respondBytes(
standardKtorSerialFormat.dump(
Boolean.serializer(), Boolean.serializer(),
proxyTo.deletePost(postId) proxyTo.deletePost(call.uniload(PostId.serializer()))
)
) )
} }
post(updatePostContentRoute) { post(updatePostContentRoute) {
val updatePostObject = standardKtorSerialFormat.load( val updatePostObject = call.uniload(UpdatePostObject.serializer())
UpdatePostObject.serializer(),
call.request.receiveChannel().toByteArray()
)
call.respondBytes( call.unianswer(
standardKtorSerialFormat.dump(
Boolean.serializer(), Boolean.serializer(),
proxyTo.updatePostContent(updatePostObject.postId, updatePostObject.post) proxyTo.updatePostContent(
updatePostObject.postId,
updatePostObject.post
) )
) )
} }
@ -75,5 +61,4 @@ class WritePostsRepoRoutingConfigurator(
override fun Route.invoke() { override fun Route.invoke() {
configureWritePostsRepoRoutes(proxyTo) configureWritePostsRepoRoutes(proxyTo)
} }
} }

View File

@ -0,0 +1,36 @@
package com.insanusmokrassar.postssystem.ktor.server
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import io.ktor.application.ApplicationCall
import io.ktor.application.call
import io.ktor.http.HttpStatusCode
import io.ktor.response.respond
import io.ktor.response.respondBytes
import io.ktor.util.toByteArray
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerializationStrategy
suspend fun <T> ApplicationCall.unianswer(
answerSerializer: SerializationStrategy<T>,
answer: T
) {
respondBytes(
standardKtorSerialFormat.dump(answerSerializer, answer),
standardKtorSerialFormatContentType
)
}
suspend fun <T> ApplicationCall.uniload(
deserializer: DeserializationStrategy<T>
) = standardKtorSerialFormat.load(
deserializer,
request.receiveChannel().toByteArray()
)
suspend fun ApplicationCall.getParameterOrSendError(
field: String
) = parameters[field].also {
if (it == null) {
respond(HttpStatusCode.BadRequest, "request must contains $field")
}
}