From d0cac56dc84ca92cfac914d53529a2c5354c2c1e Mon Sep 17 00:00:00 2001 From: TAEYONG KIM <86272688+YongsHub@users.noreply.github.com> Date: Thu, 29 Feb 2024 11:54:17 +0900 Subject: [PATCH] =?UTF-8?q?Hotfix=20|=20#81=20|=20@YongsHub=20|=20v1.0.1?= =?UTF-8?q?=20=EC=B6=9C=EC=8B=9C=20=EC=9C=84=ED=95=B4=20Query=20Transform?= =?UTF-8?q?=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: QueryDSL Config 수정 * refactor: Diary 쿼리 transform 변경 및 DTO 생성 방법 리팩토링 * refactor: 쿼리 변경으로 인한 로직 리팩토링 * test: DTO 변경으로 인한 테스트 코드 수정 * refactor: ktlint 적용 * refactor: DiarySimpleResponse로 리팩토링 * test: 유저의 다이어리 전체 조회 및 날짜별 조회 리팩토링 * refactor: ktlint 적용 * feat: Business Exception 추가 * refactor: ktlint 적용 --- .../api/controller/GlobalControllerAdvice.kt | 10 ++ .../api/controller/diary/DiaryController.kt | 6 +- .../response/diary/DiarySimpleListResponse.kt | 14 --- .../dto/response/diary/DiarySimpleResponse.kt | 14 +++ .../api/service/diary/DiaryService.kt | 16 +-- .../com/lovebird/api/util/DiaryUtils.kt | 3 +- .../src/main/resources/static/docs/index.html | 74 ++++++------- .../controller/diary/DiaryControllerTest.kt | 26 ++--- .../api/service/diary/DiaryServiceTest.kt | 10 +- .../lovebird/api/utils/DiaryTestFixture.kt | 9 +- .../lovebird/domain/config/QuerydslConfig.kt | 3 +- .../domain/dto/query/DiaryResponseParam.kt | 23 +++- .../dto/query/DiarySimpleResponseParam.kt | 14 --- .../repository/query/DiaryQueryRepository.kt | 104 ++++-------------- .../domain/repository/reader/DiaryReader.kt | 5 +- 15 files changed, 137 insertions(+), 194 deletions(-) delete mode 100644 lovebird-api/src/main/kotlin/com/lovebird/api/dto/response/diary/DiarySimpleListResponse.kt create mode 100644 lovebird-api/src/main/kotlin/com/lovebird/api/dto/response/diary/DiarySimpleResponse.kt delete mode 100644 lovebird-domain/src/main/kotlin/com/lovebird/domain/dto/query/DiarySimpleResponseParam.kt diff --git a/lovebird-api/src/main/kotlin/com/lovebird/api/controller/GlobalControllerAdvice.kt b/lovebird-api/src/main/kotlin/com/lovebird/api/controller/GlobalControllerAdvice.kt index 4900b41..fa88762 100644 --- a/lovebird-api/src/main/kotlin/com/lovebird/api/controller/GlobalControllerAdvice.kt +++ b/lovebird-api/src/main/kotlin/com/lovebird/api/controller/GlobalControllerAdvice.kt @@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler import org.springframework.web.bind.annotation.RestControllerAdvice import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException import java.sql.SQLException +import kotlin.RuntimeException @RestControllerAdvice class GlobalControllerAdvice { @@ -59,4 +60,13 @@ class GlobalControllerAdvice { .status(HttpStatus.INTERNAL_SERVER_ERROR) .body(ApiResponse.error(ReturnCode.INTERNAL_SERVER_ERROR)) } + + @ExceptionHandler( + value = [RuntimeException::class] + ) + fun handleBusinessException(e: RuntimeException): ResponseEntity> { + return ResponseEntity + .status(HttpStatus.INTERNAL_SERVER_ERROR) + .body(ApiResponse.error(ReturnCode.INTERNAL_SERVER_ERROR)) + } } diff --git a/lovebird-api/src/main/kotlin/com/lovebird/api/controller/diary/DiaryController.kt b/lovebird-api/src/main/kotlin/com/lovebird/api/controller/diary/DiaryController.kt index 15ac91f..b423dd1 100644 --- a/lovebird-api/src/main/kotlin/com/lovebird/api/controller/diary/DiaryController.kt +++ b/lovebird-api/src/main/kotlin/com/lovebird/api/controller/diary/DiaryController.kt @@ -6,7 +6,7 @@ import com.lovebird.api.dto.request.diary.DiaryListRequest import com.lovebird.api.dto.request.diary.DiaryUpdateRequest import com.lovebird.api.dto.response.diary.DiaryDetailResponse import com.lovebird.api.dto.response.diary.DiaryListResponse -import com.lovebird.api.dto.response.diary.DiarySimpleListResponse +import com.lovebird.api.dto.response.diary.DiarySimpleResponse import com.lovebird.api.service.diary.DiaryService import com.lovebird.common.response.ApiResponse import com.lovebird.domain.entity.User @@ -39,14 +39,14 @@ class DiaryController( fun findAllByMemoryDate( @AuthorizedUser user: User, @ModelAttribute request: DiaryListRequest.SearchByMemoryDateRequest - ): ApiResponse { + ): ApiResponse { return ApiResponse.success(diaryService.findAllByMemoryDate(request, user)) } @GetMapping fun findAll( @AuthorizedUser user: User - ): ApiResponse { + ): ApiResponse { return ApiResponse.success(diaryService.findAll(user)) } diff --git a/lovebird-api/src/main/kotlin/com/lovebird/api/dto/response/diary/DiarySimpleListResponse.kt b/lovebird-api/src/main/kotlin/com/lovebird/api/dto/response/diary/DiarySimpleListResponse.kt deleted file mode 100644 index f40d309..0000000 --- a/lovebird-api/src/main/kotlin/com/lovebird/api/dto/response/diary/DiarySimpleListResponse.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.lovebird.api.dto.response.diary - -import com.lovebird.domain.dto.query.DiarySimpleResponseParam - -data class DiarySimpleListResponse( - val diaries: List, - val totalCount: Int = diaries.size -) { - companion object { - fun of(diaries: List): DiarySimpleListResponse { - return DiarySimpleListResponse(diaries) - } - } -} diff --git a/lovebird-api/src/main/kotlin/com/lovebird/api/dto/response/diary/DiarySimpleResponse.kt b/lovebird-api/src/main/kotlin/com/lovebird/api/dto/response/diary/DiarySimpleResponse.kt new file mode 100644 index 0000000..43406f6 --- /dev/null +++ b/lovebird-api/src/main/kotlin/com/lovebird/api/dto/response/diary/DiarySimpleResponse.kt @@ -0,0 +1,14 @@ +package com.lovebird.api.dto.response.diary + +import com.lovebird.domain.dto.query.DiaryResponseParam + +data class DiarySimpleResponse( + val diaries: List, + val totalCount: Int = diaries.size +) { + companion object { + fun of(diaries: List): DiarySimpleResponse { + return DiarySimpleResponse(diaries) + } + } +} diff --git a/lovebird-api/src/main/kotlin/com/lovebird/api/service/diary/DiaryService.kt b/lovebird-api/src/main/kotlin/com/lovebird/api/service/diary/DiaryService.kt index 2b354b1..d5a9725 100644 --- a/lovebird-api/src/main/kotlin/com/lovebird/api/service/diary/DiaryService.kt +++ b/lovebird-api/src/main/kotlin/com/lovebird/api/service/diary/DiaryService.kt @@ -5,7 +5,7 @@ import com.lovebird.api.dto.param.diary.DiaryUpdateParam import com.lovebird.api.dto.request.diary.DiaryListRequest import com.lovebird.api.dto.response.diary.DiaryDetailResponse import com.lovebird.api.dto.response.diary.DiaryListResponse -import com.lovebird.api.dto.response.diary.DiarySimpleListResponse +import com.lovebird.api.dto.response.diary.DiarySimpleResponse import com.lovebird.api.util.DiaryUtils.decryptDiaries import com.lovebird.api.util.DiaryUtils.decryptDiariesOfSimple import com.lovebird.api.util.DiaryUtils.decryptDiary @@ -14,7 +14,6 @@ import com.lovebird.api.util.DiaryUtils.encryptDiaryUpdateParam import com.lovebird.common.enums.DiarySearchType import com.lovebird.domain.dto.query.DiaryListRequestParam import com.lovebird.domain.dto.query.DiaryResponseParam -import com.lovebird.domain.dto.query.DiarySimpleResponseParam import com.lovebird.domain.entity.CoupleEntry import com.lovebird.domain.entity.Diary import com.lovebird.domain.entity.User @@ -78,25 +77,26 @@ class DiaryService( } @Transactional(readOnly = true) - fun findAll(user: User): DiarySimpleListResponse { + fun findAll(user: User): DiarySimpleResponse { val coupleEntry: CoupleEntry? = coupleEntryReader.findByUser(user) val partner: User? = coupleEntry?.partner - val diaries: List = diaryReader.findAll(user.id!!, partner?.id) + + val diaries: List = diaryReader.findAll(user.id!!, partner?.id) decryptDiariesOfSimple(diaries) - return DiarySimpleListResponse.of(diaries) + return DiarySimpleResponse.of(diaries) } @Transactional(readOnly = true) - fun findAllByMemoryDate(request: DiaryListRequest.SearchByMemoryDateRequest, user: User): DiarySimpleListResponse { + fun findAllByMemoryDate(request: DiaryListRequest.SearchByMemoryDateRequest, user: User): DiarySimpleResponse { val coupleEntry: CoupleEntry? = coupleEntryReader.findByUser(user) val partner: User? = coupleEntry?.partner - val diaries: List = diaryReader.findAllByMemoryDate(request.toParam(user.id!!, partner?.id)) + val diaries: List = diaryReader.findAllByMemoryDate(request.toParam(user.id!!, partner?.id)) decryptDiariesOfSimple(diaries) - return DiarySimpleListResponse.of(diaries) + return DiarySimpleResponse.of(diaries) } @Transactional(readOnly = true) diff --git a/lovebird-api/src/main/kotlin/com/lovebird/api/util/DiaryUtils.kt b/lovebird-api/src/main/kotlin/com/lovebird/api/util/DiaryUtils.kt index cc68ac0..2e70522 100644 --- a/lovebird-api/src/main/kotlin/com/lovebird/api/util/DiaryUtils.kt +++ b/lovebird-api/src/main/kotlin/com/lovebird/api/util/DiaryUtils.kt @@ -5,7 +5,6 @@ import com.lovebird.api.dto.param.diary.DiaryUpdateParam import com.lovebird.api.provider.AesEncryptProvider.decryptString import com.lovebird.api.provider.AesEncryptProvider.encryptString import com.lovebird.domain.dto.query.DiaryResponseParam -import com.lovebird.domain.dto.query.DiarySimpleResponseParam import com.lovebird.domain.entity.Diary object DiaryUtils { @@ -18,7 +17,7 @@ object DiaryUtils { } } - fun decryptDiariesOfSimple(diaries: List) { + fun decryptDiariesOfSimple(diaries: List) { diaries.forEach { it.title = decryptString(it.title) it.place = it.place?.let { place -> decryptString(place) } diff --git a/lovebird-api/src/main/resources/static/docs/index.html b/lovebird-api/src/main/resources/static/docs/index.html index 1f4414d..8a91e55 100644 --- a/lovebird-api/src/main/resources/static/docs/index.html +++ b/lovebird-api/src/main/resources/static/docs/index.html @@ -2112,8 +2112,8 @@
6.2.1 "imageUrl" : "test-image-url", "email" : "test-email", "nickname" : "test-nickname", - "birthday" : "2024-02-26", - "firstDate" : "2024-02-26", + "birthday" : "2024-02-29", + "firstDate" : "2024-02-29", "gender" : "UNKNOWN" } @@ -3674,7 +3674,7 @@

9.2.1. HTTP request

-
GET /api/v1/diaries/memory-date?memoryDate=2024-02-26 HTTP/1.1
+
GET /api/v1/diaries/memory-date?memoryDate=2024-02-29 HTTP/1.1
 Authorization: Bearer access-token
 Host: localhost:8080
@@ -3728,7 +3728,7 @@

HTTP/1.1 200 OK
 Content-Type: application/json;charset=UTF-8
-Content-Length: 1125
+Content-Length: 1150
 
 {
   "code" : "1000",
@@ -3738,42 +3738,42 @@ 

내용

-

data.diaries[].imageUrl

-

String

-

이미지 URL

+

data.diaries[].imageUrls

+

Array

+

이미지 URL 목록

@@ -3867,7 +3867,7 @@

9.3.1. HTTP request

-
GET /api/v1/diaries/cursor?memoryDate=2024-02-26&searchType=BEFORE&diaryId=-1&pageSize=10 HTTP/1.1
+
GET /api/v1/diaries/cursor?memoryDate=2024-02-29&searchType=BEFORE&diaryId=-1&pageSize=10 HTTP/1.1
 Authorization: Bearer access-token
 Host: localhost:8080
@@ -3943,7 +3943,7 @@

@@ -4144,7 +4144,7 @@

{
   "title" : "다이어리 제목",
-  "memoryDate" : "2024-02-26",
+  "memoryDate" : "2024-02-29",
   "place" : "장소",
   "content" : "내용",
   "imageUrls" : [ "imageUrl1", "imageUrl2" ]
@@ -4388,7 +4388,7 @@ 

{
   "title" : "다이어리 제목 수정",
-  "memoryDate" : "2024-02-26",
+  "memoryDate" : "2024-02-29",
   "place" : "장소",
   "content" : "내용 수정",
   "imageUrls" : [ "imageUrl1", "imageUrl2" ]
@@ -4585,7 +4585,7 @@ 

diff --git a/lovebird-api/src/test/kotlin/com/lovebird/api/controller/diary/DiaryControllerTest.kt b/lovebird-api/src/test/kotlin/com/lovebird/api/controller/diary/DiaryControllerTest.kt index dd63930..915d0e7 100644 --- a/lovebird-api/src/test/kotlin/com/lovebird/api/controller/diary/DiaryControllerTest.kt +++ b/lovebird-api/src/test/kotlin/com/lovebird/api/controller/diary/DiaryControllerTest.kt @@ -3,7 +3,7 @@ package com.lovebird.api.controller.diary import com.lovebird.api.common.base.ControllerDescribeSpec import com.lovebird.api.dto.response.diary.DiaryDetailResponse import com.lovebird.api.dto.response.diary.DiaryListResponse -import com.lovebird.api.dto.response.diary.DiarySimpleListResponse +import com.lovebird.api.dto.response.diary.DiarySimpleResponse import com.lovebird.api.service.diary.DiaryService import com.lovebird.api.utils.CommonTestFixture import com.lovebird.api.utils.DiaryTestFixture @@ -59,8 +59,8 @@ class DiaryControllerTest( .header(HttpHeaders.AUTHORIZATION, "Bearer access-token") val user = CommonTestFixture.getUser(1L, "providerUniqueId") - val diaries = DiaryTestFixture.getDiarySimpleResponseList(user, null, 5) - val response = DiarySimpleListResponse.of(diaries) + val diaries = DiaryTestFixture.getDiaryResponseList(user, null, 5) + val response = DiarySimpleResponse.of(diaries) it("1000 SUCCESS") { every { diaryService.findAllByMemoryDate(any(), any()) } returns response @@ -80,7 +80,7 @@ class DiaryControllerTest( "data.diaries" type ARRAY means "다이어리 목록", "data.totalCount" type NUMBER means "캘린더 개수" ) - .andWithPrefix("data.diaries[].", getSimpleDiaryDetailResponseSnippet()) + .andWithPrefix("data.diaries[].", getDiaryDetailResponseSnippet()) ) } } @@ -131,8 +131,8 @@ class DiaryControllerTest( context("다이어리 전체 조회 요청한다면") { val request = request(HttpMethod.GET, url) .header(HttpHeaders.AUTHORIZATION, "Bearer access-token") - val diaries = DiaryTestFixture.getDiarySimpleResponseList(user, null, 5) - val response = DiarySimpleListResponse.of(diaries) + val diaries = DiaryTestFixture.getDiaryResponseList(user, null, 5) + val response = DiarySimpleResponse.of(diaries) it("1000 SUCCESS") { every { diaryService.findAll(any()) } returns response @@ -149,7 +149,7 @@ class DiaryControllerTest( "data.diaries" type ARRAY means "다이어리 목록", "data.totalCount" type NUMBER means "캘린더 개수" ) - .andWithPrefix("data.diaries[].", getSimpleDiaryDetailResponseSnippet()) + .andWithPrefix("data.diaries[].", getDiaryDetailResponseSnippet()) ) } } @@ -268,18 +268,6 @@ class DiaryControllerTest( } }) { companion object { - fun getSimpleDiaryDetailResponseSnippet(): List { - return responseDiaryDetailResponse( - "diaryId" type NUMBER means "다이어리 아이디", - "userId" type NUMBER means "유저 아이디", - "title" type STRING means "제목", - "memoryDate" type DATE means "데이트 날짜", - "place" type STRING means "장소", - "content" type STRING means "내용", - "imageUrl" type STRING means "이미지 URL" - ) - } - fun getDiaryDetailResponseSnippet(): List { return responseDiaryDetailResponse( "diaryId" type NUMBER means "다이어리 아이디", diff --git a/lovebird-api/src/test/kotlin/com/lovebird/api/service/diary/DiaryServiceTest.kt b/lovebird-api/src/test/kotlin/com/lovebird/api/service/diary/DiaryServiceTest.kt index bdee319..df88594 100644 --- a/lovebird-api/src/test/kotlin/com/lovebird/api/service/diary/DiaryServiceTest.kt +++ b/lovebird-api/src/test/kotlin/com/lovebird/api/service/diary/DiaryServiceTest.kt @@ -3,7 +3,7 @@ package com.lovebird.api.service.diary import com.lovebird.api.common.base.ServiceDescribeSpec import com.lovebird.api.dto.response.diary.DiaryDetailResponse import com.lovebird.api.dto.response.diary.DiaryListResponse -import com.lovebird.api.dto.response.diary.DiarySimpleListResponse +import com.lovebird.api.dto.response.diary.DiarySimpleResponse import com.lovebird.api.util.DiaryUtils import com.lovebird.api.utils.CoupleTestFixture.getCoupleEntry import com.lovebird.api.utils.DiaryTestFixture @@ -248,7 +248,7 @@ class DiaryServiceTest : ServiceDescribeSpec({ val size = 5 context("파트너가 존재하지 않아도") { - val diaries = DiaryTestFixture.getDiarySimpleResponseList(user = user, partner = null, size = size) + val diaries = DiaryTestFixture.getDiaryResponseList(user = user, partner = null, size = size) every { coupleEntryReader.findByUser(user) } returns getCoupleEntry(user = user, partner = user) every { diaryReader.findAllByMemoryDate(any()) } returns diaries every { DiaryUtils.decryptDiariesOfSimple(any()) } just Runs @@ -257,7 +257,7 @@ class DiaryServiceTest : ServiceDescribeSpec({ // 상태 검증 diaryService.findAllByMemoryDate(request, user) - .shouldBeEqualToComparingFields(DiarySimpleListResponse.of(diaries)) + .shouldBeEqualToComparingFields(DiarySimpleResponse.of(diaries)) // 행위 검증 verify(exactly = 1) { @@ -269,7 +269,7 @@ class DiaryServiceTest : ServiceDescribeSpec({ } context("파트너가 존재한다면") { - val diaries = DiaryTestFixture.getDiarySimpleResponseList(user = user, partner = partner, size = size) + val diaries = DiaryTestFixture.getDiaryResponseList(user = user, partner = partner, size = size) every { coupleEntryReader.findByUser(user) } returns getCoupleEntry(user = user, partner = partner) every { diaryReader.findAllByMemoryDate(any()) } returns diaries every { DiaryUtils.decryptDiariesOfSimple(any()) } just Runs @@ -278,7 +278,7 @@ class DiaryServiceTest : ServiceDescribeSpec({ // 상태 검증 diaryService.findAllByMemoryDate(request, user) - .shouldBeEqualToComparingFields(DiarySimpleListResponse.of(diaries)) + .shouldBeEqualToComparingFields(DiarySimpleResponse.of(diaries)) // 행위 검증 verify(exactly = 1) { diff --git a/lovebird-api/src/test/kotlin/com/lovebird/api/utils/DiaryTestFixture.kt b/lovebird-api/src/test/kotlin/com/lovebird/api/utils/DiaryTestFixture.kt index 4efc36a..815e05f 100644 --- a/lovebird-api/src/test/kotlin/com/lovebird/api/utils/DiaryTestFixture.kt +++ b/lovebird-api/src/test/kotlin/com/lovebird/api/utils/DiaryTestFixture.kt @@ -7,7 +7,6 @@ import com.lovebird.api.dto.request.diary.DiaryListRequest import com.lovebird.api.dto.request.diary.DiaryUpdateRequest import com.lovebird.common.enums.DiarySearchType import com.lovebird.domain.dto.query.DiaryResponseParam -import com.lovebird.domain.dto.query.DiarySimpleResponseParam import com.lovebird.domain.entity.Diary import com.lovebird.domain.entity.User import org.springframework.test.util.ReflectionTestUtils @@ -125,19 +124,19 @@ object DiaryTestFixture { ) } - fun getDiarySimpleResponseList(user: User, partner: User?, size: Int): List { - val diaries = arrayListOf() + fun getDiaryResponseList(user: User, partner: User?, size: Int): List { + val diaries = arrayListOf() for (i in 1..size) { diaries.add( - DiarySimpleResponseParam( + DiaryResponseParam( diaryId = i.toLong(), userId = getRandomUserId(user, partner), title = "제목$i", memoryDate = LocalDate.now(), place = "장소$i", content = "내용$i", - imageUrl = "imageURL$i" + imageUrls = arrayListOf("imageURL$i") ) ) } diff --git a/lovebird-domain/src/main/kotlin/com/lovebird/domain/config/QuerydslConfig.kt b/lovebird-domain/src/main/kotlin/com/lovebird/domain/config/QuerydslConfig.kt index 8a8fb64..bac057c 100644 --- a/lovebird-domain/src/main/kotlin/com/lovebird/domain/config/QuerydslConfig.kt +++ b/lovebird-domain/src/main/kotlin/com/lovebird/domain/config/QuerydslConfig.kt @@ -1,5 +1,6 @@ package com.lovebird.domain.config +import com.querydsl.jpa.JPQLTemplates import com.querydsl.jpa.impl.JPAQueryFactory import jakarta.persistence.EntityManager import jakarta.persistence.PersistenceContext @@ -13,6 +14,6 @@ class QuerydslConfig { @Bean fun queryFactory(): JPAQueryFactory { - return JPAQueryFactory(entityManager) + return JPAQueryFactory(JPQLTemplates.DEFAULT, entityManager) } } diff --git a/lovebird-domain/src/main/kotlin/com/lovebird/domain/dto/query/DiaryResponseParam.kt b/lovebird-domain/src/main/kotlin/com/lovebird/domain/dto/query/DiaryResponseParam.kt index 4bc8a8d..9c4a969 100644 --- a/lovebird-domain/src/main/kotlin/com/lovebird/domain/dto/query/DiaryResponseParam.kt +++ b/lovebird-domain/src/main/kotlin/com/lovebird/domain/dto/query/DiaryResponseParam.kt @@ -1,5 +1,6 @@ package com.lovebird.domain.dto.query +import com.lovebird.domain.entity.Diary import java.time.LocalDate data class DiaryResponseParam( @@ -9,5 +10,23 @@ data class DiaryResponseParam( val memoryDate: LocalDate, var place: String?, var content: String?, - val imageUrls: List -) + val imageUrls: List? +) { + + companion object { + fun of(transform: Map>): List { + return transform.entries.stream() + .map { entry -> + DiaryResponseParam( + diaryId = entry.key.id!!, + userId = entry.key.user.id!!, + title = entry.key.title, + memoryDate = entry.key.memoryDate, + place = entry.key.place, + content = entry.key.content, + imageUrls = entry.value + ) + }.toList() + } + } +} diff --git a/lovebird-domain/src/main/kotlin/com/lovebird/domain/dto/query/DiarySimpleResponseParam.kt b/lovebird-domain/src/main/kotlin/com/lovebird/domain/dto/query/DiarySimpleResponseParam.kt deleted file mode 100644 index 3f846ad..0000000 --- a/lovebird-domain/src/main/kotlin/com/lovebird/domain/dto/query/DiarySimpleResponseParam.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.lovebird.domain.dto.query - -import com.querydsl.core.annotations.QueryProjection -import java.time.LocalDate - -data class DiarySimpleResponseParam @QueryProjection constructor( - val diaryId: Long, - val userId: Long, - var title: String, - val memoryDate: LocalDate, - var place: String?, - var content: String?, - val imageUrl: String? -) diff --git a/lovebird-domain/src/main/kotlin/com/lovebird/domain/repository/query/DiaryQueryRepository.kt b/lovebird-domain/src/main/kotlin/com/lovebird/domain/repository/query/DiaryQueryRepository.kt index e669439..dacdc39 100644 --- a/lovebird-domain/src/main/kotlin/com/lovebird/domain/repository/query/DiaryQueryRepository.kt +++ b/lovebird-domain/src/main/kotlin/com/lovebird/domain/repository/query/DiaryQueryRepository.kt @@ -3,16 +3,12 @@ package com.lovebird.domain.repository.query import com.lovebird.domain.dto.query.DiaryListRequestParam import com.lovebird.domain.dto.query.DiaryResponseParam import com.lovebird.domain.dto.query.DiarySimpleRequestParam -import com.lovebird.domain.dto.query.DiarySimpleResponseParam -import com.lovebird.domain.dto.query.QDiarySimpleResponseParam -import com.lovebird.domain.entity.QDiary +import com.lovebird.domain.entity.Diary import com.lovebird.domain.entity.QDiary.diary import com.lovebird.domain.entity.QDiaryImage.diaryImage -import com.lovebird.domain.entity.QUser.user import com.querydsl.core.group.GroupBy.groupBy import com.querydsl.core.group.GroupBy.list import com.querydsl.core.types.OrderSpecifier -import com.querydsl.core.types.Projections import com.querydsl.core.types.dsl.BooleanExpression import com.querydsl.core.types.dsl.NumberPath import com.querydsl.jpa.impl.JPAQueryFactory @@ -25,9 +21,9 @@ class DiaryQueryRepository( ) { fun findBeforeNowUsingCursor(param: DiaryListRequestParam): List { - return queryFactory + val transform: Map> = queryFactory .from(diary) - .innerJoin(diary.user) + .leftJoin(diary.diaryImages, diaryImage) .where( eqCouple(param.userId, param.partnerId), eqMemoryDateAndGtDiaryId(param.memoryDate, param.diaryId), @@ -35,32 +31,15 @@ class DiaryQueryRepository( ) .orderBy(descMemoryDate(), ascDiaryId()) .limit(param.pageSize) - .transform( - groupBy(diary.id) - .list( - Projections.constructor( - DiaryResponseParam::class.java, - diary.id, - user.id, - diary.title, - diary.memoryDate, - diary.place, - diary.content, - list( - Projections.constructor( - String::class.java, - diaryImage.imageUrl - ) - ) - ) - ) - ) + .transform(groupBy(diary).`as`(list(diaryImage.imageUrl))) + + return DiaryResponseParam.of(transform) } fun findAfterNowUsingCursor(param: DiaryListRequestParam): List { - return queryFactory + val transform: Map> = queryFactory .from(diary) - .innerJoin(diary.user) + .leftJoin(diary.diaryImages, diaryImage) .where( eqCouple(param.userId, param.partnerId), eqMemoryDateAndGtDiaryId(param.memoryDate, param.diaryId), @@ -68,69 +47,32 @@ class DiaryQueryRepository( ) .orderBy(ascMemoryDate(), ascDiaryId()) .limit(param.pageSize) - .transform( - groupBy(diary.id) - .list( - Projections.constructor( - DiaryResponseParam::class.java, - diary.id, - user.id, - diary.title, - diary.memoryDate, - diary.place, - diary.content, - list( - Projections.constructor( - String::class.java, - diaryImage.imageUrl - ) - ) - ) - ) - ) + .transform(groupBy(diary).`as`(list(diaryImage.imageUrl))) + + return DiaryResponseParam.of(transform) } - fun findAllByMemoryDate(param: DiarySimpleRequestParam): List { - return queryFactory - .select( - QDiarySimpleResponseParam( - diary.id, - diary.user.id, - diary.title, - diary.memoryDate, - diary.place, - diary.content, - diary.diaryImages.get(0).imageUrl - ) - ) + fun findAllByMemoryDate(param: DiarySimpleRequestParam): List { + val transform: Map> = queryFactory .from(diary) - .innerJoin(diary.user) + .leftJoin(diary.diaryImages, diaryImage) .where(eqCouple(param.userId, param.partnerId), eqMemoryDate(param.memoryDate)) .orderBy(ascDiaryId()) - .fetch() + .transform(groupBy(diary).`as`(list(diaryImage.imageUrl))) + + return DiaryResponseParam.of(transform) } - fun findAll(userId: Long, partnerId: Long?): List { - return queryFactory - .select( - QDiarySimpleResponseParam( - diary.id, - diary.user.id, - diary.title, - diary.memoryDate, - diary.place, - diary.content, - diary.diaryImages.get(0).imageUrl - ) - ) + fun findAll(userId: Long, partnerId: Long?): List { + val transform: Map> = queryFactory .from(diary) - .innerJoin(diary.user) + .leftJoin(diary.diaryImages, diaryImage) .where(eqCouple(userId, partnerId)) .orderBy(ascDiaryId()) - .fetch() - } + .transform(groupBy(diary).`as`(list(diaryImage.imageUrl))) - private fun eqDiary(diary: QDiary): BooleanExpression = diary.eq(diary) + return DiaryResponseParam.of(transform) + } private fun eqCouple(userId: Long, partnerId: Long?): BooleanExpression { val expression: BooleanExpression = eqUserId(userId) diff --git a/lovebird-domain/src/main/kotlin/com/lovebird/domain/repository/reader/DiaryReader.kt b/lovebird-domain/src/main/kotlin/com/lovebird/domain/repository/reader/DiaryReader.kt index b184f7c..d136893 100644 --- a/lovebird-domain/src/main/kotlin/com/lovebird/domain/repository/reader/DiaryReader.kt +++ b/lovebird-domain/src/main/kotlin/com/lovebird/domain/repository/reader/DiaryReader.kt @@ -6,7 +6,6 @@ import com.lovebird.domain.annotation.Reader import com.lovebird.domain.dto.query.DiaryListRequestParam import com.lovebird.domain.dto.query.DiaryResponseParam import com.lovebird.domain.dto.query.DiarySimpleRequestParam -import com.lovebird.domain.dto.query.DiarySimpleResponseParam import com.lovebird.domain.entity.Diary import com.lovebird.domain.repository.jpa.DiaryJpaRepository import com.lovebird.domain.repository.query.DiaryQueryRepository @@ -29,11 +28,11 @@ class DiaryReader( return diaryQueryRepository.findAfterNowUsingCursor(param) } - fun findAllByMemoryDate(param: DiarySimpleRequestParam): List { + fun findAllByMemoryDate(param: DiarySimpleRequestParam): List { return diaryQueryRepository.findAllByMemoryDate(param) } - fun findAll(userId: Long, partnerId: Long?): List { + fun findAll(userId: Long, partnerId: Long?): List { return diaryQueryRepository.findAll(userId, partnerId) } }