diff --git a/business_cases/post_creating/client/src/commonMain/kotlin/dev/inmo/postssystem/business_cases/post_creating/client/PostCreatingClientCase.kt b/business_cases/post_creating/client/src/commonMain/kotlin/dev/inmo/postssystem/business_cases/post_creating/client/PostCreatingClientCase.kt index 4e3f2d4c..e76d5950 100644 --- a/business_cases/post_creating/client/src/commonMain/kotlin/dev/inmo/postssystem/business_cases/post_creating/client/PostCreatingClientCase.kt +++ b/business_cases/post_creating/client/src/commonMain/kotlin/dev/inmo/postssystem/business_cases/post_creating/client/PostCreatingClientCase.kt @@ -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, triggerId: TriggerId?): RegisteredPost? = client.unipost( + private val realBaseUrl = rootRoute ?.let { "$baseUrl/$rootRoute" } ?: baseUrl + override suspend fun createPost( + postContent: List, + triggerId: TriggerId? + ): RegisteredPost? = unifiedRequester.unipost( buildStandardUrl(realBaseUrl, postCreatingCreatePostRoute), BodyPair(PostCreatingCreatePostModel.serializer(), PostCreatingCreatePostModel(postContent, triggerId)), - RegisteredPost.serializer() + RegisteredPost.serializer().nullable ) } diff --git a/business_cases/post_creating/common/src/commonMain/kotlin/dev/inmo/postssystem/business_cases/post_creating/server/BusinessPostCreatingCase.kt b/business_cases/post_creating/common/src/commonMain/kotlin/dev/inmo/postssystem/business_cases/post_creating/server/BusinessPostCreatingCase.kt index f1358a48..e993efaa 100644 --- a/business_cases/post_creating/common/src/commonMain/kotlin/dev/inmo/postssystem/business_cases/post_creating/server/BusinessPostCreatingCase.kt +++ b/business_cases/post_creating/common/src/commonMain/kotlin/dev/inmo/postssystem/business_cases/post_creating/server/BusinessPostCreatingCase.kt @@ -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, 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, 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 + } +} diff --git a/business_cases/post_creating/server/src/jvmMain/kotlin/dev/inmo/postssystem/business_cases/post_creating/server/PostCreatingRoutingCase.kt b/business_cases/post_creating/server/src/jvmMain/kotlin/dev/inmo/postssystem/business_cases/post_creating/server/PostCreatingRoutingCase.kt index 923f168b..d952671e 100644 --- a/business_cases/post_creating/server/src/jvmMain/kotlin/dev/inmo/postssystem/business_cases/post_creating/server/PostCreatingRoutingCase.kt +++ b/business_cases/post_creating/server/src/jvmMain/kotlin/dev/inmo/postssystem/business_cases/post_creating/server/PostCreatingRoutingCase.kt @@ -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()) + post(postCreatingCreatePostRoute) { + 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) +}