improve hierachi of classes for contents and posts

This commit is contained in:
InsanusMokrassar 2019-10-19 16:58:49 +06:00
parent 16311b8f1c
commit 0c98fe3dfb
11 changed files with 73 additions and 62 deletions

View File

@ -20,20 +20,20 @@ class ReadableHttpPostsAPI(
private val postsByDatesAddress = "$baseAddress/$getPostsByDatesAddress" private val postsByDatesAddress = "$baseAddress/$getPostsByDatesAddress"
private val postsByPaginationAddress = "$baseAddress/$getPostsByPaginationAddress" private val postsByPaginationAddress = "$baseAddress/$getPostsByPaginationAddress"
override suspend fun getPostById(id: PostId): Post? { override suspend fun getPostById(id: PostId): RegisteredPost? {
return client.post<SimplePost>(postByIdAddress) { return client.post<SimpleRegisteredPost>(postByIdAddress) {
body = id body = id
} }
} }
override suspend fun getPostsByContent(id: ContentId): List<Post> { override suspend fun getPostsByContent(id: ContentId): List<RegisteredPost> {
return client.post<List<SimplePost>>(postsByContentIdAddress) { return client.post<List<SimpleRegisteredPost>>(postsByContentIdAddress) {
body = id body = id
} }
} }
override suspend fun getPostsByDates(from: DateTime?, to: DateTime?): List<Post> { override suspend fun getPostsByDates(from: DateTime?, to: DateTime?): List<RegisteredPost> {
return client.post<List<SimplePost>>(postsByDatesAddress) { return client.post<List<SimpleRegisteredPost>>(postsByDatesAddress) {
body = DateTimeRequest( body = DateTimeRequest(
from ?.millis, from ?.millis,
to ?.millis to ?.millis
@ -41,8 +41,8 @@ class ReadableHttpPostsAPI(
} }
} }
override suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<out Post> { override suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<out RegisteredPost> {
return client.post<PaginationResult<SimplePost>>(postsByPaginationAddress) { return client.post<PaginationResult<SimpleRegisteredPost>>(postsByPaginationAddress) {
body = pagination body = pagination
} }
} }

View File

@ -20,17 +20,17 @@ class WritableHttpPostsAPI(
port, port,
"/$url" "/$url"
) { ) {
Json.plain.parse(SimplePost.serializer(), it) Json.plain.parse(SimpleRegisteredPost.serializer(), it)
} }
override val postCreatedFlow: Flow<Post> = postEventFlow(eventPostsCreatedAddress) override val postCreatedFlow: Flow<RegisteredPost> = postEventFlow(eventPostsCreatedAddress)
override val postDeletedFlow: Flow<Post> = postEventFlow(eventPostsDeletedAddress) override val postDeletedFlow: Flow<RegisteredPost> = postEventFlow(eventPostsDeletedAddress)
override val postUpdatedFlow: Flow<Post> = postEventFlow(eventPostsUpdatedAddress) override val postUpdatedFlow: Flow<RegisteredPost> = postEventFlow(eventPostsUpdatedAddress)
override suspend fun createPost(content: PostContents): Post? = client.post<SimplePost>( override suspend fun createPost(post: Post): RegisteredPost? = client.post<SimpleRegisteredPost>(
createPostAddress createPostAddress
) { ) {
body = content body = post.content
} }
override suspend fun deletePost(id: PostId): Boolean = client.post(deletePostAddress) { override suspend fun deletePost(id: PostId): Boolean = client.post(deletePostAddress) {
@ -39,8 +39,8 @@ class WritableHttpPostsAPI(
override suspend fun updatePostContent( override suspend fun updatePostContent(
postId: PostId, postId: PostId,
content: PostContents post: Post
): Boolean = client.post(createPostAddress) { ): Boolean = client.post(createPostAddress) {
body = UpdatePostRequest(postId, content) body = UpdatePostRequest(postId, post.content)
} }
} }

View File

@ -1,17 +1,16 @@
package com.insanusmokrassar.postssystem.core.api package com.insanusmokrassar.postssystem.core.api
import com.insanusmokrassar.postssystem.core.post.Post
import com.insanusmokrassar.postssystem.core.post.PostId
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.utils.pagination.* import com.insanusmokrassar.postssystem.core.utils.pagination.*
import com.insanusmokrassar.postssystem.core.utils.pagination.Pagination import com.insanusmokrassar.postssystem.core.utils.pagination.Pagination
import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationResult import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationResult
import org.joda.time.DateTime import org.joda.time.DateTime
interface ReadPostsAPI { interface ReadPostsAPI {
suspend fun getPostById(id: PostId): Post? suspend fun getPostById(id: PostId): RegisteredPost?
suspend fun getPostsByContent(id: ContentId): List<Post> suspend fun getPostsByContent(id: ContentId): List<RegisteredPost>
suspend fun getPostsByDates(from: DateTime? = null, to: DateTime? = null): List<Post> suspend fun getPostsByDates(from: DateTime? = null, to: DateTime? = null): List<RegisteredPost>
suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<out Post> suspend fun getPostsByPagination(pagination: Pagination): PaginationResult<out RegisteredPost>
} }

View File

@ -5,12 +5,12 @@ import com.insanusmokrassar.postssystem.core.post.*
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
interface WritePostsAPI { interface WritePostsAPI {
val postCreatedFlow: Flow<Post> val postCreatedFlow: Flow<RegisteredPost>
val postDeletedFlow: Flow<Post> val postDeletedFlow: Flow<RegisteredPost>
val postUpdatedFlow: Flow<Post> val postUpdatedFlow: Flow<RegisteredPost>
suspend fun createPost(content: PostContents): Post? suspend fun createPost(post: Post): RegisteredPost?
suspend fun deletePost(id: PostId): Boolean suspend fun deletePost(id: PostId): Boolean
suspend fun updatePostContent(postId: PostId, content: PostContents): Boolean suspend fun updatePostContent(postId: PostId, post: Post): Boolean
} }

View File

@ -2,6 +2,8 @@ package com.insanusmokrassar.postssystem.core.content
typealias ContentId = String typealias ContentId = String
interface Content { interface Content
interface RegisteredContent : Content {
val id: ContentId val id: ContentId
} }

View File

@ -1,5 +1,20 @@
package com.insanusmokrassar.postssystem.core.content package com.insanusmokrassar.postssystem.core.content
import kotlinx.serialization.Serializable
interface TextContent : Content { interface TextContent : Content {
val text: String val text: String
} }
interface TextRegisteredContent : RegisteredContent, TextContent
@Serializable
data class SimpleTextContent(
override val text: String
) : TextContent
@Serializable
data class SimpleTextRegisteredContent(
override val id: ContentId,
override val text: String
) : TextRegisteredContent

View File

@ -1,21 +1,36 @@
package com.insanusmokrassar.postssystem.core.post package com.insanusmokrassar.postssystem.core.post
import com.insanusmokrassar.postssystem.core.content.Content import com.insanusmokrassar.postssystem.core.content.Content
import com.insanusmokrassar.postssystem.core.content.RegisteredContent
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import org.joda.time.DateTime
typealias PostId = String typealias PostId = String
typealias PostContents = List<Content> typealias PostContents = List<Content>
typealias RegisteredPostContents = List<RegisteredContent>
interface Post { interface Post {
val id: PostId
val content: PostContents val content: PostContents
}
val meta: PostMetaInfo interface RegisteredPost : Post {
val id: PostId
override val content: RegisteredPostContents
val creationDate: DateTime
val modificationDate: DateTime
} }
@Serializable @Serializable
data class SimplePost( data class SimplePost(
override val id: PostId, override val content: PostContents
override val content: PostContents,
override val meta: PostMetaInfo
) : Post ) : Post
@Serializable
data class SimpleRegisteredPost(
override val id: PostId,
override val content: RegisteredPostContents,
override val creationDate: DateTime,
override val modificationDate: DateTime = creationDate
) : RegisteredPost

View File

@ -1,9 +0,0 @@
package com.insanusmokrassar.postssystem.core.post
import org.joda.time.DateTime
interface PostMetaInfo {
val postId: PostId
val creationDate: DateTime
val modificationDate: DateTime
}

View File

@ -1,7 +1,7 @@
package com.insanusmokrassar.postssystem.core.server package com.insanusmokrassar.postssystem.core.server
import com.insanusmokrassar.postssystem.core.post.Post import com.insanusmokrassar.postssystem.core.post.Post
import com.insanusmokrassar.postssystem.core.post.SimplePost import com.insanusmokrassar.postssystem.core.post.SimpleRegisteredPost
import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationResult import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationResult
import io.ktor.application.ApplicationCall import io.ktor.application.ApplicationCall
import io.ktor.http.ContentType import io.ktor.http.ContentType
@ -15,8 +15,8 @@ import kotlinx.serialization.internal.NullableSerializer
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
internal val postsSerializer = ArrayListSerializer(SimplePost.serializer()) internal val postsSerializer = ArrayListSerializer(SimpleRegisteredPost.serializer())
internal val paginationResultSerializer = PaginationResult.serializer(SimplePost.serializer()) internal val paginationResultSerializer = PaginationResult.serializer(SimpleRegisteredPost.serializer())
internal val List<Post>.asSimplePostList internal val List<Post>.asSimplePostList
get() = map { post -> get() = map { post ->
@ -25,8 +25,8 @@ internal val List<Post>.asSimplePostList
internal val Post.asSimplePost internal val Post.asSimplePost
get() = when (this) { get() = when (this) {
is SimplePost -> this is SimpleRegisteredPost -> this
else -> SimplePost(id, content, meta) else -> SimpleRegisteredPost(id, content, meta)
} }
@InternalSerializationApi @InternalSerializationApi

View File

@ -8,17 +8,10 @@ import com.insanusmokrassar.postssystem.core.post.*
import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationRequest import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationRequest
import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationResult import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationResult
import io.ktor.application.* import io.ktor.application.*
import io.ktor.features.ContentNegotiation
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.request.receiveOrNull import io.ktor.request.receiveOrNull
import io.ktor.response.respond
import io.ktor.response.respondText
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.post import io.ktor.routing.post
import io.ktor.serialization.serialization
import kotlinx.serialization.InternalSerializationApi import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.json.Json
import org.joda.time.DateTime import org.joda.time.DateTime
@InternalSerializationApi @InternalSerializationApi
@ -26,7 +19,7 @@ fun Route.includePostsCoreReadModules(readPostsAPI: ReadPostsAPI) {
post(getPostByIdAddress) { post(getPostByIdAddress) {
call.receiveOrNull<PostId>() ?.also { id -> call.receiveOrNull<PostId>() ?.also { id ->
val post = readPostsAPI.getPostById(id) val post = readPostsAPI.getPostById(id)
call.answer(SimplePost.serializer(), post ?.asSimplePost) call.answer(SimpleRegisteredPost.serializer(), post ?.asSimplePost)
} ?: call.answerBadRequest("Id of post") } ?: call.answerBadRequest("Id of post")
} }
post(getPostsByContentIdAddress) { post(getPostsByContentIdAddress) {

View File

@ -5,14 +5,10 @@ import com.insanusmokrassar.postssystem.core.clientserver.common.*
import com.insanusmokrassar.postssystem.core.clientserver.common.models.UpdatePostRequest import com.insanusmokrassar.postssystem.core.clientserver.common.models.UpdatePostRequest
import com.insanusmokrassar.postssystem.core.post.* import com.insanusmokrassar.postssystem.core.post.*
import io.ktor.application.* import io.ktor.application.*
import io.ktor.features.ContentNegotiation
import io.ktor.http.ContentType
import io.ktor.http.cio.websocket.Frame import io.ktor.http.cio.websocket.Frame
import io.ktor.request.receiveOrNull import io.ktor.request.receiveOrNull
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.post import io.ktor.routing.post
import io.ktor.serialization.serialization
import io.ktor.websocket.WebSockets
import io.ktor.websocket.webSocket import io.ktor.websocket.webSocket
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collect
@ -24,7 +20,7 @@ private inline fun Route.createWebsocket(path: String, flow: Flow<Post>) {
webSocket("/$path") { webSocket("/$path") {
flow.collect { flow.collect {
val simplePost = it.asSimplePost val simplePost = it.asSimplePost
outgoing.send(Frame.Text(Json.plain.stringify(SimplePost.serializer(), simplePost))) outgoing.send(Frame.Text(Json.plain.stringify(SimpleRegisteredPost.serializer(), simplePost)))
} }
} }
} }
@ -41,7 +37,7 @@ fun Route.includePostsCoreWriteModules(
call.receiveOrNull<PostContents>() ?.also { contents -> call.receiveOrNull<PostContents>() ?.also { contents ->
val post = writePostsAPI.createPost(contents) val post = writePostsAPI.createPost(contents)
call.answer(SimplePost.serializer(), post ?.asSimplePost) call.answer(SimpleRegisteredPost.serializer(), post ?.asSimplePost)
} ?: call.answerBadRequest("Contents (List of Content)") } ?: call.answerBadRequest("Contents (List of Content)")
} }