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

View File

@ -20,17 +20,17 @@ class WritableHttpPostsAPI(
port,
"/$url"
) {
Json.plain.parse(SimplePost.serializer(), it)
Json.plain.parse(SimpleRegisteredPost.serializer(), it)
}
override val postCreatedFlow: Flow<Post> = postEventFlow(eventPostsCreatedAddress)
override val postDeletedFlow: Flow<Post> = postEventFlow(eventPostsDeletedAddress)
override val postUpdatedFlow: Flow<Post> = postEventFlow(eventPostsUpdatedAddress)
override val postCreatedFlow: Flow<RegisteredPost> = postEventFlow(eventPostsCreatedAddress)
override val postDeletedFlow: Flow<RegisteredPost> = postEventFlow(eventPostsDeletedAddress)
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
) {
body = content
body = post.content
}
override suspend fun deletePost(id: PostId): Boolean = client.post(deletePostAddress) {
@ -39,8 +39,8 @@ class WritableHttpPostsAPI(
override suspend fun updatePostContent(
postId: PostId,
content: PostContents
post: Post
): 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
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.post.*
import com.insanusmokrassar.postssystem.core.utils.pagination.*
import com.insanusmokrassar.postssystem.core.utils.pagination.Pagination
import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationResult
import org.joda.time.DateTime
interface ReadPostsAPI {
suspend fun getPostById(id: PostId): Post?
suspend fun getPostsByContent(id: ContentId): List<Post>
suspend fun getPostsByDates(from: DateTime? = null, to: DateTime? = null): List<Post>
suspend fun getPostById(id: PostId): RegisteredPost?
suspend fun getPostsByContent(id: ContentId): List<RegisteredPost>
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
interface WritePostsAPI {
val postCreatedFlow: Flow<Post>
val postDeletedFlow: Flow<Post>
val postUpdatedFlow: Flow<Post>
val postCreatedFlow: Flow<RegisteredPost>
val postDeletedFlow: Flow<RegisteredPost>
val postUpdatedFlow: Flow<RegisteredPost>
suspend fun createPost(content: PostContents): Post?
suspend fun createPost(post: Post): RegisteredPost?
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
interface Content {
interface Content
interface RegisteredContent : Content {
val id: ContentId
}

View File

@ -1,5 +1,20 @@
package com.insanusmokrassar.postssystem.core.content
import kotlinx.serialization.Serializable
interface TextContent : Content {
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
import com.insanusmokrassar.postssystem.core.content.Content
import com.insanusmokrassar.postssystem.core.content.RegisteredContent
import kotlinx.serialization.Serializable
import org.joda.time.DateTime
typealias PostId = String
typealias PostContents = List<Content>
typealias RegisteredPostContents = List<RegisteredContent>
interface Post {
val id: PostId
val content: PostContents
}
val meta: PostMetaInfo
interface RegisteredPost : Post {
val id: PostId
override val content: RegisteredPostContents
val creationDate: DateTime
val modificationDate: DateTime
}
@Serializable
data class SimplePost(
override val id: PostId,
override val content: PostContents,
override val meta: PostMetaInfo
override val content: PostContents
) : 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
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 io.ktor.application.ApplicationCall
import io.ktor.http.ContentType
@ -15,8 +15,8 @@ import kotlinx.serialization.internal.NullableSerializer
import kotlinx.serialization.json.Json
internal val postsSerializer = ArrayListSerializer(SimplePost.serializer())
internal val paginationResultSerializer = PaginationResult.serializer(SimplePost.serializer())
internal val postsSerializer = ArrayListSerializer(SimpleRegisteredPost.serializer())
internal val paginationResultSerializer = PaginationResult.serializer(SimpleRegisteredPost.serializer())
internal val List<Post>.asSimplePostList
get() = map { post ->
@ -25,8 +25,8 @@ internal val List<Post>.asSimplePostList
internal val Post.asSimplePost
get() = when (this) {
is SimplePost -> this
else -> SimplePost(id, content, meta)
is SimpleRegisteredPost -> this
else -> SimpleRegisteredPost(id, content, meta)
}
@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.PaginationResult
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.response.respond
import io.ktor.response.respondText
import io.ktor.routing.Route
import io.ktor.routing.post
import io.ktor.serialization.serialization
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.json.Json
import org.joda.time.DateTime
@InternalSerializationApi
@ -26,7 +19,7 @@ fun Route.includePostsCoreReadModules(readPostsAPI: ReadPostsAPI) {
post(getPostByIdAddress) {
call.receiveOrNull<PostId>() ?.also { id ->
val post = readPostsAPI.getPostById(id)
call.answer(SimplePost.serializer(), post ?.asSimplePost)
call.answer(SimpleRegisteredPost.serializer(), post ?.asSimplePost)
} ?: call.answerBadRequest("Id of post")
}
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.post.*
import io.ktor.application.*
import io.ktor.features.ContentNegotiation
import io.ktor.http.ContentType
import io.ktor.http.cio.websocket.Frame
import io.ktor.request.receiveOrNull
import io.ktor.routing.Route
import io.ktor.routing.post
import io.ktor.serialization.serialization
import io.ktor.websocket.WebSockets
import io.ktor.websocket.webSocket
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
@ -24,7 +20,7 @@ private inline fun Route.createWebsocket(path: String, flow: Flow<Post>) {
webSocket("/$path") {
flow.collect {
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 ->
val post = writePostsAPI.createPost(contents)
call.answer(SimplePost.serializer(), post ?.asSimplePost)
call.answer(SimpleRegisteredPost.serializer(), post ?.asSimplePost)
} ?: call.answerBadRequest("Contents (List of Content)")
}