diff --git a/src/main/kotlin/com/restaurant/be/restaurant/domain/entity/Category.kt b/src/main/kotlin/com/restaurant/be/category/domain/entity/Category.kt similarity index 90% rename from src/main/kotlin/com/restaurant/be/restaurant/domain/entity/Category.kt rename to src/main/kotlin/com/restaurant/be/category/domain/entity/Category.kt index b84a1e1..963a698 100644 --- a/src/main/kotlin/com/restaurant/be/restaurant/domain/entity/Category.kt +++ b/src/main/kotlin/com/restaurant/be/category/domain/entity/Category.kt @@ -1,4 +1,4 @@ -package com.restaurant.be.restaurant.domain.entity +package com.restaurant.be.category.domain.entity import javax.persistence.Column import javax.persistence.Entity diff --git a/src/main/kotlin/com/restaurant/be/restaurant/domain/service/GetCategoryService.kt b/src/main/kotlin/com/restaurant/be/category/domain/service/GetCategoryService.kt similarity index 51% rename from src/main/kotlin/com/restaurant/be/restaurant/domain/service/GetCategoryService.kt rename to src/main/kotlin/com/restaurant/be/category/domain/service/GetCategoryService.kt index 71f1bc9..0ded16b 100644 --- a/src/main/kotlin/com/restaurant/be/restaurant/domain/service/GetCategoryService.kt +++ b/src/main/kotlin/com/restaurant/be/category/domain/service/GetCategoryService.kt @@ -1,8 +1,8 @@ -package com.restaurant.be.restaurant.domain.service +package com.restaurant.be.category.domain.service -import com.restaurant.be.restaurant.presentation.controller.dto.GetCategoryResponse -import com.restaurant.be.restaurant.presentation.controller.dto.common.CategoryDto -import com.restaurant.be.restaurant.repository.CategoryRepository +import com.restaurant.be.category.presentation.controller.dto.GetCategoriesResponse +import com.restaurant.be.category.presentation.controller.dto.common.CategoryDto +import com.restaurant.be.category.repository.CategoryRepository import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -11,10 +11,10 @@ class GetCategoryService( private val categoryRepository: CategoryRepository ) { @Transactional(readOnly = true) - fun getCategories(): GetCategoryResponse { + fun getCategories(): GetCategoriesResponse { val categories = categoryRepository.findAll() - return GetCategoryResponse( + return GetCategoriesResponse( categories = categories.map { CategoryDto(it.id ?: 0, it.name) } ) } diff --git a/src/main/kotlin/com/restaurant/be/restaurant/presentation/controller/GetCategoryController.kt b/src/main/kotlin/com/restaurant/be/category/presentation/controller/GetCategoryController.kt similarity index 78% rename from src/main/kotlin/com/restaurant/be/restaurant/presentation/controller/GetCategoryController.kt rename to src/main/kotlin/com/restaurant/be/category/presentation/controller/GetCategoryController.kt index d0198e7..bd3b409 100644 --- a/src/main/kotlin/com/restaurant/be/restaurant/presentation/controller/GetCategoryController.kt +++ b/src/main/kotlin/com/restaurant/be/category/presentation/controller/GetCategoryController.kt @@ -1,8 +1,8 @@ -package com.restaurant.be.restaurant.presentation.controller +package com.restaurant.be.category.presentation.controller +import com.restaurant.be.category.domain.service.GetCategoryService +import com.restaurant.be.category.presentation.controller.dto.GetCategoriesResponse import com.restaurant.be.common.response.CommonResponse -import com.restaurant.be.restaurant.domain.service.GetCategoryService -import com.restaurant.be.restaurant.presentation.controller.dto.GetCategoryResponse import io.swagger.annotations.Api import io.swagger.annotations.ApiOperation import io.swagger.v3.oas.annotations.media.Content @@ -26,9 +26,9 @@ class GetCategoryController( @ApiResponse( responseCode = "200", description = "성공", - content = [Content(schema = Schema(implementation = GetCategoryResponse::class))] + content = [Content(schema = Schema(implementation = GetCategoriesResponse::class))] ) - fun getCategory(): CommonResponse { + fun getCategories(): CommonResponse { val response = getCategoryService.getCategories() return CommonResponse.success(response) } diff --git a/src/main/kotlin/com/restaurant/be/restaurant/presentation/controller/dto/GetCategoryDto.kt b/src/main/kotlin/com/restaurant/be/category/presentation/controller/dto/GetCategoryDto.kt similarity index 52% rename from src/main/kotlin/com/restaurant/be/restaurant/presentation/controller/dto/GetCategoryDto.kt rename to src/main/kotlin/com/restaurant/be/category/presentation/controller/dto/GetCategoryDto.kt index 5eeb485..522f0c8 100644 --- a/src/main/kotlin/com/restaurant/be/restaurant/presentation/controller/dto/GetCategoryDto.kt +++ b/src/main/kotlin/com/restaurant/be/category/presentation/controller/dto/GetCategoryDto.kt @@ -1,11 +1,11 @@ @file:Suppress("ktlint", "MatchingDeclarationName") -package com.restaurant.be.restaurant.presentation.controller.dto +package com.restaurant.be.category.presentation.controller.dto -import com.restaurant.be.restaurant.presentation.controller.dto.common.CategoryDto +import com.restaurant.be.category.presentation.controller.dto.common.CategoryDto import io.swagger.v3.oas.annotations.media.Schema -data class GetCategoryResponse( +data class GetCategoriesResponse( @Schema(description = "카테고리 리스트") val categories: List ) diff --git a/src/main/kotlin/com/restaurant/be/restaurant/presentation/controller/dto/common/CategoryDto.kt b/src/main/kotlin/com/restaurant/be/category/presentation/controller/dto/common/CategoryDto.kt similarity index 74% rename from src/main/kotlin/com/restaurant/be/restaurant/presentation/controller/dto/common/CategoryDto.kt rename to src/main/kotlin/com/restaurant/be/category/presentation/controller/dto/common/CategoryDto.kt index bdd1e89..3124416 100644 --- a/src/main/kotlin/com/restaurant/be/restaurant/presentation/controller/dto/common/CategoryDto.kt +++ b/src/main/kotlin/com/restaurant/be/category/presentation/controller/dto/common/CategoryDto.kt @@ -1,4 +1,4 @@ -package com.restaurant.be.restaurant.presentation.controller.dto.common +package com.restaurant.be.category.presentation.controller.dto.common import io.swagger.v3.oas.annotations.media.Schema diff --git a/src/main/kotlin/com/restaurant/be/restaurant/repository/CategoryRepository.kt b/src/main/kotlin/com/restaurant/be/category/repository/CategoryRepository.kt similarity index 53% rename from src/main/kotlin/com/restaurant/be/restaurant/repository/CategoryRepository.kt rename to src/main/kotlin/com/restaurant/be/category/repository/CategoryRepository.kt index 831b6e7..6564e00 100644 --- a/src/main/kotlin/com/restaurant/be/restaurant/repository/CategoryRepository.kt +++ b/src/main/kotlin/com/restaurant/be/category/repository/CategoryRepository.kt @@ -1,6 +1,6 @@ -package com.restaurant.be.restaurant.repository +package com.restaurant.be.category.repository -import com.restaurant.be.restaurant.domain.entity.Category +import com.restaurant.be.category.domain.entity.Category import org.springframework.data.jpa.repository.JpaRepository interface CategoryRepository : JpaRepository diff --git a/src/main/kotlin/com/restaurant/be/restaurant/repository/RestaurantRepositoryCustomImpl.kt b/src/main/kotlin/com/restaurant/be/restaurant/repository/RestaurantRepositoryCustomImpl.kt index f2de473..d5676c1 100644 --- a/src/main/kotlin/com/restaurant/be/restaurant/repository/RestaurantRepositoryCustomImpl.kt +++ b/src/main/kotlin/com/restaurant/be/restaurant/repository/RestaurantRepositoryCustomImpl.kt @@ -2,7 +2,7 @@ package com.restaurant.be.restaurant.repository import com.querydsl.core.types.dsl.PathBuilderFactory import com.querydsl.jpa.impl.JPAQueryFactory -import com.restaurant.be.restaurant.domain.entity.QCategory.category +import com.restaurant.be.category.domain.entity.QCategory.category import com.restaurant.be.restaurant.domain.entity.QMenu.menu import com.restaurant.be.restaurant.domain.entity.QRestaurant.restaurant import com.restaurant.be.restaurant.domain.entity.QRestaurantCategory.restaurantCategory @@ -130,12 +130,8 @@ class RestaurantRepositoryCustomImpl( userId: Long, pageable: Pageable ): Page { - val orderSpecifier = if (pageable.sort.isSorted) { - emptyList() - } else { - val restaurantLikePath = PathBuilderFactory().create(RestaurantLike::class.java) - listOf(restaurantLikePath.getNumber("id", Long::class.java).desc()) - } + val restaurantLikePath = PathBuilderFactory().create(RestaurantLike::class.java) + val orderSpecifier = listOf(restaurantLikePath.getNumber("id", Long::class.java).desc()) val myLikeQuery = queryFactory .select(restaurantLike.restaurantId) diff --git a/src/main/kotlin/com/restaurant/be/restaurant/repository/dto/RestaurantProjectionDto.kt b/src/main/kotlin/com/restaurant/be/restaurant/repository/dto/RestaurantProjectionDto.kt index 557778b..2f47161 100644 --- a/src/main/kotlin/com/restaurant/be/restaurant/repository/dto/RestaurantProjectionDto.kt +++ b/src/main/kotlin/com/restaurant/be/restaurant/repository/dto/RestaurantProjectionDto.kt @@ -1,6 +1,6 @@ package com.restaurant.be.restaurant.repository.dto -import com.restaurant.be.restaurant.domain.entity.Category +import com.restaurant.be.category.domain.entity.Category import com.restaurant.be.restaurant.domain.entity.Menu import com.restaurant.be.restaurant.domain.entity.Restaurant import com.restaurant.be.restaurant.presentation.controller.dto.common.RestaurantDetailDto diff --git a/src/test/kotlin/com/restaurant/be/category/presentation/controller/GetCategoryControllerTest.kt b/src/test/kotlin/com/restaurant/be/category/presentation/controller/GetCategoryControllerTest.kt new file mode 100644 index 0000000..cfd52d7 --- /dev/null +++ b/src/test/kotlin/com/restaurant/be/category/presentation/controller/GetCategoryControllerTest.kt @@ -0,0 +1,85 @@ +package com.restaurant.be.category.presentation.controller + +import com.fasterxml.jackson.core.type.TypeReference +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.module.SimpleModule +import com.fasterxml.jackson.module.kotlin.KotlinModule +import com.restaurant.be.category.domain.entity.Category +import com.restaurant.be.category.presentation.controller.dto.GetCategoriesResponse +import com.restaurant.be.category.repository.CategoryRepository +import com.restaurant.be.common.CustomDescribeSpec +import com.restaurant.be.common.IntegrationTest +import com.restaurant.be.common.PageDeserializer +import com.restaurant.be.common.response.CommonResponse +import com.restaurant.be.common.util.setUpUser +import com.restaurant.be.restaurant.presentation.controller.dto.common.RestaurantDto +import com.restaurant.be.user.repository.UserRepository +import io.kotest.matchers.shouldBe +import org.springframework.data.domain.Page +import org.springframework.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get +import org.springframework.test.web.servlet.result.MockMvcResultMatchers +import org.springframework.transaction.annotation.Transactional +import java.nio.charset.Charset + +@IntegrationTest +@Transactional +class GetCategoryControllerTest( + private val mockMvc: MockMvc, + private val userRepository: UserRepository, + private val categoryRepository: CategoryRepository +) : CustomDescribeSpec() { + private val baseUrl = "/v1/restaurants/category" + private val objectMapper: ObjectMapper = ObjectMapper().registerModule(KotlinModule()).apply { + val module = SimpleModule() + module.addDeserializer(Page::class.java, PageDeserializer(RestaurantDto::class.java)) + this.registerModule(module) + } + + init { + beforeEach { + setUpUser("test@gmail.com", userRepository) + } + + describe("#getCategories basic test") { + it("when data saved should return categories") { + // given + categoryRepository.saveAll( + listOf( + Category(name = "한식"), + Category(name = "중식"), + Category(name = "일식"), + Category(name = "양식"), + Category(name = "분식") + ) + ) + + // when + val result = mockMvc.perform( + get(baseUrl) + ).also { + println(it.andReturn().response.contentAsString) + } + .andExpect(MockMvcResultMatchers.status().isOk) + .andExpect(MockMvcResultMatchers.jsonPath("$.result").value("SUCCESS")) + .andReturn() + + val responseContent = result.response.getContentAsString(Charset.forName("UTF-8")) + val responseType = + object : TypeReference>() {} + val actualResult: CommonResponse = objectMapper.readValue( + responseContent, + responseType + ) + + // then + actualResult.data!!.categories.size shouldBe 5 + actualResult.data!!.categories[0].name shouldBe "한식" + actualResult.data!!.categories[1].name shouldBe "중식" + actualResult.data!!.categories[2].name shouldBe "일식" + actualResult.data!!.categories[3].name shouldBe "양식" + actualResult.data!!.categories[4].name shouldBe "분식" + } + } + } +} diff --git a/src/test/kotlin/com/restaurant/be/restaurant/presentation/controller/GetRestaurantControllerTest.kt b/src/test/kotlin/com/restaurant/be/restaurant/presentation/controller/GetRestaurantControllerTest.kt index 2910ca9..e06cccf 100644 --- a/src/test/kotlin/com/restaurant/be/restaurant/presentation/controller/GetRestaurantControllerTest.kt +++ b/src/test/kotlin/com/restaurant/be/restaurant/presentation/controller/GetRestaurantControllerTest.kt @@ -4,6 +4,8 @@ import com.fasterxml.jackson.core.type.TypeReference import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.module.SimpleModule import com.fasterxml.jackson.module.kotlin.KotlinModule +import com.restaurant.be.category.domain.entity.Category +import com.restaurant.be.category.repository.CategoryRepository import com.restaurant.be.common.CustomDescribeSpec import com.restaurant.be.common.IntegrationTest import com.restaurant.be.common.PageDeserializer @@ -11,13 +13,11 @@ import com.restaurant.be.common.response.CommonResponse import com.restaurant.be.common.util.RestaurantDocument import com.restaurant.be.common.util.RestaurantUtil import com.restaurant.be.common.util.setUpUser -import com.restaurant.be.restaurant.domain.entity.Category import com.restaurant.be.restaurant.domain.entity.RestaurantCategory import com.restaurant.be.restaurant.domain.entity.RestaurantLike import com.restaurant.be.restaurant.presentation.controller.dto.GetRestaurantResponse import com.restaurant.be.restaurant.presentation.controller.dto.GetRestaurantsResponse import com.restaurant.be.restaurant.presentation.controller.dto.common.RestaurantDto -import com.restaurant.be.restaurant.repository.CategoryRepository import com.restaurant.be.restaurant.repository.RestaurantCategoryRepository import com.restaurant.be.restaurant.repository.RestaurantLikeRepository import com.restaurant.be.restaurant.repository.RestaurantRepository