From 13ddb106adefbdea065d4e7fc1f9b6ab396d0100 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 15 Aug 2020 15:20:00 +0600 Subject: [PATCH] add PublishedPostsRepo --- .../core/publishing/PublishingTrigger.kt | 7 ++-- .../publishing/repos/PublishedPostsRepo.kt | 36 +++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 publishing/api/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/publishing/repos/PublishedPostsRepo.kt diff --git a/publishing/api/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/publishing/PublishingTrigger.kt b/publishing/api/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/publishing/PublishingTrigger.kt index 26733612..ec047e3e 100644 --- a/publishing/api/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/publishing/PublishingTrigger.kt +++ b/publishing/api/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/publishing/PublishingTrigger.kt @@ -4,6 +4,7 @@ import com.insanusmokrassar.postssystem.core.post.PostId import com.insanusmokrassar.postssystem.core.post.RegisteredPost import com.insanusmokrassar.postssystem.core.post.repo.PostsRepo import com.insanusmokrassar.postssystem.core.post.repo.WritePostsRepo +import com.insanusmokrassar.postssystem.core.publishing.repos.PublishedPostsWriteRepo import com.insanusmokrassar.postssystem.core.publishing.repos.PublishingKeysRepo import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.Channel @@ -20,7 +21,7 @@ interface PublishingTrigger { class BusinessPublishingTrigger( private val postsRepo: PostsRepo, - private val publishedPostsRepo: WritePostsRepo, + private val publishedPostsRepo: PublishedPostsWriteRepo, private val publishingKeysRepo: PublishingKeysRepo ) : PublishingTrigger { private val postingTriggeredChannel: BroadcastChannel = BroadcastChannel(Channel.BUFFERED) @@ -31,12 +32,12 @@ class BusinessPublishingTrigger( publishingKeysRepo.unsetPostTriggerControlKey(postId) return postsRepo.getPostById(postId) ?.let { post -> - publishedPostsRepo.createPost(post) ?.let { publishedPost -> + publishedPostsRepo.registerPublishedPost(post) ?.let { publishedPost -> if (postsRepo.deletePost(postId)) { postingTriggeredChannel.send(post) postId } else { - publishedPostsRepo.deletePost(publishedPost.id) + publishedPostsRepo.unpublishPost(publishedPost.id) null } } diff --git a/publishing/api/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/publishing/repos/PublishedPostsRepo.kt b/publishing/api/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/publishing/repos/PublishedPostsRepo.kt new file mode 100644 index 00000000..293c0d83 --- /dev/null +++ b/publishing/api/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/publishing/repos/PublishedPostsRepo.kt @@ -0,0 +1,36 @@ +package com.insanusmokrassar.postssystem.core.publishing.repos + +import com.insanusmokrassar.postssystem.core.post.PostId +import com.insanusmokrassar.postssystem.core.post.RegisteredPost +import com.insanusmokrassar.postssystem.core.post.repo.PostsRepo +import com.insanusmokrassar.postssystem.core.post.repo.ReadPostsRepo +import kotlinx.coroutines.flow.Flow + +interface PublishedPostsReadRepo : ReadPostsRepo +interface PublishedPostsWriteRepo { + val postPublishedFlow: Flow + val postUnpublishedFlow: Flow + + suspend fun registerPublishedPost(registeredPost: RegisteredPost): RegisteredPost? + suspend fun unpublishPost(postId: PostId): Boolean? +} + +interface PublishedPostsRepo : PublishedPostsReadRepo, PublishedPostsWriteRepo + +class BusinessPublishedPostsRepo( + private val realPostsRepoWithPublishedPosts: PostsRepo +) : PublishedPostsRepo, ReadPostsRepo by realPostsRepoWithPublishedPosts { + override val postPublishedFlow: Flow + get() = realPostsRepoWithPublishedPosts.postCreatedFlow + override val postUnpublishedFlow: Flow + get() = realPostsRepoWithPublishedPosts.postDeletedFlow + + override suspend fun registerPublishedPost( + registeredPost: RegisteredPost + ): RegisteredPost? = realPostsRepoWithPublishedPosts.createPost(registeredPost) + + override suspend fun unpublishPost( + postId: PostId + ): Boolean? = realPostsRepoWithPublishedPosts.deletePost(postId) + +}