Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix | #81 | @YongsHub | v1.0.1 출시 위해 Query Transform 리팩토링 #88

Merged
merged 10 commits into from
Feb 29, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -39,14 +39,14 @@ class DiaryController(
fun findAllByMemoryDate(
@AuthorizedUser user: User,
@ModelAttribute request: DiaryListRequest.SearchByMemoryDateRequest
): ApiResponse<DiarySimpleListResponse> {
): ApiResponse<DiarySimpleResponse> {
return ApiResponse.success(diaryService.findAllByMemoryDate(request, user))
}

@GetMapping
fun findAll(
@AuthorizedUser user: User
): ApiResponse<DiarySimpleListResponse> {
): ApiResponse<DiarySimpleResponse> {
return ApiResponse.success(diaryService.findAll(user))
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.lovebird.api.dto.response.diary

import com.lovebird.domain.dto.query.DiaryResponseParam

data class DiarySimpleResponse(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

갑자기 네이밍을 바꾸신 특별한 이유가 있으실까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DiaryResponse와 DiarySimpleResponse의 return value들이 기존에 달랐던걸 인지하지 못하고 통합해버려서 원래대로 다시 분리했습니다!

val diaries: List<DiaryResponseParam>,
val totalCount: Int = diaries.size
) {
companion object {
fun of(diaries: List<DiaryResponseParam>): DiarySimpleResponse {
return DiarySimpleResponse(diaries)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<DiarySimpleResponseParam> = diaryReader.findAll(user.id!!, partner?.id)

val diaries: List<DiaryResponseParam> = 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<DiarySimpleResponseParam> = diaryReader.findAllByMemoryDate(request.toParam(user.id!!, partner?.id))
val diaries: List<DiaryResponseParam> = diaryReader.findAllByMemoryDate(request.toParam(user.id!!, partner?.id))

decryptDiariesOfSimple(diaries)

return DiarySimpleListResponse.of(diaries)
return DiarySimpleResponse.of(diaries)
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -18,7 +17,7 @@ object DiaryUtils {
}
}

fun decryptDiariesOfSimple(diaries: List<DiarySimpleResponseParam>) {
fun decryptDiariesOfSimple(diaries: List<DiaryResponseParam>) {
diaries.forEach {
it.title = decryptString(it.title)
it.place = it.place?.let { place -> decryptString(place) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,9 +28,9 @@ import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
import org.springframework.http.MediaType
import org.springframework.restdocs.ManualRestDocumentation
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.request
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*
import org.springframework.restdocs.payload.FieldDescriptor
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
import org.springframework.web.context.WebApplicationContext
import java.time.LocalDate
import java.time.format.DateTimeFormatter
Expand Down Expand Up @@ -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
Expand All @@ -80,7 +80,7 @@ class DiaryControllerTest(
"data.diaries" type ARRAY means "다이어리 목록",
"data.totalCount" type NUMBER means "캘린더 개수"
)
.andWithPrefix("data.diaries[].", getSimpleDiaryDetailResponseSnippet())
.andWithPrefix("data.diaries[].", getDiaryDetailResponseSnippet())
)
}
}
Expand Down Expand Up @@ -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
Expand All @@ -149,7 +149,7 @@ class DiaryControllerTest(
"data.diaries" type ARRAY means "다이어리 목록",
"data.totalCount" type NUMBER means "캘린더 개수"
)
.andWithPrefix("data.diaries[].", getSimpleDiaryDetailResponseSnippet())
.andWithPrefix("data.diaries[].", getDiaryDetailResponseSnippet())
)
}
}
Expand Down Expand Up @@ -268,18 +268,6 @@ class DiaryControllerTest(
}
}) {
companion object {
fun getSimpleDiaryDetailResponseSnippet(): List<FieldDescriptor> {
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<FieldDescriptor> {
return responseDiaryDetailResponse(
"diaryId" type NUMBER means "다이어리 아이디",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -257,7 +257,7 @@ class DiaryServiceTest : ServiceDescribeSpec({

// 상태 검증
diaryService.findAllByMemoryDate(request, user)
.shouldBeEqualToComparingFields(DiarySimpleListResponse.of(diaries))
.shouldBeEqualToComparingFields(DiarySimpleResponse.of(diaries))

// 행위 검증
verify(exactly = 1) {
Expand All @@ -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
Expand All @@ -278,7 +278,7 @@ class DiaryServiceTest : ServiceDescribeSpec({

// 상태 검증
diaryService.findAllByMemoryDate(request, user)
.shouldBeEqualToComparingFields(DiarySimpleListResponse.of(diaries))
.shouldBeEqualToComparingFields(DiarySimpleResponse.of(diaries))

// 행위 검증
verify(exactly = 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -125,19 +124,19 @@ object DiaryTestFixture {
)
}

fun getDiarySimpleResponseList(user: User, partner: User?, size: Int): List<DiarySimpleResponseParam> {
val diaries = arrayListOf<DiarySimpleResponseParam>()
fun getDiaryResponseList(user: User, partner: User?, size: Int): List<DiaryResponseParam> {
val diaries = arrayListOf<DiaryResponseParam>()

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")
)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,6 +14,6 @@ class QuerydslConfig {

@Bean
fun queryFactory(): JPAQueryFactory {
return JPAQueryFactory(entityManager)
return JPAQueryFactory(JPQLTemplates.DEFAULT, entityManager)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lovebird.domain.dto.query

import com.lovebird.domain.entity.Diary
import java.time.LocalDate

data class DiaryResponseParam(
Expand All @@ -9,5 +10,23 @@ data class DiaryResponseParam(
val memoryDate: LocalDate,
var place: String?,
var content: String?,
val imageUrls: List<String>
)
val imageUrls: List<String>?
) {

companion object {
fun of(transform: Map<Diary, List<String>>): List<DiaryResponseParam> {
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()
}
}
}

This file was deleted.

Loading
Loading