From a51645c92c54073d0d67f05e93fd66b927078a97 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 4 Nov 2019 13:42:15 +0600 Subject: [PATCH] add ContentAPI --- .../core/content/api/ContentAPI.kt | 3 +++ .../core/content/api/ReadContentAPI.kt | 22 +++++++++++++++++++ .../core/content/api/WriteContentAPI.kt | 12 ++++++++++ .../postssystem/core/api/InMemoryPostsAPI.kt | 12 +++------- 4 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 core/src/main/kotlin/com/insanusmokrassar/postssystem/core/content/api/ContentAPI.kt create mode 100644 core/src/main/kotlin/com/insanusmokrassar/postssystem/core/content/api/ReadContentAPI.kt create mode 100644 core/src/main/kotlin/com/insanusmokrassar/postssystem/core/content/api/WriteContentAPI.kt diff --git a/core/src/main/kotlin/com/insanusmokrassar/postssystem/core/content/api/ContentAPI.kt b/core/src/main/kotlin/com/insanusmokrassar/postssystem/core/content/api/ContentAPI.kt new file mode 100644 index 00000000..c1e6aa17 --- /dev/null +++ b/core/src/main/kotlin/com/insanusmokrassar/postssystem/core/content/api/ContentAPI.kt @@ -0,0 +1,3 @@ +package com.insanusmokrassar.postssystem.core.content.api + +interface ContentAPI : ReadContentAPI, WriteContentAPI diff --git a/core/src/main/kotlin/com/insanusmokrassar/postssystem/core/content/api/ReadContentAPI.kt b/core/src/main/kotlin/com/insanusmokrassar/postssystem/core/content/api/ReadContentAPI.kt new file mode 100644 index 00000000..bb479a8e --- /dev/null +++ b/core/src/main/kotlin/com/insanusmokrassar/postssystem/core/content/api/ReadContentAPI.kt @@ -0,0 +1,22 @@ +package com.insanusmokrassar.postssystem.core.content.api + +import com.insanusmokrassar.postssystem.core.content.ContentId +import com.insanusmokrassar.postssystem.core.content.RegisteredContent +import com.insanusmokrassar.postssystem.core.post.RegisteredPost +import com.insanusmokrassar.postssystem.core.utils.pagination.Pagination +import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationResult + +/** + * Simple read API by different properties of [com.insanusmokrassar.postssystem.core.content.Content]. + */ +interface ReadContentAPI { + /** + * @return [RegisteredContent] if it is available by [id] + */ + suspend fun getContentById(id: ContentId): RegisteredContent? + + /** + * @return all [RegisteredContent] by pages basing on their creation date + */ + suspend fun getContentByPagination(pagination: Pagination): PaginationResult +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/insanusmokrassar/postssystem/core/content/api/WriteContentAPI.kt b/core/src/main/kotlin/com/insanusmokrassar/postssystem/core/content/api/WriteContentAPI.kt new file mode 100644 index 00000000..3fbad021 --- /dev/null +++ b/core/src/main/kotlin/com/insanusmokrassar/postssystem/core/content/api/WriteContentAPI.kt @@ -0,0 +1,12 @@ +package com.insanusmokrassar.postssystem.core.content.api + +import com.insanusmokrassar.postssystem.core.content.* +import kotlinx.coroutines.flow.Flow + +interface WriteContentAPI { + val contentCreatedFlow: Flow + val contentDeletedFlow: Flow + + suspend fun createContent(post: Content): RegisteredContent? + suspend fun deleteContent(id: ContentId): Boolean +} 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 00d18702..e890ef9a 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,28 +1,22 @@ package com.insanusmokrassar.postssystem.core.post.api import com.insanusmokrassar.postssystem.core.content.ContentId -import com.insanusmokrassar.postssystem.core.content.RegisteredContent -import com.insanusmokrassar.postssystem.core.post.PostId -import com.insanusmokrassar.postssystem.core.post.RegisteredPost +import com.insanusmokrassar.postssystem.core.post.* import com.insanusmokrassar.postssystem.core.utils.pagination.* +import kotlinx.coroutines.flow.Flow import org.joda.time.DateTime class InMemoryPostsAPI( initialPosts: List = emptyList() ) : PostsAPI { private val posts: MutableMap = initialPosts.associateBy { it.id }.toMutableMap() - private val content: MutableMap = initialPosts.asSequence().flatMap { - it.content.asSequence() - }.associateBy { - it.id - }.toMutableMap() private val sortedByDatePosts: List get() = posts.values.sortedBy { it.creationDate } override suspend fun getPostById(id: PostId): RegisteredPost? = posts[id] override suspend fun getPostsByContent(id: ContentId): List = posts.values.filter { post -> - post.content.any { it.id == id } + id in post.content } override suspend fun getPostsByDates(from: DateTime?, to: DateTime?): List = posts.values.filter { from ?.let { _ -> it.creationDate >= from } ?: true && to ?.let { _ -> it.creationDate <= to } ?: true