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.api.ReadContentRepo
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.extractPagination
import com.insanusmokrassar.postssystem.ktor.server.standardKtorSerialFormatContentType
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import io.ktor.application.call
import io.ktor.http.HttpStatusCode
@ -19,30 +18,25 @@ fun Route.configureReadContentRepoRoutes(
proxyTo: ReadContentRepo
) {
get(getContentsIdsRoute) {
call.respondBytes(
standardKtorSerialFormat.dump(contentIdsSerializer, proxyTo.getContentsIds()),
standardKtorSerialFormatContentType
call.unianswer(
contentIdsSerializer,
proxyTo.getContentsIds()
)
}
get("$getContentByIdRoute/{id}") {
val id: ContentId = call.parameters["id"].also {
if (it == null) call.respond(HttpStatusCode.BadRequest, "Request must contains id in route")
} ?: return@get
call.respondBytes(
standardKtorSerialFormat.dump(
RegisteredContent.serializer().nullable,
proxyTo.getContentById(id)
),
standardKtorSerialFormatContentType
val id: ContentId = call.getParameterOrSendError("id") ?: return@get
call.unianswer(
RegisteredContent.serializer().nullable,
proxyTo.getContentById(id)
)
}
get(getContentByPaginationRoute) {
val pagination = call.request.queryParameters.extractPagination
call.respondBytes(
standardKtorSerialFormat.dump(
registeredContentPaginationResultSerializer,
proxyTo.getContentByPagination(pagination)
)
call.unianswer(
registeredContentPaginationResultSerializer,
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.api.WriteContentRepo
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.includeWebsocketHandling
import com.insanusmokrassar.postssystem.ktor.server.standardKtorSerialFormatContentType
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import io.ktor.application.call
import io.ktor.response.respondBytes
@ -25,31 +24,19 @@ fun Route.configureWriteContentRepoRoutes(
standardKtorSerialFormat.dump(RegisteredContent.serializer(), it)
}
post(registerContentRoute) {
val content = standardKtorSerialFormat.load(
Content.serializer(),
call.request.receiveChannel().toByteArray()
)
val content = call.uniload(Content.serializer())
val registered = proxyTo.registerContent(content)
call.respondBytes(
standardKtorSerialFormat.dump(
RegisteredContent.serializer().nullable,
registered
),
standardKtorSerialFormatContentType
call.unianswer(
RegisteredContent.serializer().nullable,
registered
)
}
post(deleteContentRoute) {
val contentId = standardKtorSerialFormat.load(
ContentId.serializer(),
call.request.receiveChannel().toByteArray()
)
val contentId = call.uniload(ContentId.serializer())
val isDeleted = proxyTo.deleteContent(contentId)
call.respondBytes(
standardKtorSerialFormat.dump(
Boolean.serializer(),
isDeleted
),
standardKtorSerialFormatContentType
call.unianswer(
Boolean.serializer(),
isDeleted
)
}
}

View File

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

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.post.*
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.includeWebsocketHandling
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
import io.ktor.application.call
import io.ktor.response.respondBytes
@ -27,43 +27,29 @@ fun Route.configureWritePostsRepoRoutes(
}
post(createPostRoute) {
val post = standardKtorSerialFormat.load(
Post.serializer(),
call.request.receiveChannel().toByteArray()
)
call.respondBytes(
standardKtorSerialFormat.dump(
RegisteredPost.serializer().nullable,
proxyTo.createPost(post)
call.unianswer(
RegisteredPost.serializer().nullable,
proxyTo.createPost(
call.uniload(Post.serializer())
)
)
}
post(deletePostRoute) {
val postId = standardKtorSerialFormat.load(
PostId.serializer(),
call.request.receiveChannel().toByteArray()
)
call.respondBytes(
standardKtorSerialFormat.dump(
Boolean.serializer(),
proxyTo.deletePost(postId)
)
call.unianswer(
Boolean.serializer(),
proxyTo.deletePost(call.uniload(PostId.serializer()))
)
}
post(updatePostContentRoute) {
val updatePostObject = standardKtorSerialFormat.load(
UpdatePostObject.serializer(),
call.request.receiveChannel().toByteArray()
)
val updatePostObject = call.uniload(UpdatePostObject.serializer())
call.respondBytes(
standardKtorSerialFormat.dump(
Boolean.serializer(),
proxyTo.updatePostContent(updatePostObject.postId, updatePostObject.post)
call.unianswer(
Boolean.serializer(),
proxyTo.updatePostContent(
updatePostObject.postId,
updatePostObject.post
)
)
}
@ -75,5 +61,4 @@ class WritePostsRepoRoutingConfigurator(
override fun Route.invoke() {
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")
}
}