update creating case

This commit is contained in:
InsanusMokrassar 2020-11-30 14:13:39 +06:00
parent aba2152a84
commit 85faf0b850
3 changed files with 46 additions and 37 deletions

View File

@ -1,22 +1,26 @@
package dev.inmo.postssystem.business_cases.post_creating.client
import dev.inmo.micro_utils.ktor.client.*
import dev.inmo.postssystem.business_cases.post_creating.server.*
import dev.inmo.postssystem.core.content.Content
import dev.inmo.postssystem.core.post.RegisteredPost
import dev.inmo.postssystem.core.publishing.TriggerId
import dev.inmo.micro_utils.ktor.client.BodyPair
import dev.inmo.micro_utils.ktor.client.unipost
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
import io.ktor.client.HttpClient
import kotlinx.serialization.builtins.nullable
class PostCreatingClientCase(
private val baseUrl: String,
private val client: HttpClient
private val unifiedRequester: UnifiedRequester,
private val rootRoute: String? = postCreatingRootRoute
) : PostCreatingCase {
private val realBaseUrl = "$baseUrl/$postCreatingRootRoute"
override suspend fun createPost(postContent: List<Content>, triggerId: TriggerId?): RegisteredPost? = client.unipost(
private val realBaseUrl = rootRoute ?.let { "$baseUrl/$rootRoute" } ?: baseUrl
override suspend fun createPost(
postContent: List<Content>,
triggerId: TriggerId?
): RegisteredPost? = unifiedRequester.unipost(
buildStandardUrl(realBaseUrl, postCreatingCreatePostRoute),
BodyPair(PostCreatingCreatePostModel.serializer(), PostCreatingCreatePostModel(postContent, triggerId)),
RegisteredPost.serializer()
RegisteredPost.serializer().nullable
)
}

View File

@ -1,31 +1,26 @@
package dev.inmo.postssystem.business_cases.post_creating.server
import com.benasher44.uuid.uuid4
import dev.inmo.micro_utils.repos.set
import dev.inmo.postssystem.core.content.Content
import dev.inmo.postssystem.core.content.api.ContentRepo
import dev.inmo.postssystem.core.post.*
import dev.inmo.postssystem.core.post.repo.PostsRepo
import dev.inmo.postssystem.core.publishing.*
import dev.inmo.postssystem.core.publishing.repos.WriteTriggersToPostsRepo
//class BusinessPostCreatingCase(
// private val postsRepo: PostsRepo,
// private val contentRepo: ContentRepo,
// private val publishingRegistrar: PublishingRegistrar,
// private val postKeyGenerator: PostKeyGenerator = { _, _ -> uuid4().toString() },
// private val publishingKeyReceiverGetter: PublishingKeyReceiverGetter
//) : PostCreatingCase {
// override suspend fun createPost(postContent: List<Content>, triggerId: TriggerId?): RegisteredPost? {
// val content = postContent.mapNotNull { contentRepo.registerContent(it) }
// val post = postsRepo.createPost(SimplePost(content.map { it.id })) ?: return null
//
// triggerId ?.let {
// val key = postKeyGenerator(post.id, triggerId)
//
// if (publishingRegistrar.registerTriggerForPost(key, post.id)) {
// publishingKeyReceiverGetter(it) ?.acceptKey(post.id, key)
// }
// }
//
// return post
// }
//}
class BusinessPostCreatingCase(
private val postsRepo: PostsRepo,
private val contentRepo: ContentRepo,
private val postsTriggersToPostsRepo: WriteTriggersToPostsRepo
) : PostCreatingCase {
override suspend fun createPost(postContent: List<Content>, triggerId: TriggerId?): RegisteredPost? {
val content = contentRepo.create(postContent)
val post = postsRepo.createPost(SimplePost(content.map { it.id })) ?: return null
triggerId ?.let {
postsTriggersToPostsRepo.set(post.id, triggerId)
}
return post
}
}

View File

@ -1,23 +1,33 @@
package dev.inmo.postssystem.business_cases.post_creating.server
import dev.inmo.micro_utils.ktor.server.*
import dev.inmo.postssystem.core.post.RegisteredPost
import dev.inmo.micro_utils.ktor.server.unianswer
import dev.inmo.micro_utils.ktor.server.uniload
import io.ktor.application.call
import io.ktor.routing.*
import kotlinx.serialization.builtins.nullable
fun Route.configurePostCreatingRoutes(
origin: PostCreatingCase
private inline fun Route.configurePostCreatingRoutes(
origin: PostCreatingCase,
unifiedRouter: UnifiedRouter
) {
route(postCreatingRootRoute) {
post(postCreatingCreatePostRoute) {
val model = call.uniload(PostCreatingCreatePostModel.serializer())
unifiedRouter.apply {
val model = uniload(PostCreatingCreatePostModel.serializer())
call.unianswer(
unianswer(
RegisteredPost.serializer().nullable,
origin.createPost(model.postContent, model.triggerId)
)
}
}
}
fun Route.configurePostCreatingRoutes(
origin: PostCreatingCase,
unifiedRouter: UnifiedRouter,
subroute: String? = postCreatingRootRoute
) {
subroute ?.also {
route(subroute) { configurePostCreatingRoutes(origin, unifiedRouter) }
} ?: configurePostCreatingRoutes(origin, unifiedRouter)
}