Skip to content

Commit e0d1399

Browse files
authored
Merge pull request #86 from LeeSM0518/DVK-108-refactoring-wishlist-paging
[DVK-108] refactor: 찜 페이징 리팩토링
2 parents bda43b4 + 1956db1 commit e0d1399

File tree

12 files changed

+79
-93
lines changed

12 files changed

+79
-93
lines changed

src/main/kotlin/com/devooks/backend/ebook/v1/dto/response/GetEbookInquiriesResponse.kt

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/main/kotlin/com/devooks/backend/ebook/v1/dto/response/GetEbookInquiryCommentsResponse.kt

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/main/kotlin/com/devooks/backend/review/v1/controller/docs/ReviewControllerDocs.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.devooks.backend.common.exception.ErrorResponse
55
import com.devooks.backend.review.v1.dto.CreateReviewRequest
66
import com.devooks.backend.review.v1.dto.CreateReviewResponse
77
import com.devooks.backend.review.v1.dto.DeleteReviewResponse
8-
import com.devooks.backend.review.v1.dto.GetReviewsResponse
98
import com.devooks.backend.review.v1.dto.ModifyReviewRequest
109
import com.devooks.backend.review.v1.dto.ModifyReviewResponse
1110
import com.devooks.backend.review.v1.dto.ReviewView

src/main/kotlin/com/devooks/backend/review/v1/dto/GetReviewsResponse.kt

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/main/kotlin/com/devooks/backend/wishlist/v1/controller/WishlistController.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.devooks.backend.wishlist.v1.controller
22

33
import com.devooks.backend.auth.v1.domain.Authorization
44
import com.devooks.backend.auth.v1.service.TokenService
5+
import com.devooks.backend.common.dto.PageResponse
6+
import com.devooks.backend.common.dto.PageResponse.Companion.toResponse
57
import com.devooks.backend.ebook.v1.domain.Ebook
68
import com.devooks.backend.ebook.v1.service.EbookService
79
import com.devooks.backend.wishlist.v1.domain.Wishlist
@@ -11,8 +13,8 @@ import com.devooks.backend.wishlist.v1.dto.CreateWishlistResponse
1113
import com.devooks.backend.wishlist.v1.dto.DeleteWishlistCommand
1214
import com.devooks.backend.wishlist.v1.dto.DeleteWishlistResponse
1315
import com.devooks.backend.wishlist.v1.dto.GetWishlistCommand
14-
import com.devooks.backend.wishlist.v1.dto.GetWishlistResponse
15-
import com.devooks.backend.wishlist.v1.dto.GetWishlistResponse.Companion.toResponse
16+
import com.devooks.backend.wishlist.v1.dto.WishlistView
17+
import com.devooks.backend.wishlist.v1.dto.WishlistView.Companion.toWishlistView
1618
import com.devooks.backend.wishlist.v1.service.WishlistService
1719
import java.util.*
1820
import org.springframework.http.HttpHeaders.AUTHORIZATION
@@ -33,7 +35,7 @@ class WishlistController(
3335
private val wishlistService: WishlistService,
3436
private val ebookService: EbookService,
3537
private val tokenService: TokenService,
36-
): WishlistControllerDocs {
38+
) : WishlistControllerDocs {
3739

3840
@Transactional
3941
@PostMapping
@@ -60,10 +62,11 @@ class WishlistController(
6062
count: String,
6163
@RequestHeader(AUTHORIZATION)
6264
authorization: String,
63-
): GetWishlistResponse {
65+
): PageResponse<WishlistView> {
6466
val memberId = tokenService.getMemberId(Authorization(authorization))
6567
val command = GetWishlistCommand(memberId, categoryIds, page, count)
66-
return wishlistService.get(command).toResponse()
68+
val wishlists = wishlistService.get(command)
69+
return wishlists.map { it.toWishlistView() }.toResponse()
6770
}
6871

6972
@Transactional

src/main/kotlin/com/devooks/backend/wishlist/v1/controller/WishlistControllerDocs.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.devooks.backend.wishlist.v1.controller
22

3+
import com.devooks.backend.common.dto.PageResponse
34
import com.devooks.backend.common.exception.ErrorResponse
45
import com.devooks.backend.wishlist.v1.dto.CreateWishlistRequest
56
import com.devooks.backend.wishlist.v1.dto.CreateWishlistResponse
67
import com.devooks.backend.wishlist.v1.dto.DeleteWishlistResponse
7-
import com.devooks.backend.wishlist.v1.dto.GetWishlistResponse
8+
import com.devooks.backend.wishlist.v1.dto.WishlistView
89
import io.swagger.v3.oas.annotations.Operation
910
import io.swagger.v3.oas.annotations.media.Content
1011
import io.swagger.v3.oas.annotations.media.Schema
@@ -76,12 +77,6 @@ interface WishlistControllerDocs {
7677
ApiResponse(
7778
responseCode = "200",
7879
description = "OK",
79-
content = [
80-
Content(
81-
mediaType = APPLICATION_JSON_VALUE,
82-
schema = Schema(implementation = GetWishlistResponse::class)
83-
)
84-
]
8580
),
8681
ApiResponse(
8782
responseCode = "400",
@@ -106,7 +101,7 @@ interface WishlistControllerDocs {
106101
count: String,
107102
@Schema(description = "액세스 토큰", required = true, nullable = false)
108103
authorization: String,
109-
): GetWishlistResponse
104+
): PageResponse<WishlistView>
110105

111106
@Operation(summary = "찜 취소")
112107
@ApiResponses(

src/main/kotlin/com/devooks/backend/wishlist/v1/dto/GetWishlistCommand.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ package com.devooks.backend.wishlist.v1.dto
33
import com.devooks.backend.common.dto.Paging
44
import com.devooks.backend.wishlist.v1.error.validateCategoryIds
55
import java.util.*
6-
import org.springframework.data.domain.Pageable
76

87
class GetWishlistCommand(
98
val memberId: UUID,
109
val categoryIds: List<UUID>?,
11-
private val pageable: Pageable,
10+
private val paging: Paging,
1211
) {
1312
constructor(
1413
memberId: UUID,
@@ -18,12 +17,14 @@ class GetWishlistCommand(
1817
) : this(
1918
memberId = memberId,
2019
categoryIds = categoryIds.takeIf { it.isNotEmpty() }?.validateCategoryIds(),
21-
pageable = Paging(page, count).value
20+
paging = Paging(page, count)
2221
)
2322

2423
val offset: Int
25-
get() = pageable.offset.toInt()
24+
get() = paging.offset
2625

2726
val limit: Int
28-
get() = pageable.pageSize
27+
get() = paging.limit
28+
29+
val pageable = paging.value
2930
}

src/main/kotlin/com/devooks/backend/wishlist/v1/dto/GetWishlistResponse.kt

Lines changed: 0 additions & 12 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
package com.devooks.backend.wishlist.v1.dto
22

3+
import com.devooks.backend.wishlist.v1.domain.Wishlist
34
import io.swagger.v3.oas.annotations.media.Schema
45
import java.util.*
56

6-
data class WishlistDto(
7+
data class WishlistView(
78
@Schema(description = "찜 식별자")
89
val id: UUID,
910
@Schema(description = "회원 식별자")
1011
val memberId: UUID,
1112
@Schema(description = "전자책 식별자")
1213
val ebookId: UUID,
13-
)
14+
) {
15+
companion object {
16+
fun Wishlist.toWishlistView() =
17+
WishlistView(
18+
id = this.id,
19+
memberId = this.memberId,
20+
ebookId = this.ebookId,
21+
)
22+
}
23+
}

src/main/kotlin/com/devooks/backend/wishlist/v1/repository/WishlistQueryRepository.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.devooks.backend.wishlist.v1.domain.Wishlist
88
import com.devooks.backend.wishlist.v1.dto.GetWishlistCommand
99
import kotlinx.coroutines.flow.Flow
1010
import kotlinx.coroutines.flow.map
11+
import org.jooq.impl.DSL
1112
import org.springframework.stereotype.Repository
1213

1314
@Repository
@@ -26,9 +27,20 @@ class WishlistQueryRepository : JooqR2dbcRepository() {
2627
.on(EBOOK.EBOOK_ID.eq(WISHLIST.EBOOK_ID))
2728
.join(RELATED_CATEGORY)
2829
.on(RELATED_CATEGORY.RELATED_CATEGORY_ID.eq(RELATED_CATEGORY.RELATED_CATEGORY_ID))
29-
).where(WISHLIST.MEMBER_ID.eq(command.memberId))
30-
.offset(command.offset)
31-
.limit(command.limit)
30+
).where(
31+
WISHLIST.MEMBER_ID.eq(command.memberId)
32+
).offset(command.offset).limit(command.limit)
3233
}.map { it.into(Wishlist::class.java) }
3334

35+
suspend fun countBy(command: GetWishlistCommand): Flow<Long> =
36+
query {
37+
select(
38+
DSL.count()
39+
).from(
40+
WISHLIST
41+
).where(
42+
WISHLIST.MEMBER_ID.eq(command.memberId)
43+
)
44+
}.map { it.into(Long::class.java) }
45+
3446
}

src/main/kotlin/com/devooks/backend/wishlist/v1/service/WishlistService.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import com.devooks.backend.wishlist.v1.entity.WishlistEntity
99
import com.devooks.backend.wishlist.v1.error.WishlistError
1010
import com.devooks.backend.wishlist.v1.repository.WishlistCrudRepository
1111
import com.devooks.backend.wishlist.v1.repository.WishlistQueryRepository
12+
import kotlinx.coroutines.flow.first
1213
import kotlinx.coroutines.flow.toList
14+
import org.springframework.data.domain.Page
15+
import org.springframework.data.domain.PageImpl
1316
import org.springframework.stereotype.Service
1417

1518
@Service
@@ -25,13 +28,20 @@ class WishlistService(
2528
return wishlistCrudRepository.save(wishlistEntity).toDomain()
2629
}
2730

28-
suspend fun get(command: GetWishlistCommand): List<Wishlist> =
29-
wishlistQueryRepository.findBy(command).toList()
31+
suspend fun get(command: GetWishlistCommand): Page<Wishlist> {
32+
val wishlists = wishlistQueryRepository.findBy(command)
33+
val count = wishlistQueryRepository.countBy(command)
34+
return PageImpl(wishlists.toList(), command.pageable, count.first())
35+
}
3036

3137
suspend fun delete(command: DeleteWishlistCommand) {
3238
wishlistCrudRepository
3339
.findById(command.wishlistId)
34-
?.also { if (it.memberId != command.memberId) { throw WishlistError.FORBIDDEN_DELETE_WISHLIST.exception } }
40+
?.also {
41+
if (it.memberId != command.memberId) {
42+
throw WishlistError.FORBIDDEN_DELETE_WISHLIST.exception
43+
}
44+
}
3545
?.also { wishlistCrudRepository.delete(it) }
3646
?: throw WishlistError.NOT_FOUND_WISHLIST.exception
3747
}

src/test/kotlin/com/devooks/backend/wishlist/v1/controller/WishlistControllerTest.kt

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.devooks.backend.auth.v1.domain.AccessToken
66
import com.devooks.backend.auth.v1.service.TokenService
77
import com.devooks.backend.category.v1.repository.CategoryRepository
88
import com.devooks.backend.common.dto.ImageDto
9+
import com.devooks.backend.common.dto.PageResponse
910
import com.devooks.backend.config.IntegrationTest
1011
import com.devooks.backend.ebook.v1.dto.EbookImageDto
1112
import com.devooks.backend.ebook.v1.dto.request.CreateEbookRequest
@@ -26,7 +27,7 @@ import com.devooks.backend.pdf.v1.repository.PdfRepository
2627
import com.devooks.backend.pdf.v1.repository.PreviewImageRepository
2728
import com.devooks.backend.wishlist.v1.dto.CreateWishlistRequest
2829
import com.devooks.backend.wishlist.v1.dto.CreateWishlistResponse
29-
import com.devooks.backend.wishlist.v1.dto.GetWishlistResponse
30+
import com.devooks.backend.wishlist.v1.dto.WishlistView
3031
import com.devooks.backend.wishlist.v1.repository.WishlistCrudRepository
3132
import io.netty.handler.codec.http.HttpResponseStatus.CONFLICT
3233
import java.io.File
@@ -160,21 +161,25 @@ internal class WishlistControllerTest @Autowired constructor(
160161

161162
val response = postCreateWishlist(createEbookResponse, accessToken)
162163

163-
val wishlist = webTestClient
164+
val pageWishlist = webTestClient
164165
.get()
165166
.uri("/api/v1/wishlist?page=1&count=10")
166167
.header(AUTHORIZATION, "Bearer $accessToken")
167168
.accept(APPLICATION_JSON)
168169
.exchange()
169170
.expectStatus().isOk
170-
.expectBody<GetWishlistResponse>()
171+
.expectBody<PageResponse<WishlistView>>()
171172
.returnResult()
172173
.responseBody!!
173-
.wishlist
174174

175-
assertThat(wishlist[0].id).isEqualTo(response.wishlistId)
176-
assertThat(wishlist[0].ebookId).isEqualTo(response.ebookId)
177-
assertThat(wishlist[0].memberId).isEqualTo(response.memberId)
175+
val wishlistViewList = pageWishlist.data
176+
177+
assertThat(pageWishlist.pageable.totalPages).isEqualTo(1)
178+
assertThat(pageWishlist.pageable.totalElements).isEqualTo(1)
179+
assertThat(wishlistViewList.size).isEqualTo(1)
180+
assertThat(wishlistViewList[0].id).isEqualTo(response.wishlistId)
181+
assertThat(wishlistViewList[0].ebookId).isEqualTo(response.ebookId)
182+
assertThat(wishlistViewList[0].memberId).isEqualTo(response.memberId)
178183
}
179184

180185
@Test
@@ -183,7 +188,7 @@ internal class WishlistControllerTest @Autowired constructor(
183188

184189
val response = postCreateWishlist(createEbookResponse, accessToken)
185190

186-
val wishlist = webTestClient
191+
val pageWishlist = webTestClient
187192
.get()
188193
.uri(
189194
"/api/v1/wishlist?page=1&count=10&" +
@@ -194,14 +199,18 @@ internal class WishlistControllerTest @Autowired constructor(
194199
.accept(APPLICATION_JSON)
195200
.exchange()
196201
.expectStatus().isOk
197-
.expectBody<GetWishlistResponse>()
202+
.expectBody<PageResponse<WishlistView>>()
198203
.returnResult()
199204
.responseBody!!
200-
.wishlist
201205

202-
assertThat(wishlist[0].id).isEqualTo(response.wishlistId)
203-
assertThat(wishlist[0].ebookId).isEqualTo(response.ebookId)
204-
assertThat(wishlist[0].memberId).isEqualTo(response.memberId)
206+
val wishlistViewList = pageWishlist.data
207+
208+
assertThat(pageWishlist.pageable.totalPages).isEqualTo(1)
209+
assertThat(pageWishlist.pageable.totalElements).isEqualTo(1)
210+
assertThat(wishlistViewList.size).isEqualTo(1)
211+
assertThat(wishlistViewList[0].id).isEqualTo(response.wishlistId)
212+
assertThat(wishlistViewList[0].ebookId).isEqualTo(response.ebookId)
213+
assertThat(wishlistViewList[0].memberId).isEqualTo(response.memberId)
205214
}
206215

207216
@Test

0 commit comments

Comments
 (0)