feat: 投稿に絵文字データを使えるように

This commit is contained in:
usbharu 2023-12-21 19:41:27 +09:00
parent cf52ebfd12
commit 0607fe2bc5
4 changed files with 53 additions and 8 deletions

View File

@ -17,7 +17,8 @@ data class Post private constructor(
val sensitive: Boolean = false, val sensitive: Boolean = false,
val apId: String = url, val apId: String = url,
val mediaIds: List<Long> = emptyList(), val mediaIds: List<Long> = emptyList(),
val delted: Boolean = false val delted: Boolean = false,
val emojiIds: List<Long> = emptyList()
) { ) {
@Component @Component
@ -35,7 +36,8 @@ data class Post private constructor(
replyId: Long? = null, replyId: Long? = null,
sensitive: Boolean = false, sensitive: Boolean = false,
apId: String = url, apId: String = url,
mediaIds: List<Long> = emptyList() mediaIds: List<Long> = emptyList(),
emojiIds: List<Long> = emptyList()
): Post { ): Post {
require(id >= 0) { "id must be greater than or equal to 0." } require(id >= 0) { "id must be greater than or equal to 0." }
@ -74,7 +76,8 @@ data class Post private constructor(
sensitive = sensitive, sensitive = sensitive,
apId = apId, apId = apId,
mediaIds = mediaIds, mediaIds = mediaIds,
delted = false delted = false,
emojiIds = emojiIds
) )
} }

View File

@ -4,6 +4,7 @@ import dev.usbharu.hideout.application.infrastructure.exposed.QueryMapper
import dev.usbharu.hideout.application.infrastructure.exposed.ResultRowMapper import dev.usbharu.hideout.application.infrastructure.exposed.ResultRowMapper
import dev.usbharu.hideout.core.domain.model.post.Post import dev.usbharu.hideout.core.domain.model.post.Post
import dev.usbharu.hideout.core.infrastructure.exposedrepository.Posts 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.PostsMedia
import org.jetbrains.exposed.sql.Query import org.jetbrains.exposed.sql.Query
import org.springframework.stereotype.Component import org.springframework.stereotype.Component
@ -15,7 +16,9 @@ class PostQueryMapper(private val postResultRowMapper: ResultRowMapper<Post>) :
.map { it.value } .map { it.value }
.map { .map {
it.first().let(postResultRowMapper::map) it.first().let(postResultRowMapper::map)
.copy(mediaIds = it.mapNotNull { resultRow -> resultRow.getOrNull(PostsMedia.mediaId) }) .copy(
mediaIds = it.mapNotNull { resultRow -> resultRow.getOrNull(PostsMedia.mediaId) },
emojiIds = it.mapNotNull { resultRow -> resultRow.getOrNull(PostsEmojis.emojiId) })
} }
} }
} }

View File

@ -41,14 +41,25 @@ class PostRepositoryImpl(
this[PostsMedia.postId] = post.id this[PostsMedia.postId] = post.id
this[PostsMedia.mediaId] = it this[PostsMedia.mediaId] = it
} }
PostsEmojis.batchInsert(post.emojiIds) {
this[PostsEmojis.postId] = post.id
this[PostsEmojis.emojiId] = it
}
} else { } else {
PostsMedia.deleteWhere { PostsMedia.deleteWhere {
postId eq post.id postId eq post.id
} }
PostsEmojis.deleteWhere {
postId eq post.id
}
PostsMedia.batchInsert(post.mediaIds) { PostsMedia.batchInsert(post.mediaIds) {
this[PostsMedia.postId] = post.id this[PostsMedia.postId] = post.id
this[PostsMedia.mediaId] = it this[PostsMedia.mediaId] = it
} }
PostsEmojis.batchInsert(post.emojiIds) {
this[PostsEmojis.postId] = post.id
this[PostsEmojis.emojiId] = it
}
Posts.update({ Posts.id eq post.id }) { Posts.update({ Posts.id eq post.id }) {
it[actorId] = post.actorId it[actorId] = post.actorId
it[overview] = post.overview it[overview] = post.overview
@ -67,21 +78,27 @@ class PostRepositoryImpl(
} }
override suspend fun findById(id: Long): Post? = query { override suspend fun findById(id: Long): Post? = query {
return@query Posts.leftJoin(PostsMedia) return@query Posts
.leftJoin(PostsMedia)
.leftJoin(PostsEmojis)
.select { Posts.id eq id } .select { Posts.id eq id }
.let(postQueryMapper::map) .let(postQueryMapper::map)
.singleOrNull() .singleOrNull()
} }
override suspend fun findByUrl(url: String): Post? = query { override suspend fun findByUrl(url: String): Post? = query {
return@query Posts.leftJoin(PostsMedia) return@query Posts
.leftJoin(PostsMedia)
.leftJoin(PostsEmojis)
.select { Posts.url eq url } .select { Posts.url eq url }
.let(postQueryMapper::map) .let(postQueryMapper::map)
.singleOrNull() .singleOrNull()
} }
override suspend fun findByApId(apId: String): Post? = query { override suspend fun findByApId(apId: String): Post? = query {
return@query Posts.leftJoin(PostsMedia) return@query Posts
.leftJoin(PostsMedia)
.leftJoin(PostsEmojis)
.select { Posts.apId eq apId } .select { Posts.apId eq apId }
.let(postQueryMapper::map) .let(postQueryMapper::map)
.singleOrNull() .singleOrNull()
@ -92,7 +109,10 @@ class PostRepositoryImpl(
} }
override suspend fun findByActorId(actorId: Long): List<Post> = query { override suspend fun findByActorId(actorId: Long): List<Post> = query {
return@query Posts.select { Posts.actorId eq actorId }.let(postQueryMapper::map) return@query Posts
.leftJoin(PostsMedia)
.leftJoin(PostsEmojis)
.select { Posts.actorId eq actorId }.let(postQueryMapper::map)
} }
override suspend fun delete(id: Long): Unit = query { override suspend fun delete(id: Long): Unit = query {
@ -125,3 +145,9 @@ object PostsMedia : Table("posts_media") {
val mediaId = long("media_id").references(Media.id, ReferenceOption.CASCADE, ReferenceOption.CASCADE) val mediaId = long("media_id").references(Media.id, ReferenceOption.CASCADE, ReferenceOption.CASCADE)
override val primaryKey = PrimaryKey(postId, mediaId) override val primaryKey = PrimaryKey(postId, mediaId)
} }
object PostsEmojis : Table("posts_emojis") {
val postId = long("post_id").references(Posts.id)
val emojiId = long("emoji_id").references(Posts.id)
override val primaryKey: PrimaryKey = PrimaryKey(postId, emojiId)
}

View File

@ -117,6 +117,19 @@ alter table posts_media
add constraint fk_posts_media_post_id__id foreign key (post_id) references posts (id) on delete cascade on update cascade; add constraint fk_posts_media_post_id__id foreign key (post_id) references posts (id) on delete cascade on update cascade;
alter table posts_media alter table posts_media
add constraint fk_posts_media_media_id__id foreign key (media_id) references media (id) on delete cascade on update cascade; add constraint fk_posts_media_media_id__id foreign key (media_id) references media (id) on delete cascade on update cascade;
create table if not exists posts_emojis
(
post_id bigint not null,
emoji_id bigint not null,
constraint pk_postsemoji primary key (post_id, emoji_id)
);
alter table posts_emojis
add constraint fk_posts_emojis_post_id__id foreign key (post_id) references posts (id) on delete cascade on update cascade;
alter table posts_emojis
add constraint fk_posts_emojis_emoji_id__id foreign key (emoji_id) references emojis (id) on delete cascade on update cascade;
create table if not exists reactions create table if not exists reactions
( (
id bigserial primary key, id bigserial primary key,