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

[DVK-102] refactor: 전자책 문의 페이징 리팩토링 #75

Merged
merged 2 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<EbookInquiryView> {
val command = GetEbookInquiresCommand(ebookId, page, count)
val ebookInquiryList: List<EbookInquiry> = ebookInquiryService.get(command)
return ebookInquiryList.toGetEbookInquiriesResponse()
val ebookInquiryList: Page<EbookInquiry> = 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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -97,7 +92,7 @@ interface EbookInquiryControllerDocs {
page: String,
@Schema(description = "개수", required = true, nullable = false)
count: String,
): GetEbookInquiriesResponse
): PageResponse<EbookInquiryView>

@Operation(summary = "전자책 문의 수정")
@ApiResponses(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "내용")
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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())
}
}
Original file line number Diff line number Diff line change
@@ -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<EbookInquiryDto>,
val ebookInquiryList: List<EbookInquiryView>,
) {
companion object {
fun List<EbookInquiry>.toGetEbookInquiriesResponse() =
GetEbookInquiriesResponse(map { it.toDto() })
GetEbookInquiriesResponse(map { it.toEbookInquiryView() })
}
}
Original file line number Diff line number Diff line change
@@ -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())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ import org.springframework.data.repository.kotlin.CoroutineCrudRepository

interface EbookInquiryRepository : CoroutineCrudRepository<EbookInquiryEntity, UUID> {
suspend fun findAllByEbookId(ebookId: UUID, pageable: Pageable): Flow<EbookInquiryEntity>
suspend fun countByEbookId(ebookId: UUID): Long
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,11 +30,13 @@ class EbookInquiryService(
return ebookInquiryRepository.save(entity).toDomain()
}

suspend fun get(command: GetEbookInquiresCommand): List<EbookInquiry> =
ebookInquiryRepository
suspend fun get(command: GetEbookInquiresCommand): Page<EbookInquiry> {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -76,4 +76,4 @@ class TransactionController(
val command = GetSellHistoriesCommand(page, count, requesterId)
return transactionService.get(command).toGetSellHistoriesResponse()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ class WishlistController(
override suspend fun getWishlist(
@RequestParam(required = false, defaultValue = "")
categoryIds: List<String>,
@RequestParam(required = false, defaultValue = "")
@RequestParam
page: String,
@RequestParam(required = false, defaultValue = "")
@RequestParam
count: String,
@RequestHeader(AUTHORIZATION)
authorization: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -311,7 +311,7 @@ internal class EbookInquiryCommentControllerTest @Autowired constructor(
return ebookInquiryComment
}

private suspend fun EbookInquiryCommentControllerTest.postCreateEbookInquiry(): Pair<AccessToken, EbookInquiryDto> {
private suspend fun EbookInquiryCommentControllerTest.postCreateEbookInquiry(): Pair<AccessToken, EbookInquiryView> {
val (_, createEbookResponse) = postCreateEbook()
val accessToken = tokenService.createTokenGroup(expectedMember1).accessToken
val createEbookInquiryRequest = CreateEbookInquiryRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ 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
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
Expand Down Expand Up @@ -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<GetEbookInquiriesResponse>()
.expectBody<PageResponse<EbookInquiryView>>()
.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)
Expand Down Expand Up @@ -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(
Expand Down
Loading