QueryMapperとResultRowMapperを作成

This commit is contained in:
usbharu 2024-06-04 22:46:48 +09:00
parent 21d0d7af69
commit bd747718d4
5 changed files with 121 additions and 13 deletions

View File

@ -18,7 +18,7 @@ package dev.usbharu.hideout.core.domain.model.post
import dev.usbharu.hideout.core.domain.model.emoji.EmojiId import dev.usbharu.hideout.core.domain.model.emoji.EmojiId
class PostContent private constructor(val text: String, val content: String, val emojiIds: List<EmojiId>) { data class PostContent(val text: String, val content: String, val emojiIds: List<EmojiId>) {
companion object { companion object {
val empty = PostContent("", "", emptyList()) val empty = PostContent("", "", emptyList())
@ -26,13 +26,4 @@ class PostContent private constructor(val text: String, val content: String, val
val textLength = 3000 val textLength = 3000
} }
abstract class PostContentFactory {
protected suspend fun create(text: String, content: String, emojiIds: List<EmojiId>): PostContent {
return PostContent(
text,
content,
emojiIds
)
}
}
} }

View File

@ -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<Post>) : QueryMapper<Post> {
override fun map(query: Query): List<Post> {
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()
}
}
}
}

View File

@ -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<Post> {
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) }
)
}
}

View File

@ -145,7 +145,11 @@ class ExposedPostRepository(override val domainEventPublisher: DomainEventPublis
} }
override suspend fun findById(id: PostId): Post? { 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<Post> { override suspend fun findByActorId(id: ActorId): List<Post> {

View File

@ -23,10 +23,10 @@ import org.springframework.stereotype.Component
@Component @Component
class PostContentFactoryImpl( class PostContentFactoryImpl(
private val postContentFormatter: PostContentFormatter, private val postContentFormatter: PostContentFormatter,
) : PostContent.PostContentFactory() { ) {
suspend fun create(content: String): PostContent { suspend fun create(content: String): PostContent {
val format = postContentFormatter.format(content) val format = postContentFormatter.format(content)
return super.create( return PostContent(
format.content, format.content,
format.html, format.html,
emptyList() emptyList()