Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 프로필 조회 비즈니스 로직 수정
Browse files Browse the repository at this point in the history
lcomment committed Mar 1, 2024
1 parent 15d0512 commit 44cfcbe
Showing 8 changed files with 101 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -3,8 +3,11 @@ package com.lovebird.api.service.profile
import com.lovebird.api.dto.param.profile.ProfileCreateParam
import com.lovebird.api.dto.response.profile.ProfileDetailResponse
import com.lovebird.domain.dto.command.ProfileUpdateRequestParam
import com.lovebird.domain.dto.query.ProfilePartnerResponseParam
import com.lovebird.domain.dto.query.ProfileUserResponseParam
import com.lovebird.domain.entity.Profile
import com.lovebird.domain.entity.User
import com.lovebird.domain.repository.reader.CoupleEntryReader
import com.lovebird.domain.repository.reader.ProfileReader
import com.lovebird.domain.repository.writer.ProfileWriter
import org.springframework.stereotype.Service
@@ -13,7 +16,8 @@ import org.springframework.transaction.annotation.Transactional
@Service
class ProfileService(
private val profileReader: ProfileReader,
private val profileWriter: ProfileWriter
private val profileWriter: ProfileWriter,
private val coupleEntryReader: CoupleEntryReader
) {

@Transactional
@@ -28,7 +32,11 @@ class ProfileService(

@Transactional(readOnly = true)
fun findDetailByUser(user: User): ProfileDetailResponse {
return ProfileDetailResponse.from(profileReader.findDetailParamByUser(user))
val userParam: ProfileUserResponseParam = profileReader.findUserProfileByUser(user)
val partnerId: Long? = userParam.coupleEntryId?.let { coupleEntryReader.findPartnerIdById(it) }
val partnerParam: ProfilePartnerResponseParam? = partnerId?.let { profileReader.findPartnerProfileByUser(it) }

return ProfileDetailResponse.of(userParam, partnerParam)
}

private fun findByUser(user: User): Profile {
54 changes: 27 additions & 27 deletions lovebird-api/src/main/resources/static/docs/index.html
Original file line number Diff line number Diff line change
@@ -2116,8 +2116,8 @@ <h5 id="_success_6_http_response"><a class="link" href="#_success_6_http_respons
"nickname" : "test",
"partnerNickname" : "test2",
"firstDate" : "2024-01-01",
"birthday" : "2024-03-01",
"partnerBirthday" : "2024-03-01",
"birthday" : "2024-03-02",
"partnerBirthday" : "2024-03-02",
"dayCount" : 48,
"nextAnniversary" : {
"kind" : "DAY",
@@ -2279,8 +2279,8 @@ <h4 id="_수정_http_request"><a class="link" href="#_수정_http_request">6.2.1
"imageUrl" : "test-image-url",
"email" : "test-email",
"nickname" : "test-nickname",
"birthday" : "2024-03-01",
"firstDate" : "2024-03-01",
"birthday" : "2024-03-02",
"firstDate" : "2024-03-02",
"gender" : "UNKNOWN"
}</code></pre>
</div>
@@ -3672,7 +3672,7 @@ <h3 id="_데이트_날짜_기준_다이어리_조회"><a class="link" href="#_
<h4 id="_데이트_날짜_기준_다이어리_조회_http_request"><a class="link" href="#_데이트_날짜_기준_다이어리_조회_http_request">9.2.1. HTTP request</a></h4>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight nowrap"><code class="language-http hljs" data-lang="http">GET /api/v1/diaries/memory-date?memoryDate=2024-03-01 HTTP/1.1
<pre class="highlightjs highlight nowrap"><code class="language-http hljs" data-lang="http">GET /api/v1/diaries/memory-date?memoryDate=2024-03-02 HTTP/1.1
Authorization: Bearer access-token
Host: localhost:8080</code></pre>
</div>
@@ -3736,39 +3736,39 @@ <h4 id="_데이트_날짜_기준_다이어리_조회_http_response"><a class="li
"diaryId" : 1,
"userId" : 1,
"title" : "제목1",
"memoryDate" : "2024-03-01",
"memoryDate" : "2024-03-02",
"place" : "장소1",
"content" : "내용1",
"imageUrls" : [ "imageURL1" ]
}, {
"diaryId" : 2,
"userId" : 1,
"title" : "제목2",
"memoryDate" : "2024-03-01",
"memoryDate" : "2024-03-02",
"place" : "장소2",
"content" : "내용2",
"imageUrls" : [ "imageURL2" ]
}, {
"diaryId" : 3,
"userId" : 1,
"title" : "제목3",
"memoryDate" : "2024-03-01",
"memoryDate" : "2024-03-02",
"place" : "장소3",
"content" : "내용3",
"imageUrls" : [ "imageURL3" ]
}, {
"diaryId" : 4,
"userId" : 1,
"title" : "제목4",
"memoryDate" : "2024-03-01",
"memoryDate" : "2024-03-02",
"place" : "장소4",
"content" : "내용4",
"imageUrls" : [ "imageURL4" ]
}, {
"diaryId" : 5,
"userId" : 1,
"title" : "제목5",
"memoryDate" : "2024-03-01",
"memoryDate" : "2024-03-02",
"place" : "장소5",
"content" : "내용5",
"imageUrls" : [ "imageURL5" ]
@@ -3865,7 +3865,7 @@ <h3 id="_커서_기반_다이어리_리스트_조회"><a class="link" href="#_
<h4 id="_커서_기반_다이어리_리스트_조회_http_request"><a class="link" href="#_커서_기반_다이어리_리스트_조회_http_request">9.3.1. HTTP request</a></h4>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight nowrap"><code class="language-http hljs" data-lang="http">GET /api/v1/diaries/cursor?memoryDate=2024-03-01&amp;searchType=BEFORE&amp;diaryId=-1&amp;pageSize=10 HTTP/1.1
<pre class="highlightjs highlight nowrap"><code class="language-http hljs" data-lang="http">GET /api/v1/diaries/cursor?memoryDate=2024-03-02&amp;searchType=BEFORE&amp;diaryId=-1&amp;pageSize=10 HTTP/1.1
Authorization: Bearer access-token
Host: localhost:8080</code></pre>
</div>
@@ -3941,86 +3941,86 @@ <h4 id="_커서_기반_다이어리_리스트_조회_http_response"><a class="li
"diaryId" : 1,
"userId" : 1,
"title" : "제목1",
"memoryDate" : "2024-03-01",
"memoryDate" : "2024-03-02",
"place" : null,
"content" : "내용1",
"imageUrls" : [ ]
}, {
"diaryId" : 2,
"userId" : 2,
"title" : "제목2",
"memoryDate" : "2024-03-01",
"memoryDate" : "2024-03-02",
"place" : null,
"content" : "내용2",
"imageUrls" : [ ]
}, {
"diaryId" : 3,
"userId" : 3,
"title" : "제목3",
"memoryDate" : "2024-03-01",
"memoryDate" : "2024-03-02",
"place" : null,
"content" : "내용3",
"imageUrls" : [ ]
}, {
"diaryId" : 4,
"userId" : 4,
"title" : "제목4",
"memoryDate" : "2024-03-01",
"memoryDate" : "2024-03-02",
"place" : null,
"content" : "내용4",
"imageUrls" : [ ]
}, {
"diaryId" : 5,
"userId" : 5,
"title" : "제목5",
"memoryDate" : "2024-03-01",
"memoryDate" : "2024-03-02",
"place" : null,
"content" : "내용5",
"imageUrls" : [ ]
}, {
"diaryId" : 6,
"userId" : 6,
"title" : "제목6",
"memoryDate" : "2024-03-01",
"memoryDate" : "2024-03-02",
"place" : null,
"content" : "내용6",
"imageUrls" : [ ]
}, {
"diaryId" : 7,
"userId" : 7,
"title" : "제목7",
"memoryDate" : "2024-03-01",
"memoryDate" : "2024-03-02",
"place" : null,
"content" : "내용7",
"imageUrls" : [ ]
}, {
"diaryId" : 8,
"userId" : 8,
"title" : "제목8",
"memoryDate" : "2024-03-01",
"memoryDate" : "2024-03-02",
"place" : null,
"content" : "내용8",
"imageUrls" : [ ]
}, {
"diaryId" : 9,
"userId" : 9,
"title" : "제목9",
"memoryDate" : "2024-03-01",
"memoryDate" : "2024-03-02",
"place" : null,
"content" : "내용9",
"imageUrls" : [ ]
}, {
"diaryId" : 10,
"userId" : 10,
"title" : "제목10",
"memoryDate" : "2024-03-01",
"memoryDate" : "2024-03-02",
"place" : null,
"content" : "내용10",
"imageUrls" : [ ]
} ],
"totalCount" : 10,
"diaryId" : 10,
"memoryDate" : "2024-03-01"
"memoryDate" : "2024-03-02"
}
}</code></pre>
</div>
@@ -4142,7 +4142,7 @@ <h4 id="_다이어리_상세_조회_http_response"><a class="link" href="#_다
"diaryId" : 1,
"userId" : 1,
"title" : "다이어리 제목",
"memoryDate" : "2024-03-01",
"memoryDate" : "2024-03-02",
"place" : "장소",
"content" : "내용",
"imageUrls" : [ ]
@@ -4235,7 +4235,7 @@ <h4 id="_다이어리_생성_http_request"><a class="link" href="#_다이어리_

{
"title" : "다이어리 제목",
"memoryDate" : "2024-03-01",
"memoryDate" : "2024-03-02",
"place" : "장소",
"content" : "내용",
"imageUrls" : [ "imageUrl1", "imageUrl2" ]
@@ -4249,7 +4249,7 @@ <h4 id="_다이어리_생성_request_body"><a class="link" href="#_다이어리_
<div class="content">
<pre class="highlightjs highlight nowrap"><code class="language-json hljs" data-lang="json">{
"title" : "다이어리 제목",
"memoryDate" : "2024-03-01",
"memoryDate" : "2024-03-02",
"place" : "장소",
"content" : "내용",
"imageUrls" : [ "imageUrl1", "imageUrl2" ]
@@ -4386,7 +4386,7 @@ <h4 id="_다이어리_수정_http_request"><a class="link" href="#_다이어리_

{
"title" : "다이어리 제목 수정",
"memoryDate" : "2024-03-01",
"memoryDate" : "2024-03-02",
"place" : "장소",
"content" : "내용 수정",
"imageUrls" : [ "imageUrl1", "imageUrl2" ]
@@ -4400,7 +4400,7 @@ <h4 id="_다이어리_수정_request_body"><a class="link" href="#_다이어리_
<div class="content">
<pre class="highlightjs highlight nowrap"><code class="language-json hljs" data-lang="json">{
"title" : "다이어리 제목 수정",
"memoryDate" : "2024-03-01",
"memoryDate" : "2024-03-02",
"place" : "장소",
"content" : "내용 수정",
"imageUrls" : [ "imageUrl1", "imageUrl2" ]
Original file line number Diff line number Diff line change
@@ -3,13 +3,15 @@ package com.lovebird.api.service.profile
import com.lovebird.api.common.base.ServiceDescribeSpec
import com.lovebird.api.dto.param.profile.ProfileCreateParam
import com.lovebird.api.dto.response.profile.ProfileDetailResponse
import com.lovebird.api.utils.ProfileTestFixture.getProfilePartnerParam
import com.lovebird.api.utils.ProfileTestFixture.getProfileUserParam
import com.lovebird.common.enums.Gender
import com.lovebird.common.enums.Provider
import com.lovebird.common.enums.ReturnCode
import com.lovebird.common.exception.LbException
import com.lovebird.domain.dto.command.ProfileUpdateRequestParam
import com.lovebird.domain.dto.query.ProfileDetailResponseParam
import com.lovebird.domain.entity.User
import com.lovebird.domain.repository.reader.CoupleEntryReader
import com.lovebird.domain.repository.reader.ProfileReader
import com.lovebird.domain.repository.writer.ProfileWriter
import io.kotest.assertions.throwables.shouldNotThrow
@@ -24,7 +26,8 @@ class ProfileServiceTest : ServiceDescribeSpec({

val profileReader: ProfileReader = mockk<ProfileReader>(relaxed = true)
val profileWriter: ProfileWriter = mockk<ProfileWriter>(relaxed = true)
val profileService = ProfileService(profileReader, profileWriter)
val coupleEntryReader: CoupleEntryReader = mockk<CoupleEntryReader>(relaxed = true)
val profileService = ProfileService(profileReader, profileWriter, coupleEntryReader)

afterEach {
clearMocks(profileReader, profileWriter)
@@ -73,17 +76,22 @@ class ProfileServiceTest : ServiceDescribeSpec({
val user: User = getUser()

context("해당 유저가 프로필을 가지고 있을 때") {
every { profileReader.findDetailParamByUser(user) } returns getDetailParam()
val userParam = getProfileUserParam()
val partnerParam = getProfilePartnerParam()

every { profileReader.findUserProfileByUser(user) } returns userParam
every { coupleEntryReader.findPartnerIdById(any()) } returns 2L
every { profileReader.findPartnerProfileByUser(2L) } returns partnerParam

it("상세 조회를 성공한다.") {
val detailResponse: ProfileDetailResponse = profileService.findDetailByUser(user)

detailResponse shouldBe ProfileDetailResponse.from(getDetailParam())
detailResponse shouldBe ProfileDetailResponse.of(userParam, partnerParam)
}
}

context("해당 유저가 프로필을 가지고 있지 않을 때") {
every { profileReader.findDetailParamByUser(user) } throws LbException(ReturnCode.NOT_EXIST_PROFILE)
every { profileReader.findUserProfileByUser(user) } throws LbException(ReturnCode.NOT_EXIST_PROFILE)

it("예외를 반환한다") {
shouldThrow<LbException> { profileService.findDetailByUser(user) }
@@ -116,21 +124,6 @@ class ProfileServiceTest : ServiceDescribeSpec({
)
}

fun getDetailParam(): ProfileDetailResponseParam {
return ProfileDetailResponseParam(
userId = 1L,
partnerId = 2L,
email = "test-email",
nickname = "test-nickname",
partnerNickname = "test-partner-nickname",
firstDate = LocalDate.of(2021, 1, 1),
birthday = LocalDate.of(1998, 5, 6),
partnerBirthday = LocalDate.of(1998, 3, 16),
profileImageUrl = "test-profile-image-url",
partnerImageUrl = "test-partner-image-url"
)
}

fun getUser(): User {
return User(
provider = Provider.APPLE,
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.lovebird.api.utils

import com.lovebird.domain.dto.query.ProfilePartnerResponseParam
import com.lovebird.domain.dto.query.ProfileUserResponseParam
import java.time.LocalDate

object ProfileTestFixture {

fun getProfileUserParam(): ProfileUserResponseParam {
return ProfileUserResponseParam(
userId = 1L,
coupleEntryId = 1L,
email = "test-email",
nickname = "test-nickname",
firstDate = LocalDate.of(2021, 1, 1),
birthday = LocalDate.of(1998, 5, 6),
profileImageUrl = "test-profile-image-url"
)
}

fun getProfilePartnerParam(): ProfilePartnerResponseParam {
return ProfilePartnerResponseParam(
partnerId = 2L,
partnerNickname = "test-partner-nickname",
partnerBirthday = LocalDate.of(1998, 3, 16),
partnerImageUrl = "test-partner-image-url"
)
}
}
Original file line number Diff line number Diff line change
@@ -18,5 +18,15 @@ class CoupleEntryQueryRepository(
.fetchOne()
}

fun findPartnerIdById(id: Long): Long {
return queryFactory
.select(coupleEntry.partner.id)
.from(coupleEntry)
.where(eqCoupleEntryId(id))
.fetchOne()!!
}

fun eqCoupleEntryId(coupleEntryId: Long): BooleanExpression = coupleEntry.id.eq(coupleEntryId)

fun eqUserId(userId: Long): BooleanExpression = coupleEntry.user.id.eq(userId)
}
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import com.lovebird.domain.dto.query.QProfilePartnerResponseParam
import com.lovebird.domain.dto.query.QProfileUserResponseParam
import com.lovebird.domain.entity.QCoupleEntry.coupleEntry
import com.lovebird.domain.entity.QProfile.profile
import com.lovebird.domain.entity.QUser.user
import com.querydsl.core.types.dsl.BooleanExpression
import com.querydsl.jpa.impl.JPAQueryFactory
import org.springframework.stereotype.Repository
@@ -35,23 +36,19 @@ class ProfileQueryRepository(
.fetchOne()
}

fun findDetailPartnerParamByUser(partnerId: Long): ProfilePartnerResponseParam? {
fun findDetailPartnerParamByUser(partnerId: Long): ProfilePartnerResponseParam {
return queryFactory
.select(
QProfilePartnerResponseParam(
profile.user.id,
profile.email,
profile.nickname,
profile.firstDate,
profile.birthday,
profile.imageUrl
)
)
.from(profile)
.innerJoin(coupleEntry)
.on(profile.user.eq(coupleEntry.user))
.where(eqUserId(partnerId))
.fetchOne()
.fetchOne()!!
}

private fun eqUserId(userId: Long): BooleanExpression {
Original file line number Diff line number Diff line change
@@ -17,4 +17,8 @@ class CoupleEntryReader(
fun findByUser(user: User): CoupleEntry? {
return coupleEntryQueryRepository.findByUserId(user.id!!)
}

fun findPartnerIdById(id: Long): Long {
return coupleEntryQueryRepository.findPartnerIdById(id)
}
}
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@ package com.lovebird.domain.repository.reader
import com.lovebird.common.enums.ReturnCode
import com.lovebird.common.exception.LbException
import com.lovebird.domain.annotation.Reader
import com.lovebird.domain.dto.query.ProfileDetailResponseParam
import com.lovebird.domain.dto.query.ProfilePartnerResponseParam
import com.lovebird.domain.dto.query.ProfileUserResponseParam
import com.lovebird.domain.entity.Profile
@@ -21,12 +20,11 @@ class ProfileReader(
return profileJpaRepository.findByUser(user) ?: throw LbException(ReturnCode.NOT_EXIST_PROFILE)
}

fun findDetailParamByUser(user: User): ProfileDetailResponseParam {
val userParam: ProfileUserResponseParam = profileQueryRepository.findDetailUserParamByUser(user.id!!)
?: throw LbException(ReturnCode.NOT_EXIST_PROFILE)
val partnerParam: ProfilePartnerResponseParam? =
userParam.coupleEntryId?.let { profileQueryRepository.findDetailPartnerParamByUser(it) }
fun findUserProfileByUser(user: User): ProfileUserResponseParam {
return profileQueryRepository.findDetailUserParamByUser(user.id!!) ?: throw LbException(ReturnCode.NOT_EXIST_PROFILE)
}

return ProfileDetailResponseParam.of(userParam, partnerParam)
fun findPartnerProfileByUser(partnerId: Long): ProfilePartnerResponseParam? {
return profileQueryRepository.findDetailPartnerParamByUser(partnerId)
}
}

0 comments on commit 44cfcbe

Please sign in to comment.