Skip to content

Commit

Permalink
imp: 친구 이름으로 검색 진행
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Sep 2, 2024
1 parent 4ceb194 commit bdcad75
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,64 +236,44 @@ class EnvelopeFacade(
request: SearchEnvelopeRequest,
pageRequest: SusuPageRequest,
): Page<SearchEnvelopeResponse> {
val searchSpec = SearchEnvelopeSpec(
val response = envelopeService.search(resolveSearchSpec(user, request), pageRequest.toDefault())

val envelopeIds = response.content.mapNotNull { envelope -> envelope.envelope.id }

val categoryAssignments = categoryAssignmentService.findAllByTypeAndIdIn(
targetType = CategoryAssignmentType.ENVELOPE,
targetIds = envelopeIds
).associateBy { categoryAssignment -> categoryAssignment.targetId }

return response.map { (envelope, friend, friendRelationship) ->
val category = categoryAssignments[envelope.id]?.let { categoryAssignment ->
val category = categoryService.getCategory(categoryAssignment.categoryId)
CategoryWithCustomModel.of(category, categoryAssignment)
}
val relationship = friendRelationship?.relationshipId
?.let { relationshipId -> relationshipService.getRelationship(relationshipId) }

SearchEnvelopeResponse.of(
envelope = envelope,
category = category,
friend = friend,
relationship = relationship,
friendRelationship = friendRelationship,
include = request.include
)
}
}

private fun resolveSearchSpec(user: AuthUser, request: SearchEnvelopeRequest): SearchEnvelopeSpec {
return SearchEnvelopeSpec(
uid = user.uid,
friendId = request.friendIds,
friendName = request.friendName,
ledgerId = request.ledgerId,
types = request.types,
fromAmount = request.fromAmount,
toAmount = request.toAmount
)
val pageable = pageRequest.toDefault()

val response = envelopeService.search(searchSpec, pageable)
val friendIds = response.content.map { envelope -> envelope.friendId }
val envelopeIds = response.content.map { envelope -> envelope.id }

return parZipWithMDC(
{
when (request.includeFriend) {
true -> friendService.findAllByIdIn(friendIds)
false -> emptyList()
}.associateBy { friend -> friend.id }
},
{
when (request.includeRelationship || request.includeFriendRelationship) {
true -> friendRelationshipService.findAllByFriendIds(friendIds)
false -> emptyList()
}.associateBy { friendRelationShip -> friendRelationShip.friendId }
},
{
when (request.includeCategory) {
true -> categoryAssignmentService.findAllByTypeAndIdIn(CategoryAssignmentType.ENVELOPE, envelopeIds)
false -> emptyList()
}.associateBy { categoryAssignment -> categoryAssignment.targetId }
}
) { friends, friendRelationships, categoryAssignments ->
response.map { envelope ->
val category = categoryAssignments[envelope.id]?.let { categoryAssignment ->
val category = categoryService.getCategory(categoryAssignment.categoryId)
CategoryWithCustomModel.of(category, categoryAssignment)
}
val relationship = friendRelationships[envelope.friendId]?.let { friendRelationship ->
relationshipService.getRelationship(friendRelationship.relationshipId)
}
val friend = friends[envelope.friendId]?.let { friend ->
FriendModel.from(friend)
}
val friendRelationship = friendRelationships[envelope.friendId]?.let { friendRelationship ->
FriendRelationshipModel.from(friendRelationship)
}

SearchEnvelopeResponse(
envelope = EnvelopeModel.from(envelope),
category = category,
friend = friend,
relationship = relationship,
friendRelationship = friendRelationship
)
}
}
}

suspend fun searchFriendStatistics(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class EnvelopeService(
return withMDCContext(Dispatchers.IO) { envelopeRepository.countPerCategoryIdByUid(uid) }
}

suspend fun search(spec: SearchEnvelopeSpec, pageable: Pageable): Page<Envelope> {
suspend fun search(spec: SearchEnvelopeSpec, pageable: Pageable): Page<SearchEnvelopeModel> {
return withMDCContext(Dispatchers.IO) { envelopeRepository.search(spec, pageable) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.oksusu.susu.domain.envelope.domain.vo.EnvelopeType
data class SearchEnvelopeRequest(
/** 지인 id */
val friendIds: Set<Long>?,
/** 지인 이름 */
val friendName: String?,
/** 장부 id */
val ledgerId: Long?,
/** type: SENT, RECEIVED */
Expand All @@ -23,16 +25,4 @@ data class SearchEnvelopeRequest(
FRIEND_RELATIONSHIP,
;
}

val includeCategory: Boolean
get() = include?.contains(IncludeSpec.CATEGORY) == true

val includeFriend: Boolean
get() = include?.contains(IncludeSpec.FRIEND) == true

val includeRelationship: Boolean
get() = include?.contains(IncludeSpec.RELATIONSHIP) == true

val includeFriendRelationship: Boolean
get() = include?.contains(IncludeSpec.FRIEND_RELATIONSHIP) == true
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package com.oksusu.susu.api.envelope.model.response

import com.oksusu.susu.api.category.model.CategoryWithCustomModel
import com.oksusu.susu.api.envelope.model.EnvelopeModel
import com.oksusu.susu.api.envelope.model.request.SearchEnvelopeRequest.IncludeSpec
import com.oksusu.susu.api.friend.model.FriendModel
import com.oksusu.susu.api.friend.model.FriendRelationshipModel
import com.oksusu.susu.api.friend.model.RelationshipModel
import com.oksusu.susu.domain.envelope.domain.Envelope
import com.oksusu.susu.domain.friend.domain.Friend
import com.oksusu.susu.domain.friend.domain.FriendRelationship

data class SearchEnvelopeResponse(
/** 봉투 */
Expand All @@ -17,4 +21,27 @@ data class SearchEnvelopeResponse(
val relationship: RelationshipModel? = null,
/** 지인 관계 정보 */
val friendRelationship: FriendRelationshipModel? = null,
)
) {
companion object {
fun of(
envelope: Envelope,
category: CategoryWithCustomModel?,
friend: Friend?,
relationship: RelationshipModel?,
friendRelationship: FriendRelationship?,
include: Set<IncludeSpec>?,
) = SearchEnvelopeResponse(
envelope = EnvelopeModel.from(envelope),
category = category
?.takeIf { include?.contains(IncludeSpec.CATEGORY) == true },
friend = friend
?.let { FriendModel.from(it) }
?.takeIf { include?.contains(IncludeSpec.FRIEND) == true },
relationship = relationship
?.takeIf { include?.contains(IncludeSpec.RELATIONSHIP) == true },
friendRelationship = friendRelationship
?.let { FriendRelationshipModel.from(it) }
?.takeIf { include?.contains(IncludeSpec.FRIEND_RELATIONSHIP) == true }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class EnvelopeResource(
/**
* **검색조건**
* - types: SENT: 보낸 봉투만, RECEIVED: 받은 봉투만, 그외 케이스는 전체 봉투 정보
* - friendName: 친구 이름으로 검색, ex) %동건%
*
* **정렬조건**
* - createdAt: 생성일
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ interface EnvelopeQRepository {
targetDate: LocalDateTime,
): List<CountAvgAmountPerStatisticGroupModel>

fun search(spec: SearchEnvelopeSpec, pageable: Pageable): Page<Envelope>
fun search(spec: SearchEnvelopeSpec, pageable: Pageable): Page<SearchEnvelopeModel>

fun findFriendStatistics(spec: SearchFriendStatisticsSpec, pageable: Pageable): Page<FriendStatisticsModel>

Expand Down Expand Up @@ -342,7 +342,10 @@ class EnvelopeQRepositoryImpl : EnvelopeQRepository, QuerydslRepositorySupport(E
).from(qEnvelope)
.join(qUser).on(qEnvelope.uid.eq(qUser.id))
.join(qFriendRelationship).on(qEnvelope.friendId.eq(qFriendRelationship.friendId))
.join(qCategoryAssignment).on(qEnvelope.id.eq(qCategoryAssignment.targetId).and(qCategoryAssignment.targetType.eq(CategoryAssignmentType.ENVELOPE)))
.join(qCategoryAssignment).on(
qEnvelope.id.eq(qCategoryAssignment.targetId)
.and(qCategoryAssignment.targetType.eq(CategoryAssignmentType.ENVELOPE))
)
.where(
qEnvelope.amount.between(minAmount, maxAmount),
qEnvelope.uid.notIn(uid),
Expand All @@ -355,20 +358,23 @@ class EnvelopeQRepositoryImpl : EnvelopeQRepository, QuerydslRepositorySupport(E
).fetch()
}

override fun search(spec: SearchEnvelopeSpec, pageable: Pageable): Page<Envelope> {
override fun search(spec: SearchEnvelopeSpec, pageable: Pageable): Page<SearchEnvelopeModel> {
/** select */
val query = JPAQuery<Envelope>(entityManager)
.select(qEnvelope)
val query = JPAQuery<QEnvelope>(entityManager)
.select(QSearchEnvelopeModel(qEnvelope, qFriend, qFriendRelationship))
.from(qEnvelope)
.join(qFriend).on(qEnvelope.friendId.eq(qFriend.id))
.join(qFriendRelationship).on(qEnvelope.friendId.eq(qFriendRelationship.friendId))

/** where */
query.where(
qEnvelope.uid.eq(spec.uid),
qEnvelope.friendId.isIn(spec.friendId),
qFriend.name.isContains(spec.friendName),
qEnvelope.ledgerId.isEquals(spec.ledgerId),
qEnvelope.type.isIn(spec.types),
qEnvelope.amount.isGoe(spec.fromAmount),
qEnvelope.amount.isLoe(spec.toAmount)
qEnvelope.amount.isLoe(spec.toAmount),
)

return querydsl.execute(query, pageable)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.oksusu.susu.domain.envelope.infrastructure.model

import com.oksusu.susu.domain.category.domain.CategoryAssignment
import com.oksusu.susu.domain.envelope.domain.Envelope
import com.oksusu.susu.domain.friend.domain.Friend
import com.oksusu.susu.domain.friend.domain.FriendRelationship
Expand All @@ -10,5 +9,4 @@ data class SearchEnvelopeModel @QueryProjection constructor(
val envelope: Envelope,
val friend: Friend?,
val friendRelationship: FriendRelationship?,
val categoryAssignment: CategoryAssignment?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import com.oksusu.susu.domain.envelope.domain.vo.EnvelopeType
data class SearchEnvelopeSpec(
val uid: Long,
val friendId: Set<Long>?,
val friendName: String?,
val ledgerId: Long?,
val types: Set<EnvelopeType>?,
val fromAmount: Long?,
val toAmount: Long?,
)
) {
companion object
}

0 comments on commit bdcad75

Please sign in to comment.