add PublishedPostsRepo

This commit is contained in:
InsanusMokrassar 2020-08-15 15:20:00 +06:00
parent dbf8e3716c
commit 13ddb106ad
2 changed files with 40 additions and 3 deletions

View File

@ -4,6 +4,7 @@ import com.insanusmokrassar.postssystem.core.post.PostId
import com.insanusmokrassar.postssystem.core.post.RegisteredPost import com.insanusmokrassar.postssystem.core.post.RegisteredPost
import com.insanusmokrassar.postssystem.core.post.repo.PostsRepo import com.insanusmokrassar.postssystem.core.post.repo.PostsRepo
import com.insanusmokrassar.postssystem.core.post.repo.WritePostsRepo 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 com.insanusmokrassar.postssystem.core.publishing.repos.PublishingKeysRepo
import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
@ -20,7 +21,7 @@ interface PublishingTrigger {
class BusinessPublishingTrigger( class BusinessPublishingTrigger(
private val postsRepo: PostsRepo, private val postsRepo: PostsRepo,
private val publishedPostsRepo: WritePostsRepo, private val publishedPostsRepo: PublishedPostsWriteRepo,
private val publishingKeysRepo: PublishingKeysRepo private val publishingKeysRepo: PublishingKeysRepo
) : PublishingTrigger { ) : PublishingTrigger {
private val postingTriggeredChannel: BroadcastChannel<RegisteredPost> = BroadcastChannel(Channel.BUFFERED) private val postingTriggeredChannel: BroadcastChannel<RegisteredPost> = BroadcastChannel(Channel.BUFFERED)
@ -31,12 +32,12 @@ class BusinessPublishingTrigger(
publishingKeysRepo.unsetPostTriggerControlKey(postId) publishingKeysRepo.unsetPostTriggerControlKey(postId)
return postsRepo.getPostById(postId) ?.let { post -> return postsRepo.getPostById(postId) ?.let { post ->
publishedPostsRepo.createPost(post) ?.let { publishedPost -> publishedPostsRepo.registerPublishedPost(post) ?.let { publishedPost ->
if (postsRepo.deletePost(postId)) { if (postsRepo.deletePost(postId)) {
postingTriggeredChannel.send(post) postingTriggeredChannel.send(post)
postId postId
} else { } else {
publishedPostsRepo.deletePost(publishedPost.id) publishedPostsRepo.unpublishPost(publishedPost.id)
null null
} }
} }

View File

@ -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<RegisteredPost>
val postUnpublishedFlow: Flow<RegisteredPost>
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<RegisteredPost>
get() = realPostsRepoWithPublishedPosts.postCreatedFlow
override val postUnpublishedFlow: Flow<RegisteredPost>
get() = realPostsRepoWithPublishedPosts.postDeletedFlow
override suspend fun registerPublishedPost(
registeredPost: RegisteredPost
): RegisteredPost? = realPostsRepoWithPublishedPosts.createPost(registeredPost)
override suspend fun unpublishPost(
postId: PostId
): Boolean? = realPostsRepoWithPublishedPosts.deletePost(postId)
}