Skip to content

Commit

Permalink
✨feature: 피드 작성자 정보를 보내주도록 수정
Browse files Browse the repository at this point in the history
feature/feed-user-info
  • Loading branch information
daehyeon authored and kohmlab committed Mar 28, 2023
1 parent 8a51396 commit c650db4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class CustomFeedRepositoryImpl(
val feedMapper: (t: MutableList<MutableMap<String, Any>>) -> 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
Expand Down Expand Up @@ -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<Feed> {
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)
Expand All @@ -84,13 +88,15 @@ class CustomFeedRepositoryImpl(

override fun findByIdWithContentAndClap(feedId: Long): Mono<Feed> {
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)
Expand All @@ -103,28 +109,32 @@ class CustomFeedRepositoryImpl(

override fun findHomeByUserIdWithContentAndClap(userId: String): Flux<Feed> {
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContentDto>,
val user: UserForFeedDto,
): Serializable
Original file line number Diff line number Diff line change
@@ -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
12 changes: 10 additions & 2 deletions src/main/kotlin/com/nexters/pimo/feed/domain/Feed.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Content> = listOf(),

Expand All @@ -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() }
)
}
}

0 comments on commit c650db4

Please sign in to comment.