From bd747718d48e203a8ca58d8581cbc0e0d72dd36a Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Tue, 4 Jun 2024 22:46:48 +0900 Subject: [PATCH] =?UTF-8?q?QueryMapper=E3=81=A8ResultRowMapper=E3=82=92?= =?UTF-8?q?=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/domain/model/post/PostContent.kt | 11 +--- .../infrastructure/exposed/PostQueryMapper.kt | 65 +++++++++++++++++++ .../exposed/PostResultRowMapper.kt | 48 ++++++++++++++ .../ExposedPostRepository.kt | 6 +- .../factory/PostContentFactoryImpl.kt | 4 +- 5 files changed, 121 insertions(+), 13 deletions(-) create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposed/PostQueryMapper.kt create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposed/PostResultRowMapper.kt diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/PostContent.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/PostContent.kt index 2471ba2a..38e4052d 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/PostContent.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/PostContent.kt @@ -18,7 +18,7 @@ package dev.usbharu.hideout.core.domain.model.post import dev.usbharu.hideout.core.domain.model.emoji.EmojiId -class PostContent private constructor(val text: String, val content: String, val emojiIds: List) { +data class PostContent(val text: String, val content: String, val emojiIds: List) { companion object { val empty = PostContent("", "", emptyList()) @@ -26,13 +26,4 @@ class PostContent private constructor(val text: String, val content: String, val val textLength = 3000 } - abstract class PostContentFactory { - protected suspend fun create(text: String, content: String, emojiIds: List): PostContent { - return PostContent( - text, - content, - emojiIds - ) - } - } } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposed/PostQueryMapper.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposed/PostQueryMapper.kt new file mode 100644 index 00000000..c0b600a4 --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposed/PostQueryMapper.kt @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2024 usbharu + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.usbharu.hideout.core.infrastructure.exposed + +import dev.usbharu.hideout.core.domain.model.actor.ActorId +import dev.usbharu.hideout.core.domain.model.emoji.EmojiId +import dev.usbharu.hideout.core.domain.model.media.MediaId +import dev.usbharu.hideout.core.domain.model.post.Post +import dev.usbharu.hideout.core.infrastructure.exposedrepository.Posts +import dev.usbharu.hideout.core.infrastructure.exposedrepository.PostsEmojis +import dev.usbharu.hideout.core.infrastructure.exposedrepository.PostsMedia +import dev.usbharu.hideout.core.infrastructure.exposedrepository.PostsVisibleActors +import org.jetbrains.exposed.sql.Query +import org.jetbrains.exposed.sql.ResultRow +import org.springframework.stereotype.Component + +@Component +class PostQueryMapper(private val postResultRowMapper: ResultRowMapper) : QueryMapper { + override fun map(query: Query): List { + return query + .groupBy { it[Posts.id] } + .map { it.value } + .map { + it + .first() + .let(postResultRowMapper::map) + .apply { + addMediaIds( + it.mapNotNull { resultRow: ResultRow -> + resultRow + .getOrNull(PostsMedia.mediaId) + ?.let { mediaId -> MediaId(mediaId) } + } + ) + content = content.copy(emojiIds = it + .mapNotNull { resultRow: ResultRow -> + resultRow + .getOrNull(PostsEmojis.emojiId) + ?.let { emojiId -> EmojiId(emojiId) } + } + ) + visibleActors = it.mapNotNull { resultRow: ResultRow -> + resultRow + .getOrNull(PostsVisibleActors.actorId) + ?.let { actorId -> ActorId(actorId) } + } + clearDomainEvents() + } + } + } +} \ No newline at end of file diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposed/PostResultRowMapper.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposed/PostResultRowMapper.kt new file mode 100644 index 00000000..f2a5667a --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposed/PostResultRowMapper.kt @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2024 usbharu + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.usbharu.hideout.core.infrastructure.exposed + +import dev.usbharu.hideout.core.domain.model.actor.ActorId +import dev.usbharu.hideout.core.domain.model.post.* +import dev.usbharu.hideout.core.infrastructure.exposedrepository.Posts +import org.jetbrains.exposed.sql.ResultRow +import org.springframework.stereotype.Component +import java.net.URI + +@Component +class PostResultRowMapper : ResultRowMapper { + override fun map(resultRow: ResultRow): Post { + return Post( + id = PostId(resultRow[Posts.id]), + actorId = ActorId(resultRow[Posts.actorId]), + overview = resultRow[Posts.overview]?.let { PostOverview(it) }, + content = PostContent(resultRow[Posts.text], resultRow[Posts.content], emptyList()), + createdAt = resultRow[Posts.createdAt], + visibility = Visibility.valueOf(resultRow[Posts.visibility]), + url = URI.create(resultRow[Posts.url]), + repostId = resultRow[Posts.repostId]?.let { PostId(it) }, + replyId = resultRow[Posts.replyId]?.let { PostId(it) }, + sensitive = resultRow[Posts.sensitive], + apId = URI.create(resultRow[Posts.apId]), + deleted = resultRow[Posts.deleted], + mediaIds = emptyList(), + visibleActors = emptyList(), + hide = resultRow[Posts.hide], + moveTo = resultRow[Posts.moveTo]?.let { PostId(it) } + ) + } +} \ No newline at end of file diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedPostRepository.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedPostRepository.kt index 8115fc25..922a605f 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedPostRepository.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedPostRepository.kt @@ -145,7 +145,11 @@ class ExposedPostRepository(override val domainEventPublisher: DomainEventPublis } override suspend fun findById(id: PostId): Post? { - TODO("Not yet implemented") + query { + Posts.selectAll().where { + Posts.id eq id.id + } + } } override suspend fun findByActorId(id: ActorId): List { diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/factory/PostContentFactoryImpl.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/factory/PostContentFactoryImpl.kt index 7a18fa29..a59af46c 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/factory/PostContentFactoryImpl.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/factory/PostContentFactoryImpl.kt @@ -23,10 +23,10 @@ import org.springframework.stereotype.Component @Component class PostContentFactoryImpl( private val postContentFormatter: PostContentFormatter, -) : PostContent.PostContentFactory() { +) { suspend fun create(content: String): PostContent { val format = postContentFormatter.format(content) - return super.create( + return PostContent( format.content, format.html, emptyList()