add ContentAPI

This commit is contained in:
InsanusMokrassar 2019-11-04 13:42:15 +06:00
parent 3be34a94bc
commit a51645c92c
4 changed files with 40 additions and 9 deletions

View File

@ -0,0 +1,3 @@
package com.insanusmokrassar.postssystem.core.content.api
interface ContentAPI : ReadContentAPI, WriteContentAPI

View File

@ -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<out RegisteredContent>
}

View File

@ -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<RegisteredContent>
val contentDeletedFlow: Flow<RegisteredContent>
suspend fun createContent(post: Content): RegisteredContent?
suspend fun deleteContent(id: ContentId): Boolean
}

View File

@ -1,28 +1,22 @@
package com.insanusmokrassar.postssystem.core.post.api package com.insanusmokrassar.postssystem.core.post.api
import com.insanusmokrassar.postssystem.core.content.ContentId import com.insanusmokrassar.postssystem.core.content.ContentId
import com.insanusmokrassar.postssystem.core.content.RegisteredContent import com.insanusmokrassar.postssystem.core.post.*
import com.insanusmokrassar.postssystem.core.post.PostId
import com.insanusmokrassar.postssystem.core.post.RegisteredPost
import com.insanusmokrassar.postssystem.core.utils.pagination.* import com.insanusmokrassar.postssystem.core.utils.pagination.*
import kotlinx.coroutines.flow.Flow
import org.joda.time.DateTime import org.joda.time.DateTime
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 content: MutableMap<ContentId, RegisteredContent> = initialPosts.asSequence().flatMap {
it.content.asSequence()
}.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 }
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 ->
post.content.any { it.id == id } id in post.content
} }
override suspend fun getPostsByDates(from: DateTime?, to: DateTime?): List<RegisteredPost> = posts.values.filter { override suspend fun getPostsByDates(from: DateTime?, to: DateTime?): List<RegisteredPost> = posts.values.filter {
from ?.let { _ -> it.creationDate >= from } ?: true && to ?.let { _ -> it.creationDate <= to } ?: true from ?.let { _ -> it.creationDate >= from } ?: true && to ?.let { _ -> it.creationDate <= to } ?: true