Skip to content

Commit

Permalink
[FO-916] 프로필 my-registrations에 delete 된 프로필 조회되는 문제 해결 (#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
DoodlesOnMyFood authored Feb 12, 2024
1 parent f2d0040 commit 01b029f
Showing 1 changed file with 112 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,50 +34,52 @@ class ProfileRepositoryImpl(
private val sessionFactory: Mutiny.SessionFactory,
private val queryFactory: SpringDataHibernateMutinyReactiveQueryFactory,
) : ProfileRepository {

override suspend fun findAllByFilters(
pageable: Pageable,
request: RetrieveProfilesRequest,
): Page<Profile> {
val domainProfileIds = if (request.domains.isEmpty()) {
emptyList()
} else {
queryFactory.listQuery {
select(col(ProfileDomain::profileId))
from(entity(ProfileDomain::class))
where(col(ProfileDomain::type).`in`(request.domains))
val domainProfileIds =
if (request.domains.isEmpty()) {
emptyList()
} else {
queryFactory.listQuery {
select(col(ProfileDomain::profileId))
from(entity(ProfileDomain::class))
where(col(ProfileDomain::type).`in`(request.domains))
}
}
}

val categoryProfileIds = if (request.categories.isEmpty()) {
emptyList()
} else {
queryFactory.listQuery {
select(col(ProfileCategory::profileId))
from(entity(ProfileCategory::class))
where(col(ProfileCategory::type).`in`(request.categories))
val categoryProfileIds =
if (request.categories.isEmpty()) {
emptyList()
} else {
queryFactory.listQuery {
select(col(ProfileCategory::profileId))
from(entity(ProfileCategory::class))
where(col(ProfileCategory::type).`in`(request.categories))
}
}
}

val ids = queryFactory.pageQuery(pageable) {
select(column(Profile::id))
from(entity(Profile::class))
where(
and(
col(Profile::type).equal(request.type),
col(Profile::gender).`in`(request.genders),
col(Profile::birthday).lessThanOrEqualTo(
DateTimeFormat.calculdateLocalDate(request.ageMin)
),
col(Profile::birthday).greaterThanOrEqualTo(
DateTimeFormat.calculdateLocalDate(request.ageMax)
),
if (request.domains.isNotEmpty()) col(Profile::id).`in`(domainProfileIds) else null,
if (request.categories.isNotEmpty()) col(Profile::id).`in`(categoryProfileIds) else null,
col(Profile::isDeleted).equal(false)
val ids =
queryFactory.pageQuery(pageable) {
select(column(Profile::id))
from(entity(Profile::class))
where(
and(
col(Profile::type).equal(request.type),
col(Profile::gender).`in`(request.genders),
col(Profile::birthday).lessThanOrEqualTo(
DateTimeFormat.calculdateLocalDate(request.ageMin)
),
col(Profile::birthday).greaterThanOrEqualTo(
DateTimeFormat.calculdateLocalDate(request.ageMax)
),
if (request.domains.isNotEmpty()) col(Profile::id).`in`(domainProfileIds) else null,
if (request.categories.isNotEmpty()) col(Profile::id).`in`(categoryProfileIds) else null,
col(Profile::isDeleted).equal(false)
)
)
)
}
}

if (ids.content.isEmpty()) {
return PageImpl(
Expand All @@ -87,20 +89,22 @@ class ProfileRepositoryImpl(
)
}

val profiles = queryFactory.listQuery {
select(entity(Profile::class))
from(entity(Profile::class))
where(and(col(Profile::id).`in`(ids.content)))
orderBy(orderSpec(pageable.sort))
}

val uniqueProfiles = profiles.groupBy { it?.id }
.map { it.value.first() }
.onEach {
it!!.snsUrls = emptySet()
it.profileImages = mutableListOf()
val profiles =
queryFactory.listQuery {
select(entity(Profile::class))
from(entity(Profile::class))
where(and(col(Profile::id).`in`(ids.content)))
orderBy(orderSpec(pageable.sort))
}

val uniqueProfiles =
profiles.groupBy { it?.id }
.map { it.value.first() }
.onEach {
it!!.snsUrls = emptySet()
it.profileImages = mutableListOf()
}

return PageImpl(
uniqueProfiles,
pageable,
Expand All @@ -118,7 +122,10 @@ class ProfileRepositoryImpl(
}
}

override suspend fun findByTypeAndId(type: Type?, profileId: Long?): Profile? {
override suspend fun findByTypeAndId(
type: Type?,
profileId: Long?,
): Profile? {
return queryFactory.singleQueryOrNull {
select(entity(Profile::class))
from(entity(Profile::class))
Expand All @@ -132,23 +139,26 @@ class ProfileRepositoryImpl(
pageable: Pageable,
userId: Long,
): Page<Profile> {
val ids = queryFactory.pageQuery(pageable) {
select(column(Profile::id))
from(entity(Profile::class))
where(col(Profile::userId).equal(userId))
}

val profiles = queryFactory.listQuery {
select(entity(Profile::class))
from(entity(Profile::class))
fetch(Profile::profileImages, joinType = JoinType.LEFT)
fetch(Profile::snsUrls, joinType = JoinType.LEFT)
where(
and(
col(Profile::id).`in`(ids.content)
val ids =
queryFactory.pageQuery(pageable) {
select(column(Profile::id))
from(entity(Profile::class))
where(
and(
col(Profile::userId).equal(userId),
col(Profile::isDeleted).equal(false)
)
)
)
}.associateBy { it?.id }
}

val profiles =
queryFactory.listQuery {
select(entity(Profile::class))
from(entity(Profile::class))
fetch(Profile::profileImages, joinType = JoinType.LEFT)
fetch(Profile::snsUrls, joinType = JoinType.LEFT)
where(col(Profile::id).`in`(ids.content))
}.associateBy { it?.id }

return ids.map { profiles[it] }
}
Expand All @@ -159,25 +169,28 @@ class ProfileRepositoryImpl(
type: Type?,
): Page<Profile> {
return queryFactory.withFactory { factory ->
val ids = factory.pageQuery(pageable) {
select(column(ProfileWant::profileId))
from(entity(ProfileWant::class))
join(entity(Profile::class), col(Profile::id).equal(col(ProfileWant::profileId)))
where(
and(
col(ProfileWant::userId).equal(userId),
if (type != null) col(Profile::type).equal(type) else null
val ids =
factory.pageQuery(pageable) {
select(column(ProfileWant::profileId))
from(entity(ProfileWant::class))
join(entity(Profile::class), col(Profile::id).equal(col(ProfileWant::profileId)))
where(
and(
col(ProfileWant::userId).equal(userId),
col(Profile::isDeleted).equal(false),
if (type != null) col(Profile::type).equal(type) else null
)
)
)
}
}

val profiles = factory.listQuery {
select(entity(Profile::class))
from(entity(Profile::class))
fetch(Profile::profileImages, joinType = JoinType.LEFT)
fetch(Profile::snsUrls, joinType = JoinType.LEFT)
where(col(Profile::id).`in`(ids.content))
}.associateBy { it!!.id }
val profiles =
factory.listQuery {
select(entity(Profile::class))
from(entity(Profile::class))
fetch(Profile::profileImages, joinType = JoinType.LEFT)
fetch(Profile::snsUrls, joinType = JoinType.LEFT)
where(col(Profile::id).`in`(ids.content))
}.associateBy { it!!.id }

ids.map { profiles[it] }
}
Expand All @@ -195,38 +208,34 @@ class ProfileRepositoryImpl(
}
}

private fun SpringDataReactiveCriteriaQueryDsl<Profile?>.idEq(
profileId: Long?,
): EqualValueSpec<Long?>? {
private fun SpringDataReactiveCriteriaQueryDsl<Profile?>.idEq(profileId: Long?): EqualValueSpec<Long?>? {
profileId ?: return null

return col(Profile::id).equal(profileId)
}

private fun SpringDataReactiveCriteriaQueryDsl<Profile?>.typeEq(
type: Type?,
): EqualValueSpec<Type>? {
private fun SpringDataReactiveCriteriaQueryDsl<Profile?>.typeEq(type: Type?): EqualValueSpec<Type>? {
type ?: return null

return col(Profile::type).equal(type)
}

private fun SpringDataReactiveCriteriaQueryDsl<Profile?>.orderSpec(
sort: Sort,
): List<OrderSpec> {
val res = sort.map {
val columnSpec = when (it.property) {
"viewCount" -> col(Profile::viewCount)
"createdAt" -> col(Profile::createdAt)
else -> col(Profile::viewCount)
}

if (it.isAscending) {
columnSpec.asc()
} else {
columnSpec.desc()
}
}.toList()
private fun SpringDataReactiveCriteriaQueryDsl<Profile?>.orderSpec(sort: Sort): List<OrderSpec> {
val res =
sort.map {
val columnSpec =
when (it.property) {
"viewCount" -> col(Profile::viewCount)
"createdAt" -> col(Profile::createdAt)
else -> col(Profile::viewCount)
}

if (it.isAscending) {
columnSpec.asc()
} else {
columnSpec.desc()
}
}.toList()

return res
}
Expand Down

0 comments on commit 01b029f

Please sign in to comment.