From b67e1292ef2dce9ba604bb4181a241a750da9136 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 4 Nov 2019 14:01:05 +0600 Subject: [PATCH] complete InMemoryPostsAPI and temporary add ImplicitReflectionSerializer to it --- .../postssystem/core/post/Post.kt | 2 +- .../postssystem/core/api/InMemoryPostsAPI.kt | 57 ++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/core/src/main/kotlin/com/insanusmokrassar/postssystem/core/post/Post.kt b/core/src/main/kotlin/com/insanusmokrassar/postssystem/core/post/Post.kt index 15814a68..cbbe4203 100644 --- a/core/src/main/kotlin/com/insanusmokrassar/postssystem/core/post/Post.kt +++ b/core/src/main/kotlin/com/insanusmokrassar/postssystem/core/post/Post.kt @@ -40,6 +40,6 @@ data class SimplePost( data class SimpleRegisteredPost( override val id: PostId, override val content: ContentIds, - override val creationDate: DateTime, + override val creationDate: DateTime = DateTime.now(), override val modificationDate: DateTime = creationDate ) : RegisteredPost \ No newline at end of file diff --git a/core/src/test/kotlin/com/insanusmokrassar/postssystem/core/api/InMemoryPostsAPI.kt b/core/src/test/kotlin/com/insanusmokrassar/postssystem/core/api/InMemoryPostsAPI.kt index e890ef9a..0bb3bf48 100644 --- a/core/src/test/kotlin/com/insanusmokrassar/postssystem/core/api/InMemoryPostsAPI.kt +++ b/core/src/test/kotlin/com/insanusmokrassar/postssystem/core/api/InMemoryPostsAPI.kt @@ -1,19 +1,72 @@ -package com.insanusmokrassar.postssystem.core.post.api +package com.insanusmokrassar.postssystem.core.api import com.insanusmokrassar.postssystem.core.content.ContentId import com.insanusmokrassar.postssystem.core.post.* +import com.insanusmokrassar.postssystem.core.post.api.PostsAPI import com.insanusmokrassar.postssystem.core.utils.pagination.* +import kotlinx.coroutines.channels.BroadcastChannel +import kotlinx.coroutines.channels.Channel.Factory.BUFFERED import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.asFlow +import kotlinx.serialization.ImplicitReflectionSerializer import org.joda.time.DateTime +import java.util.* +@ImplicitReflectionSerializer +private fun generateId(): PostId = UUID.randomUUID().toString() + +@ImplicitReflectionSerializer class InMemoryPostsAPI( initialPosts: List = emptyList() ) : PostsAPI { private val posts: MutableMap = initialPosts.associateBy { it.id }.toMutableMap() - private val sortedByDatePosts: List get() = posts.values.sortedBy { it.creationDate } + + private val postCreatedBroadcastChannel = BroadcastChannel(BUFFERED) + override val postCreatedFlow: Flow = postCreatedBroadcastChannel.asFlow() + + private val postDeletedBroadcastChannel = BroadcastChannel(BUFFERED) + override val postDeletedFlow: Flow = postDeletedBroadcastChannel.asFlow() + + private val postUpdatedBroadcastChannel = BroadcastChannel(BUFFERED) + override val postUpdatedFlow: Flow = postUpdatedBroadcastChannel.asFlow() + + + @Synchronized + override suspend fun createPost(post: Post): RegisteredPost? { + return SimpleRegisteredPost( + generateId(), + post.content + ).also { newPost -> + posts[newPost.id] = newPost + postCreatedBroadcastChannel.send(newPost) + } + } + + @Synchronized + override suspend fun deletePost(id: PostId): Boolean { + return posts.remove(id) ?.also { + postDeletedBroadcastChannel.send(it) + } != null + } + + @Synchronized + override suspend fun updatePostContent(postId: PostId, post: Post): Boolean { + return getPostById(postId) ?.also { dbPost -> + val newPost = SimpleRegisteredPost( + dbPost.id, + post.content, + dbPost.creationDate, + DateTime.now() + ) + posts[newPost.id] = newPost + postUpdatedBroadcastChannel.send(newPost) + } != null + } + + override suspend fun getPostById(id: PostId): RegisteredPost? = posts[id] override suspend fun getPostsByContent(id: ContentId): List = posts.values.filter { post -> id in post.content