From eb733db3a10c5b4f688cb2e604399f353a86b3ba Mon Sep 17 00:00:00 2001 From: Sangmin Lee Date: Sat, 23 Nov 2024 17:20:58 +0900 Subject: [PATCH] =?UTF-8?q?[DVK-102]=20refactor:=20=EC=A0=84=EC=9E=90?= =?UTF-8?q?=EC=B1=85=20=EB=AC=B8=EC=9D=98=20=ED=8E=98=EC=9D=B4=EC=A7=95=20?= =?UTF-8?q?=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 --- .../v1/controller/EbookInquiryController.kt | 23 +++++++++++-------- .../docs/EbookInquiryControllerDocs.kt | 11 +++------ ...EbookInquiryDto.kt => EbookInquiryView.kt} | 6 ++--- .../response/CreateEbookInquiryResponse.kt | 8 +++---- .../dto/response/GetEbookInquiriesResponse.kt | 8 +++---- .../response/ModifyEbookInquiryResponse.kt | 8 +++---- .../v1/repository/EbookInquiryRepository.kt | 1 + .../ebook/v1/service/EbookInquiryService.kt | 10 +++++--- .../v1/controller/TransactionController.kt | 10 ++++---- .../v1/controller/WishlistController.kt | 4 ++-- .../EbookInquiryCommentControllerTest.kt | 4 ++-- .../controller/EbookInquiryControllerTest.kt | 15 +++++++----- 12 files changed, 57 insertions(+), 51 deletions(-) rename src/main/kotlin/com/devooks/backend/ebook/v1/dto/{EbookInquiryDto.kt => EbookInquiryView.kt} (89%) diff --git a/src/main/kotlin/com/devooks/backend/ebook/v1/controller/EbookInquiryController.kt b/src/main/kotlin/com/devooks/backend/ebook/v1/controller/EbookInquiryController.kt index e444986..02cadae 100644 --- a/src/main/kotlin/com/devooks/backend/ebook/v1/controller/EbookInquiryController.kt +++ b/src/main/kotlin/com/devooks/backend/ebook/v1/controller/EbookInquiryController.kt @@ -2,8 +2,12 @@ package com.devooks.backend.ebook.v1.controller import com.devooks.backend.auth.v1.domain.Authorization import com.devooks.backend.auth.v1.service.TokenService +import com.devooks.backend.common.dto.PageResponse +import com.devooks.backend.common.dto.PageResponse.Companion.toResponse import com.devooks.backend.ebook.v1.controller.docs.EbookInquiryControllerDocs import com.devooks.backend.ebook.v1.domain.EbookInquiry +import com.devooks.backend.ebook.v1.dto.EbookInquiryView +import com.devooks.backend.ebook.v1.dto.EbookInquiryView.Companion.toEbookInquiryView import com.devooks.backend.ebook.v1.dto.command.CreateEbookInquiryCommand import com.devooks.backend.ebook.v1.dto.command.DeleteEbookInquiryCommand import com.devooks.backend.ebook.v1.dto.command.GetEbookInquiresCommand @@ -13,13 +17,12 @@ import com.devooks.backend.ebook.v1.dto.request.ModifyEbookInquiryRequest import com.devooks.backend.ebook.v1.dto.response.CreateEbookInquiryResponse import com.devooks.backend.ebook.v1.dto.response.CreateEbookInquiryResponse.Companion.toCreateEbookInquiryResponse import com.devooks.backend.ebook.v1.dto.response.DeleteEbookInquiryResponse -import com.devooks.backend.ebook.v1.dto.response.GetEbookInquiriesResponse -import com.devooks.backend.ebook.v1.dto.response.GetEbookInquiriesResponse.Companion.toGetEbookInquiriesResponse import com.devooks.backend.ebook.v1.dto.response.ModifyEbookInquiryResponse import com.devooks.backend.ebook.v1.dto.response.ModifyEbookInquiryResponse.Companion.toModifyEbookInquiryResponse import com.devooks.backend.ebook.v1.service.EbookInquiryEventService import com.devooks.backend.ebook.v1.service.EbookInquiryService import com.devooks.backend.ebook.v1.service.EbookService +import org.springframework.data.domain.Page import org.springframework.http.HttpHeaders.AUTHORIZATION import org.springframework.transaction.annotation.Transactional import org.springframework.web.bind.annotation.DeleteMapping @@ -60,22 +63,22 @@ class EbookInquiryController( @GetMapping override suspend fun getEbookInquiries( - @RequestParam(required = false, defaultValue = "") + @RequestParam ebookId: String, - @RequestParam(required = false, defaultValue = "") + @RequestParam page: String, - @RequestParam(required = false, defaultValue = "") + @RequestParam count: String, - ): GetEbookInquiriesResponse { + ): PageResponse { val command = GetEbookInquiresCommand(ebookId, page, count) - val ebookInquiryList: List = ebookInquiryService.get(command) - return ebookInquiryList.toGetEbookInquiriesResponse() + val ebookInquiryList: Page = ebookInquiryService.get(command) + return ebookInquiryList.map { it.toEbookInquiryView() }.toResponse() } @Transactional @PatchMapping("/{inquiryId}") override suspend fun modifyEbookInquiry( - @PathVariable(name = "inquiryId", required = false) + @PathVariable inquiryId: String, @RequestBody request: ModifyEbookInquiryRequest, @@ -91,7 +94,7 @@ class EbookInquiryController( @Transactional @DeleteMapping("/{inquiryId}") override suspend fun deleteEbookInquiry( - @PathVariable(name = "inquiryId", required = false) + @PathVariable inquiryId: String, @RequestHeader(AUTHORIZATION) authorization: String, diff --git a/src/main/kotlin/com/devooks/backend/ebook/v1/controller/docs/EbookInquiryControllerDocs.kt b/src/main/kotlin/com/devooks/backend/ebook/v1/controller/docs/EbookInquiryControllerDocs.kt index 6a7b43f..60ee141 100644 --- a/src/main/kotlin/com/devooks/backend/ebook/v1/controller/docs/EbookInquiryControllerDocs.kt +++ b/src/main/kotlin/com/devooks/backend/ebook/v1/controller/docs/EbookInquiryControllerDocs.kt @@ -1,11 +1,12 @@ package com.devooks.backend.ebook.v1.controller.docs +import com.devooks.backend.common.dto.PageResponse import com.devooks.backend.common.exception.ErrorResponse +import com.devooks.backend.ebook.v1.dto.EbookInquiryView import com.devooks.backend.ebook.v1.dto.request.CreateEbookInquiryRequest import com.devooks.backend.ebook.v1.dto.request.ModifyEbookInquiryRequest import com.devooks.backend.ebook.v1.dto.response.CreateEbookInquiryResponse import com.devooks.backend.ebook.v1.dto.response.DeleteEbookInquiryResponse -import com.devooks.backend.ebook.v1.dto.response.GetEbookInquiriesResponse import com.devooks.backend.ebook.v1.dto.response.ModifyEbookInquiryResponse import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.media.Content @@ -67,12 +68,6 @@ interface EbookInquiryControllerDocs { ApiResponse( responseCode = "200", description = "OK", - content = [ - Content( - mediaType = APPLICATION_JSON_VALUE, - schema = Schema(implementation = GetEbookInquiriesResponse::class) - ) - ] ), ApiResponse( responseCode = "400", @@ -97,7 +92,7 @@ interface EbookInquiryControllerDocs { page: String, @Schema(description = "개수", required = true, nullable = false) count: String, - ): GetEbookInquiriesResponse + ): PageResponse @Operation(summary = "전자책 문의 수정") @ApiResponses( diff --git a/src/main/kotlin/com/devooks/backend/ebook/v1/dto/EbookInquiryDto.kt b/src/main/kotlin/com/devooks/backend/ebook/v1/dto/EbookInquiryView.kt similarity index 89% rename from src/main/kotlin/com/devooks/backend/ebook/v1/dto/EbookInquiryDto.kt rename to src/main/kotlin/com/devooks/backend/ebook/v1/dto/EbookInquiryView.kt index b3bb23e..43ad6f8 100644 --- a/src/main/kotlin/com/devooks/backend/ebook/v1/dto/EbookInquiryDto.kt +++ b/src/main/kotlin/com/devooks/backend/ebook/v1/dto/EbookInquiryView.kt @@ -5,7 +5,7 @@ import io.swagger.v3.oas.annotations.media.Schema import java.time.Instant import java.util.* -data class EbookInquiryDto( +data class EbookInquiryView( @Schema(description = "전자책 문의 식별자") val id: UUID, @Schema(description = "내용") @@ -20,8 +20,8 @@ data class EbookInquiryDto( val modifiedDate: Instant, ) { companion object { - fun EbookInquiry.toDto() = - EbookInquiryDto( + fun EbookInquiry.toEbookInquiryView() = + EbookInquiryView( id = this.id, content = this.content, ebookId = this.ebookId, diff --git a/src/main/kotlin/com/devooks/backend/ebook/v1/dto/response/CreateEbookInquiryResponse.kt b/src/main/kotlin/com/devooks/backend/ebook/v1/dto/response/CreateEbookInquiryResponse.kt index 9cc81ae..0681fb1 100644 --- a/src/main/kotlin/com/devooks/backend/ebook/v1/dto/response/CreateEbookInquiryResponse.kt +++ b/src/main/kotlin/com/devooks/backend/ebook/v1/dto/response/CreateEbookInquiryResponse.kt @@ -1,14 +1,14 @@ package com.devooks.backend.ebook.v1.dto.response import com.devooks.backend.ebook.v1.domain.EbookInquiry -import com.devooks.backend.ebook.v1.dto.EbookInquiryDto -import com.devooks.backend.ebook.v1.dto.EbookInquiryDto.Companion.toDto +import com.devooks.backend.ebook.v1.dto.EbookInquiryView +import com.devooks.backend.ebook.v1.dto.EbookInquiryView.Companion.toEbookInquiryView data class CreateEbookInquiryResponse( - val ebookInquiry: EbookInquiryDto, + val ebookInquiry: EbookInquiryView, ) { companion object { fun EbookInquiry.toCreateEbookInquiryResponse() = - CreateEbookInquiryResponse(toDto()) + CreateEbookInquiryResponse(toEbookInquiryView()) } } diff --git a/src/main/kotlin/com/devooks/backend/ebook/v1/dto/response/GetEbookInquiriesResponse.kt b/src/main/kotlin/com/devooks/backend/ebook/v1/dto/response/GetEbookInquiriesResponse.kt index 5216602..a8b821b 100644 --- a/src/main/kotlin/com/devooks/backend/ebook/v1/dto/response/GetEbookInquiriesResponse.kt +++ b/src/main/kotlin/com/devooks/backend/ebook/v1/dto/response/GetEbookInquiriesResponse.kt @@ -1,14 +1,14 @@ package com.devooks.backend.ebook.v1.dto.response import com.devooks.backend.ebook.v1.domain.EbookInquiry -import com.devooks.backend.ebook.v1.dto.EbookInquiryDto -import com.devooks.backend.ebook.v1.dto.EbookInquiryDto.Companion.toDto +import com.devooks.backend.ebook.v1.dto.EbookInquiryView +import com.devooks.backend.ebook.v1.dto.EbookInquiryView.Companion.toEbookInquiryView data class GetEbookInquiriesResponse( - val ebookInquiryList: List, + val ebookInquiryList: List, ) { companion object { fun List.toGetEbookInquiriesResponse() = - GetEbookInquiriesResponse(map { it.toDto() }) + GetEbookInquiriesResponse(map { it.toEbookInquiryView() }) } } diff --git a/src/main/kotlin/com/devooks/backend/ebook/v1/dto/response/ModifyEbookInquiryResponse.kt b/src/main/kotlin/com/devooks/backend/ebook/v1/dto/response/ModifyEbookInquiryResponse.kt index 522efc9..15c5886 100644 --- a/src/main/kotlin/com/devooks/backend/ebook/v1/dto/response/ModifyEbookInquiryResponse.kt +++ b/src/main/kotlin/com/devooks/backend/ebook/v1/dto/response/ModifyEbookInquiryResponse.kt @@ -1,14 +1,14 @@ package com.devooks.backend.ebook.v1.dto.response import com.devooks.backend.ebook.v1.domain.EbookInquiry -import com.devooks.backend.ebook.v1.dto.EbookInquiryDto -import com.devooks.backend.ebook.v1.dto.EbookInquiryDto.Companion.toDto +import com.devooks.backend.ebook.v1.dto.EbookInquiryView +import com.devooks.backend.ebook.v1.dto.EbookInquiryView.Companion.toEbookInquiryView data class ModifyEbookInquiryResponse( - val ebookInquiry: EbookInquiryDto + val ebookInquiry: EbookInquiryView ) { companion object { fun EbookInquiry.toModifyEbookInquiryResponse() = - ModifyEbookInquiryResponse(toDto()) + ModifyEbookInquiryResponse(toEbookInquiryView()) } } diff --git a/src/main/kotlin/com/devooks/backend/ebook/v1/repository/EbookInquiryRepository.kt b/src/main/kotlin/com/devooks/backend/ebook/v1/repository/EbookInquiryRepository.kt index d5ce885..2f0c8a1 100644 --- a/src/main/kotlin/com/devooks/backend/ebook/v1/repository/EbookInquiryRepository.kt +++ b/src/main/kotlin/com/devooks/backend/ebook/v1/repository/EbookInquiryRepository.kt @@ -8,4 +8,5 @@ import org.springframework.data.repository.kotlin.CoroutineCrudRepository interface EbookInquiryRepository : CoroutineCrudRepository { suspend fun findAllByEbookId(ebookId: UUID, pageable: Pageable): Flow + suspend fun countByEbookId(ebookId: UUID): Long } diff --git a/src/main/kotlin/com/devooks/backend/ebook/v1/service/EbookInquiryService.kt b/src/main/kotlin/com/devooks/backend/ebook/v1/service/EbookInquiryService.kt index 55c8897..9e0ee17 100644 --- a/src/main/kotlin/com/devooks/backend/ebook/v1/service/EbookInquiryService.kt +++ b/src/main/kotlin/com/devooks/backend/ebook/v1/service/EbookInquiryService.kt @@ -13,6 +13,8 @@ import java.time.Instant import java.util.* import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.toList +import org.springframework.data.domain.Page +import org.springframework.data.domain.PageImpl import org.springframework.stereotype.Service @Service @@ -28,11 +30,13 @@ class EbookInquiryService( return ebookInquiryRepository.save(entity).toDomain() } - suspend fun get(command: GetEbookInquiresCommand): List = - ebookInquiryRepository + suspend fun get(command: GetEbookInquiresCommand): Page { + val ebookInquiries = ebookInquiryRepository .findAllByEbookId(command.ebookId, command.pageable) .map { it.toDomain() } - .toList() + val count = ebookInquiryRepository.countByEbookId(command.ebookId) + return PageImpl(ebookInquiries.toList(), command.pageable, count) + } suspend fun modify(command: ModifyEbookInquiryCommand): EbookInquiry = findById(command.inquiryId) diff --git a/src/main/kotlin/com/devooks/backend/transaciton/v1/controller/TransactionController.kt b/src/main/kotlin/com/devooks/backend/transaciton/v1/controller/TransactionController.kt index d3d2b15..eac2d05 100644 --- a/src/main/kotlin/com/devooks/backend/transaciton/v1/controller/TransactionController.kt +++ b/src/main/kotlin/com/devooks/backend/transaciton/v1/controller/TransactionController.kt @@ -51,9 +51,9 @@ class TransactionController( suspend fun getBuyHistories( @RequestParam(required = false, defaultValue = "") ebookTitle: String, - @RequestParam(required = false, defaultValue = "") + @RequestParam page: String, - @RequestParam(required = false, defaultValue = "") + @RequestParam count: String, @RequestHeader(AUTHORIZATION) authorization: String, @@ -65,9 +65,9 @@ class TransactionController( @GetMapping("/sell-histories") suspend fun getSellHistories( - @RequestParam(required = false, defaultValue = "") + @RequestParam page: String, - @RequestParam(required = false, defaultValue = "") + @RequestParam count: String, @RequestHeader(AUTHORIZATION) authorization: String, @@ -76,4 +76,4 @@ class TransactionController( val command = GetSellHistoriesCommand(page, count, requesterId) return transactionService.get(command).toGetSellHistoriesResponse() } -} \ No newline at end of file +} diff --git a/src/main/kotlin/com/devooks/backend/wishlist/v1/controller/WishlistController.kt b/src/main/kotlin/com/devooks/backend/wishlist/v1/controller/WishlistController.kt index acccba1..cf978e3 100644 --- a/src/main/kotlin/com/devooks/backend/wishlist/v1/controller/WishlistController.kt +++ b/src/main/kotlin/com/devooks/backend/wishlist/v1/controller/WishlistController.kt @@ -54,9 +54,9 @@ class WishlistController( override suspend fun getWishlist( @RequestParam(required = false, defaultValue = "") categoryIds: List, - @RequestParam(required = false, defaultValue = "") + @RequestParam page: String, - @RequestParam(required = false, defaultValue = "") + @RequestParam count: String, @RequestHeader(AUTHORIZATION) authorization: String, diff --git a/src/test/kotlin/com/devooks/backend/ebook/v1/controller/EbookInquiryCommentControllerTest.kt b/src/test/kotlin/com/devooks/backend/ebook/v1/controller/EbookInquiryCommentControllerTest.kt index 4ba55e2..e4ab3b9 100644 --- a/src/test/kotlin/com/devooks/backend/ebook/v1/controller/EbookInquiryCommentControllerTest.kt +++ b/src/test/kotlin/com/devooks/backend/ebook/v1/controller/EbookInquiryCommentControllerTest.kt @@ -9,7 +9,7 @@ import com.devooks.backend.common.dto.ImageDto import com.devooks.backend.config.IntegrationTest import com.devooks.backend.ebook.v1.dto.EbookImageDto import com.devooks.backend.ebook.v1.dto.EbookInquiryCommentDto -import com.devooks.backend.ebook.v1.dto.EbookInquiryDto +import com.devooks.backend.ebook.v1.dto.EbookInquiryView import com.devooks.backend.ebook.v1.dto.request.CreateEbookInquiryCommentRequest import com.devooks.backend.ebook.v1.dto.request.CreateEbookInquiryRequest import com.devooks.backend.ebook.v1.dto.request.CreateEbookRequest @@ -311,7 +311,7 @@ internal class EbookInquiryCommentControllerTest @Autowired constructor( return ebookInquiryComment } - private suspend fun EbookInquiryCommentControllerTest.postCreateEbookInquiry(): Pair { + private suspend fun EbookInquiryCommentControllerTest.postCreateEbookInquiry(): Pair { val (_, createEbookResponse) = postCreateEbook() val accessToken = tokenService.createTokenGroup(expectedMember1).accessToken val createEbookInquiryRequest = CreateEbookInquiryRequest( diff --git a/src/test/kotlin/com/devooks/backend/ebook/v1/controller/EbookInquiryControllerTest.kt b/src/test/kotlin/com/devooks/backend/ebook/v1/controller/EbookInquiryControllerTest.kt index faa64d2..922b983 100644 --- a/src/test/kotlin/com/devooks/backend/ebook/v1/controller/EbookInquiryControllerTest.kt +++ b/src/test/kotlin/com/devooks/backend/ebook/v1/controller/EbookInquiryControllerTest.kt @@ -6,9 +6,10 @@ import com.devooks.backend.auth.v1.domain.AccessToken import com.devooks.backend.auth.v1.service.TokenService import com.devooks.backend.category.v1.repository.CategoryRepository import com.devooks.backend.common.dto.ImageDto +import com.devooks.backend.common.dto.PageResponse import com.devooks.backend.config.IntegrationTest import com.devooks.backend.ebook.v1.dto.EbookImageDto -import com.devooks.backend.ebook.v1.dto.EbookInquiryDto +import com.devooks.backend.ebook.v1.dto.EbookInquiryView import com.devooks.backend.ebook.v1.dto.request.CreateEbookInquiryRequest import com.devooks.backend.ebook.v1.dto.request.CreateEbookRequest import com.devooks.backend.ebook.v1.dto.request.ModifyEbookInquiryRequest @@ -16,7 +17,6 @@ import com.devooks.backend.ebook.v1.dto.request.SaveDescriptionImagesRequest import com.devooks.backend.ebook.v1.dto.request.SaveMainImageRequest import com.devooks.backend.ebook.v1.dto.response.CreateEbookInquiryResponse import com.devooks.backend.ebook.v1.dto.response.CreateEbookResponse -import com.devooks.backend.ebook.v1.dto.response.GetEbookInquiriesResponse import com.devooks.backend.ebook.v1.dto.response.ModifyEbookInquiryResponse import com.devooks.backend.ebook.v1.dto.response.SaveDescriptionImagesResponse import com.devooks.backend.ebook.v1.dto.response.SaveMainImageResponse @@ -149,18 +149,21 @@ internal class EbookInquiryControllerTest @Autowired constructor( fun `전자책 문의를 조회할 수 있다`(): Unit = runBlocking { val createdEbookInquiry = postCreateEbookInquiry() - val foundEbookInquiry = + val pageEbookInquiry = webTestClient .get() .uri("/api/v1/ebook-inquiries?ebookId=${createdEbookInquiry.ebookId}&page=1&count=10") .accept(APPLICATION_JSON) .exchange() .expectStatus().isOk - .expectBody() + .expectBody>() .returnResult() .responseBody!! - .ebookInquiryList[0] + val foundEbookInquiry = pageEbookInquiry.data[0] + + assertThat(pageEbookInquiry.pageable.totalPages).isEqualTo(1) + assertThat(pageEbookInquiry.pageable.totalElements).isEqualTo(1) assertThat(foundEbookInquiry.id).isEqualTo(createdEbookInquiry.id) assertThat(foundEbookInquiry.content).isEqualTo(createdEbookInquiry.content) assertThat(foundEbookInquiry.ebookId).isEqualTo(createdEbookInquiry.ebookId) @@ -288,7 +291,7 @@ internal class EbookInquiryControllerTest @Autowired constructor( .expectStatus().isNotFound } - private suspend fun EbookInquiryControllerTest.postCreateEbookInquiry(): EbookInquiryDto { + private suspend fun EbookInquiryControllerTest.postCreateEbookInquiry(): EbookInquiryView { val (_, createEbookResponse) = postCreateEbook() val accessToken = tokenService.createTokenGroup(expectedMember1).accessToken val createEbookInquiryRequest = CreateEbookInquiryRequest(