From 7ec2621c6744bccba118f3fd521c25ba3b28b83d Mon Sep 17 00:00:00 2001 From: pricelees Date: Tue, 22 Jul 2025 13:40:04 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20theme=20=ED=8C=A8=ED=82=A4=EC=A7=80?= =?UTF-8?q?=EC=97=90=EC=84=9C=EC=9D=98=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EC=BB=A8=EB=B2=A4=EC=85=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/roomescape/theme/business/ThemeService.kt | 10 +++++----- src/main/java/roomescape/theme/docs/ThemeAPI.kt | 8 ++++---- .../infrastructure/persistence/ThemeRepository.kt | 3 +-- .../java/roomescape/theme/web/ThemeController.kt | 14 +++++++------- .../roomescape/theme/business/ThemeServiceTest.kt | 6 +++--- .../persistence/ThemeRepositoryTest.kt | 12 ++++++------ .../roomescape/theme/web/ThemeControllerTest.kt | 2 +- 7 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/main/java/roomescape/theme/business/ThemeService.kt b/src/main/java/roomescape/theme/business/ThemeService.kt index 1bb1199e..82ddf48c 100644 --- a/src/main/java/roomescape/theme/business/ThemeService.kt +++ b/src/main/java/roomescape/theme/business/ThemeService.kt @@ -19,7 +19,7 @@ class ThemeService( private val themeRepository: ThemeRepository ) { @Transactional(readOnly = true) - fun findThemeById(id: Long): ThemeEntity = themeRepository.findByIdOrNull(id) + fun findById(id: Long): ThemeEntity = themeRepository.findByIdOrNull(id) ?: throw RoomescapeException( ErrorType.THEME_NOT_FOUND, "[themeId: $id]", @@ -27,22 +27,22 @@ class ThemeService( ) @Transactional(readOnly = true) - fun findAllThemes(): ThemesResponse = themeRepository.findAll() + fun findAll(): ThemesResponse = themeRepository.findAll() .toResponse() @Transactional(readOnly = true) - fun getMostReservedThemesByCount(count: Int): ThemesResponse { + fun findMostReservedThemes(count: Int): ThemesResponse { val today = LocalDate.now() val startDate = today.minusDays(7) val endDate = today.minusDays(1) - return themeRepository.findTopNThemeBetweenStartDateAndEndDate(startDate, endDate, count) + return themeRepository.findPopularThemes(startDate, endDate, count) .toResponse() } @Transactional - fun save(request: ThemeRequest): ThemeResponse { + fun create(request: ThemeRequest): ThemeResponse { if (themeRepository.existsByName(request.name)) { throw RoomescapeException( ErrorType.THEME_DUPLICATED, diff --git a/src/main/java/roomescape/theme/docs/ThemeAPI.kt b/src/main/java/roomescape/theme/docs/ThemeAPI.kt index 624d830a..60751c4f 100644 --- a/src/main/java/roomescape/theme/docs/ThemeAPI.kt +++ b/src/main/java/roomescape/theme/docs/ThemeAPI.kt @@ -23,11 +23,11 @@ interface ThemeAPI { @LoginRequired @Operation(summary = "모든 테마 조회", description = "모든 테마를 조회합니다.", tags = ["로그인이 필요한 API"]) @ApiResponses(ApiResponse(responseCode = "200", description = "성공", useReturnTypeSchema = true)) - fun getAllThemes(): ResponseEntity> + fun findAll(): ResponseEntity> @Operation(summary = "가장 많이 예약된 테마 조회") @ApiResponses(ApiResponse(responseCode = "200", description = "성공", useReturnTypeSchema = true)) - fun getMostReservedThemes( + fun findMostReservedThemes( @RequestParam(defaultValue = "10") @Parameter(description = "최대로 조회할 테마 갯수") count: Int ): ResponseEntity> @@ -36,7 +36,7 @@ interface ThemeAPI { @ApiResponses( ApiResponse(responseCode = "201", description = "성공", useReturnTypeSchema = true), ) - fun saveTheme( + fun create( @Valid @RequestBody request: ThemeRequest, ): ResponseEntity> @@ -45,7 +45,7 @@ interface ThemeAPI { @ApiResponses( ApiResponse(responseCode = "204", description = "성공", useReturnTypeSchema = true), ) - fun removeTheme( + fun deleteById( @PathVariable id: Long ): ResponseEntity> } diff --git a/src/main/java/roomescape/theme/infrastructure/persistence/ThemeRepository.kt b/src/main/java/roomescape/theme/infrastructure/persistence/ThemeRepository.kt index 83b02cfe..baf1bd62 100644 --- a/src/main/java/roomescape/theme/infrastructure/persistence/ThemeRepository.kt +++ b/src/main/java/roomescape/theme/infrastructure/persistence/ThemeRepository.kt @@ -14,10 +14,9 @@ interface ThemeRepository : JpaRepository { GROUP BY r.theme.id ORDER BY COUNT(r.theme.id) DESC, t.id ASC LIMIT :limit - """ ) - fun findTopNThemeBetweenStartDateAndEndDate(startDate: LocalDate, endDate: LocalDate, limit: Int): List + fun findPopularThemes(startDate: LocalDate, endDate: LocalDate, limit: Int): List fun existsByName(name: String): Boolean diff --git a/src/main/java/roomescape/theme/web/ThemeController.kt b/src/main/java/roomescape/theme/web/ThemeController.kt index 1a7a7ec1..0c4f65fa 100644 --- a/src/main/java/roomescape/theme/web/ThemeController.kt +++ b/src/main/java/roomescape/theme/web/ThemeController.kt @@ -15,33 +15,33 @@ class ThemeController( ) : ThemeAPI { @GetMapping("/themes") - override fun getAllThemes(): ResponseEntity> { - val response: ThemesResponse = themeService.findAllThemes() + override fun findAll(): ResponseEntity> { + val response: ThemesResponse = themeService.findAll() return ResponseEntity.ok(CommonApiResponse(response)) } @GetMapping("/themes/most-reserved-last-week") - override fun getMostReservedThemes( + override fun findMostReservedThemes( @RequestParam(defaultValue = "10") @Parameter(description = "최대로 조회할 테마 갯수") count: Int ): ResponseEntity> { - val response: ThemesResponse = themeService.getMostReservedThemesByCount(count) + val response: ThemesResponse = themeService.findMostReservedThemes(count) return ResponseEntity.ok(CommonApiResponse(response)) } @PostMapping("/themes") - override fun saveTheme( + override fun create( @RequestBody @Valid request: ThemeRequest ): ResponseEntity> { - val themeResponse: ThemeResponse = themeService.save(request) + val themeResponse: ThemeResponse = themeService.create(request) return ResponseEntity.created(URI.create("/themes/${themeResponse.id}")) .body(CommonApiResponse(themeResponse)) } @DeleteMapping("/themes/{id}") - override fun removeTheme( + override fun deleteById( @PathVariable id: Long ): ResponseEntity> { themeService.deleteById(id) diff --git a/src/test/java/roomescape/theme/business/ThemeServiceTest.kt b/src/test/java/roomescape/theme/business/ThemeServiceTest.kt index a244273c..4c29b180 100644 --- a/src/test/java/roomescape/theme/business/ThemeServiceTest.kt +++ b/src/test/java/roomescape/theme/business/ThemeServiceTest.kt @@ -37,7 +37,7 @@ class ThemeServiceTest : FunSpec({ } returns null val exception = shouldThrow { - themeService.findThemeById(themeId) + themeService.findById(themeId) } exception.errorType shouldBe ErrorType.THEME_NOT_FOUND @@ -51,7 +51,7 @@ class ThemeServiceTest : FunSpec({ themeRepository.findAll() } returns themes - assertSoftly(themeService.findAllThemes()) { + assertSoftly(themeService.findAll()) { this.themes.size shouldBe themes.size this.themes[0].name shouldBe "t1" this.themes[1].name shouldBe "t2" @@ -68,7 +68,7 @@ class ThemeServiceTest : FunSpec({ } returns true val exception = shouldThrow { - themeService.save(ThemeRequest( + themeService.create(ThemeRequest( name = name, description = "Description", thumbnail = "http://example.com/thumbnail.jpg" diff --git a/src/test/java/roomescape/theme/infrastructure/persistence/ThemeRepositoryTest.kt b/src/test/java/roomescape/theme/infrastructure/persistence/ThemeRepositoryTest.kt index 73df4b11..df2ba71b 100644 --- a/src/test/java/roomescape/theme/infrastructure/persistence/ThemeRepositoryTest.kt +++ b/src/test/java/roomescape/theme/infrastructure/persistence/ThemeRepositoryTest.kt @@ -28,7 +28,7 @@ class ThemeRepositoryTest( } test("지난 10일간 예약 수가 가장 많은 테마 5개를 조회한다.") { - themeRepository.findTopNThemeBetweenStartDateAndEndDate( + themeRepository.findPopularThemes( LocalDate.now().minusDays(10), LocalDate.now().minusDays(1), 5 @@ -41,7 +41,7 @@ class ThemeRepositoryTest( } test("8일 전부터 5일 전까지 예약 수가 가장 많은 테마 3개를 조회한다.") { - themeRepository.findTopNThemeBetweenStartDateAndEndDate( + themeRepository.findPopularThemes( LocalDate.now().minusDays(8), LocalDate.now().minusDays(5), 3 @@ -61,7 +61,7 @@ class ThemeRepositoryTest( date = LocalDate.now().minusDays(5), ) - themeRepository.findTopNThemeBetweenStartDateAndEndDate( + themeRepository.findPopularThemes( LocalDate.now().minusDays(6), LocalDate.now().minusDays(4), 5 @@ -74,7 +74,7 @@ class ThemeRepositoryTest( } test("입력된 갯수보다 조회된 갯수가 작으면, 조회된 갯수만큼 반환한다.") { - themeRepository.findTopNThemeBetweenStartDateAndEndDate( + themeRepository.findPopularThemes( LocalDate.now().minusDays(10), LocalDate.now().minusDays(6), 10 @@ -84,7 +84,7 @@ class ThemeRepositoryTest( } test("입력된 갯수보다 조회된 갯수가 많으면, 입력된 갯수만큼 반환한다.") { - themeRepository.findTopNThemeBetweenStartDateAndEndDate( + themeRepository.findPopularThemes( LocalDate.now().minusDays(10), LocalDate.now().minusDays(1), 15 @@ -94,7 +94,7 @@ class ThemeRepositoryTest( } test("입력된 날짜 범위에 예약된 테마가 없을 경우 빈 리스트를 반환한다.") { - themeRepository.findTopNThemeBetweenStartDateAndEndDate( + themeRepository.findPopularThemes( LocalDate.now().plusDays(1), LocalDate.now().plusDays(10), 5 diff --git a/src/test/java/roomescape/theme/web/ThemeControllerTest.kt b/src/test/java/roomescape/theme/web/ThemeControllerTest.kt index 46893438..ff678493 100644 --- a/src/test/java/roomescape/theme/web/ThemeControllerTest.kt +++ b/src/test/java/roomescape/theme/web/ThemeControllerTest.kt @@ -201,7 +201,7 @@ class ThemeControllerTest(mockMvc: MockMvc) : RoomescapeApiTest() { ) every { - themeService.save(request) + themeService.create(request) } returns ThemeResponse( id = theme.id!!, name = theme.name,