complete InMemoryPostsAPI and temporary add ImplicitReflectionSerializer to it

This commit is contained in:
InsanusMokrassar 2019-11-04 14:01:05 +06:00
parent 40bdd44ce8
commit b67e1292ef
2 changed files with 56 additions and 3 deletions

View File

@ -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

View File

@ -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<RegisteredPost> = emptyList()
) : PostsAPI {
private val posts: MutableMap<PostId, RegisteredPost> = initialPosts.associateBy { it.id }.toMutableMap()
private val sortedByDatePosts: List<RegisteredPost>
get() = posts.values.sortedBy { it.creationDate }
private val postCreatedBroadcastChannel = BroadcastChannel<RegisteredPost>(BUFFERED)
override val postCreatedFlow: Flow<RegisteredPost> = postCreatedBroadcastChannel.asFlow()
private val postDeletedBroadcastChannel = BroadcastChannel<RegisteredPost>(BUFFERED)
override val postDeletedFlow: Flow<RegisteredPost> = postDeletedBroadcastChannel.asFlow()
private val postUpdatedBroadcastChannel = BroadcastChannel<RegisteredPost>(BUFFERED)
override val postUpdatedFlow: Flow<RegisteredPost> = 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<RegisteredPost> = posts.values.filter { post ->
id in post.content