add server utils
This commit is contained in:
parent
6d6bed2eb0
commit
8104678a99
@ -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,31 +18,26 @@ 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(
|
||||
val id: ContentId = call.getParameterOrSendError("id") ?: return@get
|
||||
|
||||
call.unianswer(
|
||||
RegisteredContent.serializer().nullable,
|
||||
proxyTo.getContentById(id)
|
||||
),
|
||||
standardKtorSerialFormatContentType
|
||||
)
|
||||
}
|
||||
get(getContentByPaginationRoute) {
|
||||
val pagination = call.request.queryParameters.extractPagination
|
||||
call.respondBytes(
|
||||
standardKtorSerialFormat.dump(
|
||||
|
||||
call.unianswer(
|
||||
registeredContentPaginationResultSerializer,
|
||||
proxyTo.getContentByPagination(pagination)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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(
|
||||
call.unianswer(
|
||||
RegisteredContent.serializer().nullable,
|
||||
registered
|
||||
),
|
||||
standardKtorSerialFormatContentType
|
||||
)
|
||||
}
|
||||
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(
|
||||
call.unianswer(
|
||||
Boolean.serializer(),
|
||||
isDeleted
|
||||
),
|
||||
standardKtorSerialFormatContentType
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -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(
|
||||
val id: PostId = call.getParameterOrSendError("id") ?: return@get
|
||||
call.unianswer(
|
||||
RegisteredPost.serializer().nullable,
|
||||
proxyTo.getPostById(id)
|
||||
),
|
||||
standardKtorSerialFormatContentType
|
||||
)
|
||||
}
|
||||
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(
|
||||
val id: ContentId = call.getParameterOrSendError("id") ?: return@get
|
||||
call.unianswer(
|
||||
registeredPostsListSerializer,
|
||||
proxyTo.getPostsByContent(id)
|
||||
),
|
||||
standardKtorSerialFormatContentType
|
||||
)
|
||||
}
|
||||
get(getPostsByCreatingDatesRoute) {
|
||||
val fromToDateTime = call.request.queryParameters.extractFromToDateTime
|
||||
val pagination = call.request.queryParameters.extractPagination
|
||||
|
||||
call.respondBytes(
|
||||
standardKtorSerialFormat.dump(
|
||||
call.unianswer(
|
||||
registeredPostsPaginationResultSerializer,
|
||||
proxyTo.getPostsByCreatingDates(
|
||||
fromToDateTime.first ?: MIN_DATE,
|
||||
fromToDateTime.second ?: MAX_DATE,
|
||||
pagination
|
||||
)
|
||||
),
|
||||
standardKtorSerialFormatContentType
|
||||
)
|
||||
}
|
||||
get(getPostsByPaginationRoute) {
|
||||
val pagination = call.request.queryParameters.extractPagination
|
||||
|
||||
call.respondBytes(
|
||||
standardKtorSerialFormat.dump(
|
||||
call.unianswer(
|
||||
registeredPostsPaginationResultSerializer,
|
||||
proxyTo.getPostsByPagination(pagination)
|
||||
),
|
||||
standardKtorSerialFormatContentType
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -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(
|
||||
call.unianswer(
|
||||
RegisteredPost.serializer().nullable,
|
||||
proxyTo.createPost(post)
|
||||
proxyTo.createPost(
|
||||
call.uniload(Post.serializer())
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
post(deletePostRoute) {
|
||||
val postId = standardKtorSerialFormat.load(
|
||||
PostId.serializer(),
|
||||
call.request.receiveChannel().toByteArray()
|
||||
)
|
||||
|
||||
call.respondBytes(
|
||||
standardKtorSerialFormat.dump(
|
||||
call.unianswer(
|
||||
Boolean.serializer(),
|
||||
proxyTo.deletePost(postId)
|
||||
)
|
||||
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(
|
||||
call.unianswer(
|
||||
Boolean.serializer(),
|
||||
proxyTo.updatePostContent(updatePostObject.postId, updatePostObject.post)
|
||||
proxyTo.updatePostContent(
|
||||
updatePostObject.postId,
|
||||
updatePostObject.post
|
||||
)
|
||||
)
|
||||
}
|
||||
@ -75,5 +61,4 @@ class WritePostsRepoRoutingConfigurator(
|
||||
override fun Route.invoke() {
|
||||
configureWritePostsRepoRoutes(proxyTo)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user