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 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.business_cases.post_creating.server.*
import dev.inmo.postssystem.core.content.Content import dev.inmo.postssystem.core.content.Content
import dev.inmo.postssystem.core.post.RegisteredPost import dev.inmo.postssystem.core.post.RegisteredPost
import dev.inmo.postssystem.core.publishing.TriggerId 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 dev.inmo.micro_utils.ktor.common.buildStandardUrl
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.serialization.builtins.nullable
class PostCreatingClientCase( class PostCreatingClientCase(
private val baseUrl: String, private val baseUrl: String,
private val client: HttpClient private val unifiedRequester: UnifiedRequester,
private val rootRoute: String? = postCreatingRootRoute
) : PostCreatingCase { ) : PostCreatingCase {
private val realBaseUrl = "$baseUrl/$postCreatingRootRoute" private val realBaseUrl = rootRoute ?.let { "$baseUrl/$rootRoute" } ?: baseUrl
override suspend fun createPost(postContent: List<Content>, triggerId: TriggerId?): RegisteredPost? = client.unipost( override suspend fun createPost(
postContent: List<Content>,
triggerId: TriggerId?
): RegisteredPost? = unifiedRequester.unipost(
buildStandardUrl(realBaseUrl, postCreatingCreatePostRoute), buildStandardUrl(realBaseUrl, postCreatingCreatePostRoute),
BodyPair(PostCreatingCreatePostModel.serializer(), PostCreatingCreatePostModel(postContent, triggerId)), 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 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.Content
import dev.inmo.postssystem.core.content.api.ContentRepo import dev.inmo.postssystem.core.content.api.ContentRepo
import dev.inmo.postssystem.core.post.* import dev.inmo.postssystem.core.post.*
import dev.inmo.postssystem.core.post.repo.PostsRepo import dev.inmo.postssystem.core.post.repo.PostsRepo
import dev.inmo.postssystem.core.publishing.* import dev.inmo.postssystem.core.publishing.*
import dev.inmo.postssystem.core.publishing.repos.WriteTriggersToPostsRepo
//class BusinessPostCreatingCase( class BusinessPostCreatingCase(
// private val postsRepo: PostsRepo, private val postsRepo: PostsRepo,
// private val contentRepo: ContentRepo, private val contentRepo: ContentRepo,
// private val publishingRegistrar: PublishingRegistrar, private val postsTriggersToPostsRepo: WriteTriggersToPostsRepo
// private val postKeyGenerator: PostKeyGenerator = { _, _ -> uuid4().toString() }, ) : PostCreatingCase {
// private val publishingKeyReceiverGetter: PublishingKeyReceiverGetter override suspend fun createPost(postContent: List<Content>, triggerId: TriggerId?): RegisteredPost? {
//) : PostCreatingCase { val content = contentRepo.create(postContent)
// override suspend fun createPost(postContent: List<Content>, triggerId: TriggerId?): RegisteredPost? { val post = postsRepo.createPost(SimplePost(content.map { it.id })) ?: return null
// val content = postContent.mapNotNull { contentRepo.registerContent(it) }
// val post = postsRepo.createPost(SimplePost(content.map { it.id })) ?: return null triggerId ?.let {
// postsTriggersToPostsRepo.set(post.id, triggerId)
// triggerId ?.let { }
// val key = postKeyGenerator(post.id, triggerId)
// return post
// if (publishingRegistrar.registerTriggerForPost(key, post.id)) { }
// publishingKeyReceiverGetter(it) ?.acceptKey(post.id, key) }
// }
// }
//
// return post
// }
//}

View File

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