diff --git a/src/main/kotlin/com/nexters/pimo/feed/adapter/out/persistence/CustomFeedRepository.kt b/src/main/kotlin/com/nexters/pimo/feed/adapter/out/persistence/CustomFeedRepository.kt index c47fc95..e7f0fe1 100644 --- a/src/main/kotlin/com/nexters/pimo/feed/adapter/out/persistence/CustomFeedRepository.kt +++ b/src/main/kotlin/com/nexters/pimo/feed/adapter/out/persistence/CustomFeedRepository.kt @@ -29,6 +29,8 @@ class CustomFeedRepositoryImpl( val feedMapper: (t: MutableList>) -> Feed = { list -> val feedId = list[0]["id"] as Long val userId = list[0]["userId"] as String + val userNickName = list[0]["nickName"] as String + val userProfileImgUrl = list[0]["profileImgUrl"] as String val contents = list.stream().map { try { val id = it["contentId"] as Long @@ -61,20 +63,22 @@ class CustomFeedRepositoryImpl( null } }.collect(Collectors.toList()).filterNotNull().distinctBy{ it.id } - Feed(id = feedId, userId = userId, contents = contents, claps = claps) + Feed(id = feedId, userId = userId, contents = contents, claps = claps, userNickName = userNickName, userProfileImgUrl = userProfileImgUrl) } return feedMapper } override fun findAllByUserIdWithContentAndClap(userId: String): Flux { val query = "SELECT" + - " f.id, f.userId, f.status, f.createdAt,\n" + + " F.id, F.userId, F.status, F.createdAt,\n" + " CT.id AS contentId, CT.feedId, CT.caption, CT.url, CT.status AS contentStatus,\n" + - " CL.id AS clapId, CL.userId AS clapUserId\n" + - " FROM FeedTB f\n" + + " CL.id AS clapId, CL.userId AS clapUserId,\n" + + " IFNULL(US.nickName, '이름 없음') AS nickName, IFNULL(US.profileImgUrl, '') AS profileImgUrl\n" + + " FROM FeedTB F\n" + " JOIN ContentTB AS CT ON F.id = CT.feedId AND CT.status = '0'\n" + " LEFT JOIN ClapTB AS CL ON F.id = CL.feedId\n" + - " WHERE f.status = '0' AND f.userId = :userId\n" + - " ORDER BY f.createdAt DESC, f.id DESC;" + " LEFT JOIN UserTB AS US ON F.userId = US.userId AND US.status = '0'\n" + + " WHERE F.status = '0' AND F.userId = :userId\n" + + " ORDER BY F.createdAt DESC, F.id DESC;" return client.sql(query) .bind("userId", userId) @@ -84,13 +88,15 @@ class CustomFeedRepositoryImpl( override fun findByIdWithContentAndClap(feedId: Long): Mono { val query = "SELECT\n" + - " f.id, f.userId, f.status, f.createdAt,\n" + + " F.id, F.userId, F.status, F.createdAt,\n" + " CT.id AS contentId, CT.feedId, CT.caption, CT.url, CT.status AS contentStatus,\n" + - " CL.id AS clapId, CL.userId AS clapUserId\n" + - " FROM FeedTB f\n" + + " CL.id AS clapId, CL.userId AS clapUserId,\n" + + " IFNULL(US.nickName, '이름 없음') AS nickName, IFNULL(US.profileImgUrl, '') AS profileImgUrl\n" + + " FROM FeedTB F\n" + " JOIN ContentTB AS CT ON F.id = CT.feedId AND CT.status = '0'\n" + " LEFT JOIN ClapTB CL ON F.id = CL.feedId\n" + - " WHERE f.id = :feedId;" + " LEFT JOIN UserTB AS US ON F.userId = US.userId AND US.status = '0'\n" + + " WHERE F.id = :feedId;" return client.sql(query) .bind("feedId", feedId) @@ -103,28 +109,32 @@ class CustomFeedRepositoryImpl( override fun findHomeByUserIdWithContentAndClap(userId: String): Flux { val query = """ - SELECT T.id, T.userId, T.status, T.createdAt, T.contentId, T.feedId, T.caption, T.url, T.contentStatus, T.clapId, T.clapUserId + SELECT T.id, T.userId, T.status, T.createdAt, T.contentId, T.feedId, T.caption, T.url, T.contentStatus, T.clapId, T.clapUserId, T.nickName, T.profileImgUrl FROM ( SELECT - f.id, f.userId, f.status, f.createdAt, + F.id, F.userId, F.status, F.createdAt, CT.id AS contentId, CT.feedId, CT.caption, CT.url, CT.status as contentStatus, - CL.id AS clapId, CL.userId AS clapUserId - FROM FeedTB f - JOIN FollowTB follow ON follow.followerUserId = f.userId AND follow.followeeUserId = :userId + CL.id AS clapId, CL.userId AS clapUserId, + IFNULL(US.nickName, '이름 없음') AS nickName, IFNULL(US.profileImgUrl, '') AS profileImgUrl + FROM FeedTB F + JOIN FollowTB follow ON follow.followerUserId = F.userId AND follow.followeeUserId = :userId JOIN ContentTB AS CT ON F.id = CT.feedId AND CT.status = '0' LEFT JOIN ClapTB CL ON F.id = CL.feedId - WHERE f.status = '0' + LEFT JOIN UserTB AS US ON F.userId = US.userId AND US.status = '0' + WHERE F.status = '0' UNION ALL SELECT - f.id, f.userId, f.status, f.createdAt, + F.id, F.userId, F.status, F.createdAt, CT.id AS contentId, CT.feedId, CT.caption, CT.url, CT.status AS contentStatus, - CL.id AS clapId, CL.userId AS clapUserId - FROM FeedTB f + CL.id AS clapId, CL.userId AS clapUserId, + IFNULL(US.nickName, '이름 없음') AS nickName, IFNULL(US.profileImgUrl, '') AS profileImgUrl + FROM FeedTB F JOIN ContentTB AS CT ON F.id = CT.feedId AND CT.status = '0' LEFT JOIN ClapTB AS CL ON F.id = CL.feedId - WHERE f.status = '0' AND f.userId = :userId + LEFT JOIN UserTB AS US ON F.userId = US.userId AND US.status = '0' + WHERE F.status = '0' AND F.userId = :userId ) T ORDER BY T.createdAt DESC, T.id DESC; """.trimIndent() diff --git a/src/main/kotlin/com/nexters/pimo/feed/application/dto/FeedDto.kt b/src/main/kotlin/com/nexters/pimo/feed/application/dto/FeedDto.kt index 4592e37..33b8b24 100644 --- a/src/main/kotlin/com/nexters/pimo/feed/application/dto/FeedDto.kt +++ b/src/main/kotlin/com/nexters/pimo/feed/application/dto/FeedDto.kt @@ -4,10 +4,10 @@ import java.io.Serializable data class FeedDto( val id: Long, - val userId: String, val status: String, val createdAt: String, val clapCount: Int, val clapped: Boolean, val contents: List, + val user: UserForFeedDto, ): Serializable diff --git a/src/main/kotlin/com/nexters/pimo/feed/application/dto/UserForFeedDto.kt b/src/main/kotlin/com/nexters/pimo/feed/application/dto/UserForFeedDto.kt new file mode 100644 index 0000000..ac00cad --- /dev/null +++ b/src/main/kotlin/com/nexters/pimo/feed/application/dto/UserForFeedDto.kt @@ -0,0 +1,9 @@ +package com.nexters.pimo.feed.application.dto + +import java.io.Serializable + +data class UserForFeedDto( + val userId: String, + val nickName: String, + val profileImgUrl: String, +): Serializable diff --git a/src/main/kotlin/com/nexters/pimo/feed/domain/Feed.kt b/src/main/kotlin/com/nexters/pimo/feed/domain/Feed.kt index 73fcaa6..29b0a55 100644 --- a/src/main/kotlin/com/nexters/pimo/feed/domain/Feed.kt +++ b/src/main/kotlin/com/nexters/pimo/feed/domain/Feed.kt @@ -1,6 +1,7 @@ package com.nexters.pimo.feed.domain import com.nexters.pimo.feed.application.dto.FeedDto +import com.nexters.pimo.feed.application.dto.UserForFeedDto import jakarta.persistence.* import org.springframework.data.annotation.Id import org.springframework.data.relational.core.mapping.Column @@ -27,6 +28,12 @@ data class Feed( @Column("updatedAt") val updatedAt: LocalDateTime = LocalDateTime.now(), + @Transient + var userNickName: String = "", + + @Transient + var userProfileImgUrl: String = "", + @Transient var contents: List = listOf(), @@ -35,11 +42,12 @@ data class Feed( ) { fun toDto(userId: String? = null) = FeedDto( id = this.id, - userId = this.userId, + user = UserForFeedDto(this.userId, this.userNickName, this.userProfileImgUrl), status = this.status, createdAt = this.createdAt.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")), clapCount = this.claps.count(), clapped = this.claps.any { it.userId == userId }, contents = this.contents.map { it.toDto() } ) -} \ No newline at end of file +} +