complete InMemoryPostsAPI and temporary add ImplicitReflectionSerializer to it
This commit is contained in:
parent
40bdd44ce8
commit
b67e1292ef
@ -40,6 +40,6 @@ data class SimplePost(
|
|||||||
data class SimpleRegisteredPost(
|
data class SimpleRegisteredPost(
|
||||||
override val id: PostId,
|
override val id: PostId,
|
||||||
override val content: ContentIds,
|
override val content: ContentIds,
|
||||||
override val creationDate: DateTime,
|
override val creationDate: DateTime = DateTime.now(),
|
||||||
override val modificationDate: DateTime = creationDate
|
override val modificationDate: DateTime = creationDate
|
||||||
) : RegisteredPost
|
) : RegisteredPost
|
@ -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.content.ContentId
|
||||||
import com.insanusmokrassar.postssystem.core.post.*
|
import com.insanusmokrassar.postssystem.core.post.*
|
||||||
|
import com.insanusmokrassar.postssystem.core.post.api.PostsAPI
|
||||||
import com.insanusmokrassar.postssystem.core.utils.pagination.*
|
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.Flow
|
||||||
|
import kotlinx.coroutines.flow.asFlow
|
||||||
|
import kotlinx.serialization.ImplicitReflectionSerializer
|
||||||
import org.joda.time.DateTime
|
import org.joda.time.DateTime
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
@ImplicitReflectionSerializer
|
||||||
|
private fun generateId(): PostId = UUID.randomUUID().toString()
|
||||||
|
|
||||||
|
@ImplicitReflectionSerializer
|
||||||
class InMemoryPostsAPI(
|
class InMemoryPostsAPI(
|
||||||
initialPosts: List<RegisteredPost> = emptyList()
|
initialPosts: List<RegisteredPost> = emptyList()
|
||||||
) : PostsAPI {
|
) : PostsAPI {
|
||||||
private val posts: MutableMap<PostId, RegisteredPost> = initialPosts.associateBy { it.id }.toMutableMap()
|
private val posts: MutableMap<PostId, RegisteredPost> = initialPosts.associateBy { it.id }.toMutableMap()
|
||||||
|
|
||||||
private val sortedByDatePosts: List<RegisteredPost>
|
private val sortedByDatePosts: List<RegisteredPost>
|
||||||
get() = posts.values.sortedBy { it.creationDate }
|
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 getPostById(id: PostId): RegisteredPost? = posts[id]
|
||||||
override suspend fun getPostsByContent(id: ContentId): List<RegisteredPost> = posts.values.filter { post ->
|
override suspend fun getPostsByContent(id: ContentId): List<RegisteredPost> = posts.values.filter { post ->
|
||||||
id in post.content
|
id in post.content
|
||||||
|
Loading…
Reference in New Issue
Block a user