From 7d68b31331dce3ae56a18c286851a12fc8540e45 Mon Sep 17 00:00:00 2001 From: Ogu1208 Date: Tue, 2 Jul 2024 23:34:35 +0900 Subject: [PATCH 1/7] =?UTF-8?q?refactor:=20Mapper,=20RepositoryConfig=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../clothstar/order/entity/QOrderEntity.java | 68 --------------- .../common/config/RepositoryConfig.java | 59 ------------- .../product/controller/ProductController.java | 2 - .../repository/ProductMybatisRepository.java | 24 ------ .../ProductLineMybatisRepository.java | 24 ------ .../resources/mappers/ProductLineMapper.xml | 82 ------------------- src/main/resources/mappers/ProductMapper.xml | 37 --------- src/main/resources/mappers/categoryMapper.xml | 29 ------- .../product/service/ProductServiceTest.java | 1 - .../service/ProductLineServiceTest.java | 1 - 10 files changed, 327 deletions(-) delete mode 100644 src/main/generated/org/store/clothstar/order/entity/QOrderEntity.java delete mode 100644 src/main/java/org/store/clothstar/common/config/RepositoryConfig.java delete mode 100644 src/main/java/org/store/clothstar/product/repository/ProductMybatisRepository.java delete mode 100644 src/main/java/org/store/clothstar/productLine/repository/ProductLineMybatisRepository.java delete mode 100644 src/main/resources/mappers/ProductLineMapper.xml delete mode 100644 src/main/resources/mappers/ProductMapper.xml delete mode 100644 src/main/resources/mappers/categoryMapper.xml diff --git a/src/main/generated/org/store/clothstar/order/entity/QOrderEntity.java b/src/main/generated/org/store/clothstar/order/entity/QOrderEntity.java deleted file mode 100644 index 4daf95dc..00000000 --- a/src/main/generated/org/store/clothstar/order/entity/QOrderEntity.java +++ /dev/null @@ -1,68 +0,0 @@ -package org.store.clothstar.order.entity; - -import static com.querydsl.core.types.PathMetadataFactory.*; - -import com.querydsl.core.types.dsl.*; - -import com.querydsl.core.types.PathMetadata; -import javax.annotation.processing.Generated; -import com.querydsl.core.types.Path; -import com.querydsl.core.types.dsl.PathInits; - - -/** - * QOrderEntity is a Querydsl query type for OrderEntity - */ -@Generated("com.querydsl.codegen.DefaultEntitySerializer") -public class QOrderEntity extends EntityPathBase { - - private static final long serialVersionUID = 880822783L; - - private static final PathInits INITS = PathInits.DIRECT2; - - public static final QOrderEntity orderEntity = new QOrderEntity("orderEntity"); - - public final org.store.clothstar.member.entity.QAddressEntity address; - - public final DateTimePath createdAt = createDateTime("createdAt", java.time.LocalDateTime.class); - - public final org.store.clothstar.member.entity.QMemberEntity member; - - public final ListPath orderDetails = this.createList("orderDetails", org.store.clothstar.orderDetail.entity.OrderDetailEntity.class, org.store.clothstar.orderDetail.entity.QOrderDetailEntity.class, PathInits.DIRECT2); - - public final NumberPath orderId = createNumber("orderId", Long.class); - - public final EnumPath paymentMethod = createEnum("paymentMethod", org.store.clothstar.order.type.PaymentMethod.class); - - public final EnumPath status = createEnum("status", org.store.clothstar.order.type.Status.class); - - public final NumberPath totalPaymentPrice = createNumber("totalPaymentPrice", Integer.class); - - public final NumberPath totalProductsPrice = createNumber("totalProductsPrice", Integer.class); - - public final NumberPath totalShippingPrice = createNumber("totalShippingPrice", Integer.class); - - public QOrderEntity(String variable) { - this(OrderEntity.class, forVariable(variable), INITS); - } - - public QOrderEntity(Path path) { - this(path.getType(), path.getMetadata(), PathInits.getFor(path.getMetadata(), INITS)); - } - - public QOrderEntity(PathMetadata metadata) { - this(metadata, PathInits.getFor(metadata, INITS)); - } - - public QOrderEntity(PathMetadata metadata, PathInits inits) { - this(OrderEntity.class, metadata, inits); - } - - public QOrderEntity(Class type, PathMetadata metadata, PathInits inits) { - super(type, metadata, inits); - this.address = inits.isInitialized("address") ? new org.store.clothstar.member.entity.QAddressEntity(forProperty("address"), inits.get("address")) : null; - this.member = inits.isInitialized("member") ? new org.store.clothstar.member.entity.QMemberEntity(forProperty("member")) : null; - } - -} - diff --git a/src/main/java/org/store/clothstar/common/config/RepositoryConfig.java b/src/main/java/org/store/clothstar/common/config/RepositoryConfig.java deleted file mode 100644 index ef0d7ef0..00000000 --- a/src/main/java/org/store/clothstar/common/config/RepositoryConfig.java +++ /dev/null @@ -1,59 +0,0 @@ -package org.store.clothstar.common.config; - -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.mybatis.spring.SqlSessionTemplate; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Primary; -import org.store.clothstar.category.repository.CategoryJpaRepository; -import org.store.clothstar.member.repository.SellerRepository; -import org.store.clothstar.product.repository.ProductJPARepository; -import org.store.clothstar.product.repository.ProductJPARepositoryAdapter; -import org.store.clothstar.product.repository.ProductMybatisRepository; -import org.store.clothstar.product.repository.ProductRepository; -import org.store.clothstar.productLine.repository.ProductLineJPARepository; -import org.store.clothstar.productLine.repository.ProductLineMybatisRepository; -import org.store.clothstar.productLine.repository.ProductLineRepository; -import org.store.clothstar.productLine.repository.adapter.ProductLineJPARepositoryAdapter; - -@Slf4j -@Configuration -@RequiredArgsConstructor -public class RepositoryConfig { - - @Bean -// @Primary - @ConditionalOnProperty(name = "app.repository.type", havingValue = "jpa", matchIfMissing = true) - public ProductLineRepository jpaProductLineRepository(CategoryJpaRepository categoryJpaRepository, - SellerRepository sellerRepository, - ProductLineJPARepository productLineJPARepository, - ProductJPARepository productJPARepository) { - log.info("Configuring ProductLine JPA repository"); - return new ProductLineJPARepositoryAdapter(categoryJpaRepository, sellerRepository, productLineJPARepository, productJPARepository); - } - - @Bean - @Primary - @ConditionalOnProperty(name = "app.repository.type", havingValue = "mybatis") - public ProductLineRepository mybatisProductLineRepository(SqlSessionTemplate sqlSessionTemplate) { - log.info("Configuring ProductLine MyBatis repository"); - return sqlSessionTemplate.getMapper(ProductLineMybatisRepository.class); - } - @Bean -// @Primary - @ConditionalOnProperty(name = "app.repository.type", havingValue = "jpa", matchIfMissing = true) - public ProductRepository jpaProductRepository(ProductJPARepository productJPARepository, ProductLineJPARepository productLineJPARepository) { - log.info("Configuring Product JPA repository"); - return new ProductJPARepositoryAdapter(productJPARepository, productLineJPARepository); - } - - @Bean - @Primary - @ConditionalOnProperty(name = "app.repository.type", havingValue = "mybatis") - public ProductRepository mybatisProductRepository(SqlSessionTemplate sqlSessionTemplate) { - log.info("Configuring Product MyBatis repository"); - return sqlSessionTemplate.getMapper(ProductMybatisRepository.class); - } -} diff --git a/src/main/java/org/store/clothstar/product/controller/ProductController.java b/src/main/java/org/store/clothstar/product/controller/ProductController.java index 06f1cc19..8a09025c 100644 --- a/src/main/java/org/store/clothstar/product/controller/ProductController.java +++ b/src/main/java/org/store/clothstar/product/controller/ProductController.java @@ -12,7 +12,6 @@ import org.store.clothstar.product.dto.request.CreateProductRequest; import org.store.clothstar.product.dto.request.UpdateProductRequest; import org.store.clothstar.product.dto.response.ProductResponse; -import org.store.clothstar.product.repository.ProductMybatisRepository; import org.store.clothstar.product.service.ProductService; import java.net.URI; @@ -24,7 +23,6 @@ public class ProductController { private final ProductService productService; - private final ProductMybatisRepository productMybatisRepository; /* @Operation(summary = "전체 상품 옵션 조회", description = "상품 Id의 모든 상품 옵션을 조회한다.") diff --git a/src/main/java/org/store/clothstar/product/repository/ProductMybatisRepository.java b/src/main/java/org/store/clothstar/product/repository/ProductMybatisRepository.java deleted file mode 100644 index f69a8624..00000000 --- a/src/main/java/org/store/clothstar/product/repository/ProductMybatisRepository.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.store.clothstar.product.repository; - -import org.apache.ibatis.annotations.Mapper; -import org.store.clothstar.product.domain.Product; - -import java.util.List; -import java.util.Optional; - -@Mapper -public interface ProductMybatisRepository extends ProductRepository { - - List selectAllProductsById(Long productId); - - Optional selectByProductId(Long productId); - -// Optional selectProductLineWithOptions(Long productId); - - int save(Product product); - - int updateProduct(Product product); - - int deleteProduct(Long productId); - -} diff --git a/src/main/java/org/store/clothstar/productLine/repository/ProductLineMybatisRepository.java b/src/main/java/org/store/clothstar/productLine/repository/ProductLineMybatisRepository.java deleted file mode 100644 index e59253ff..00000000 --- a/src/main/java/org/store/clothstar/productLine/repository/ProductLineMybatisRepository.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.store.clothstar.productLine.repository; - -import org.apache.ibatis.annotations.Mapper; -import org.store.clothstar.productLine.domain.ProductLine; -import org.store.clothstar.productLine.dto.response.ProductLineWithProductsResponse; - -import java.util.List; -import java.util.Optional; - -@Mapper -public interface ProductLineMybatisRepository extends ProductLineRepository { - - List selectAllProductLinesNotDeleted(); - - Optional selectByProductLineId(Long productId); - - Optional selectProductLineWithOptions(Long productId); - - int save(ProductLine productLine); - - int updateProductLine(ProductLine productLine); - - int setDeletedAt(ProductLine productLine); -} \ No newline at end of file diff --git a/src/main/resources/mappers/ProductLineMapper.xml b/src/main/resources/mappers/ProductLineMapper.xml deleted file mode 100644 index b026e577..00000000 --- a/src/main/resources/mappers/ProductLineMapper.xml +++ /dev/null @@ -1,82 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - INSERT INTO product_line(member_id, category_id, name, content, price, total_stock, sale_count, status, - created_at) - VALUES (#{memberId}, #{categoryId}, #{name}, #{content}, #{price}, #{totalStock}, #{saleCount}, #{status}, - #{createdAt}) - - - update product_line set - name = #{name}, - content = #{content}, - price = #{price}, - modified_at = #{modifiedAt} - where product_line_id = #{productLineId} - - - update product_line set - deleted_at = #{deletedAt} - where product_line_id = #{productLineId} - - \ No newline at end of file diff --git a/src/main/resources/mappers/ProductMapper.xml b/src/main/resources/mappers/ProductMapper.xml deleted file mode 100644 index 155d743e..00000000 --- a/src/main/resources/mappers/ProductMapper.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - INSERT INTO product(product_line_id, name, extra_charge, stock) - VALUES (#{productLineId}, #{name}, #{extraCharge}, #{stock}) - - - update product set - name = #{name}, - extra_charge = #{extraCharge}, - stock = #{stock} - where product_id = #{productId} - - - delete from product - where product_id = #{productId} - - - \ No newline at end of file diff --git a/src/main/resources/mappers/categoryMapper.xml b/src/main/resources/mappers/categoryMapper.xml deleted file mode 100644 index a035a66f..00000000 --- a/src/main/resources/mappers/categoryMapper.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - INSERT INTO category(category_id, category_type) - VALUES (#{categoryId}, #{categoryType}) - - - update category set - category_type = #{categoryType} - where category_id = #{categoryId} - - \ No newline at end of file diff --git a/src/test/java/org/store/clothstar/product/service/ProductServiceTest.java b/src/test/java/org/store/clothstar/product/service/ProductServiceTest.java index a4c62bd7..26e17cf5 100644 --- a/src/test/java/org/store/clothstar/product/service/ProductServiceTest.java +++ b/src/test/java/org/store/clothstar/product/service/ProductServiceTest.java @@ -11,7 +11,6 @@ import org.store.clothstar.product.dto.request.CreateProductRequest; import org.store.clothstar.product.dto.request.UpdateProductRequest; import org.store.clothstar.product.dto.response.ProductResponse; -import org.store.clothstar.product.repository.ProductMybatisRepository; import org.store.clothstar.product.repository.ProductRepository; import java.util.Optional; diff --git a/src/test/java/org/store/clothstar/productLine/service/ProductLineServiceTest.java b/src/test/java/org/store/clothstar/productLine/service/ProductLineServiceTest.java index 1e0b991e..90d813b0 100644 --- a/src/test/java/org/store/clothstar/productLine/service/ProductLineServiceTest.java +++ b/src/test/java/org/store/clothstar/productLine/service/ProductLineServiceTest.java @@ -14,7 +14,6 @@ import org.store.clothstar.productLine.dto.request.UpdateProductLineRequest; import org.store.clothstar.productLine.dto.response.ProductLineResponse; import org.store.clothstar.productLine.dto.response.ProductLineWithProductsResponse; -import org.store.clothstar.productLine.repository.ProductLineMybatisRepository; import org.store.clothstar.productLine.repository.ProductLineRepository; import java.time.LocalDateTime; From f258fb4764d59bee1f70bacc018ea4da0add930a Mon Sep 17 00:00:00 2001 From: Ogu1208 Date: Tue, 2 Jul 2024 23:43:37 +0900 Subject: [PATCH 2/7] =?UTF-8?q?refactor:=20Category=20Mybatis=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../adapter/JpaCategoryRepositoryAdapter.java | 1 - .../MybatisCategoryRepositoryAdapter.java | 39 ------------------- .../repository/CategoryMybatisRepository.java | 18 --------- .../category/service/CategoryServiceTest.java | 4 +- 4 files changed, 2 insertions(+), 60 deletions(-) delete mode 100644 src/main/java/org/store/clothstar/category/adapter/MybatisCategoryRepositoryAdapter.java delete mode 100644 src/main/java/org/store/clothstar/category/repository/CategoryMybatisRepository.java diff --git a/src/main/java/org/store/clothstar/category/adapter/JpaCategoryRepositoryAdapter.java b/src/main/java/org/store/clothstar/category/adapter/JpaCategoryRepositoryAdapter.java index cb34904c..8e9b1f7a 100644 --- a/src/main/java/org/store/clothstar/category/adapter/JpaCategoryRepositoryAdapter.java +++ b/src/main/java/org/store/clothstar/category/adapter/JpaCategoryRepositoryAdapter.java @@ -11,7 +11,6 @@ import java.util.Optional; @Repository -@Profile("jpa") @RequiredArgsConstructor public class JpaCategoryRepositoryAdapter implements CategoryRepository { private final CategoryJpaRepository categoryJpaRepository; diff --git a/src/main/java/org/store/clothstar/category/adapter/MybatisCategoryRepositoryAdapter.java b/src/main/java/org/store/clothstar/category/adapter/MybatisCategoryRepositoryAdapter.java deleted file mode 100644 index e391a648..00000000 --- a/src/main/java/org/store/clothstar/category/adapter/MybatisCategoryRepositoryAdapter.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.store.clothstar.category.adapter; - -import lombok.RequiredArgsConstructor; -import org.springframework.context.annotation.Profile; -import org.springframework.stereotype.Repository; -import org.store.clothstar.category.domain.Category; -import org.store.clothstar.category.repository.CategoryMybatisRepository; -import org.store.clothstar.category.repository.CategoryRepository; - -import java.util.List; -import java.util.Optional; - -@Repository -@Profile("mybatis") -@RequiredArgsConstructor -public class MybatisCategoryRepositoryAdapter implements CategoryRepository { - - private final CategoryMybatisRepository categoryMybatisRepository; - - @Override - public List selectAllCategory() { - return categoryMybatisRepository.selectAllCategory(); - } - - @Override - public Optional selectCategoryById(Long categoryId) { - return categoryMybatisRepository.selectCategoryById(categoryId); - } - - @Override - public int save(Category category) { - return categoryMybatisRepository.save(category); - } - - @Override - public int updateCategory(Category category) { - return categoryMybatisRepository.updateCategory(category); - } -} diff --git a/src/main/java/org/store/clothstar/category/repository/CategoryMybatisRepository.java b/src/main/java/org/store/clothstar/category/repository/CategoryMybatisRepository.java deleted file mode 100644 index d39a3991..00000000 --- a/src/main/java/org/store/clothstar/category/repository/CategoryMybatisRepository.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.store.clothstar.category.repository; - -import org.apache.ibatis.annotations.Mapper; -import org.store.clothstar.category.domain.Category; - -import java.util.List; -import java.util.Optional; - -@Mapper -public interface CategoryMybatisRepository extends CategoryRepository { - List selectAllCategory(); - - Optional selectCategoryById(Long categoryId); - - int save(Category category); - - int updateCategory(Category category); -} \ No newline at end of file diff --git a/src/test/java/org/store/clothstar/category/service/CategoryServiceTest.java b/src/test/java/org/store/clothstar/category/service/CategoryServiceTest.java index 676256c4..49588d0b 100644 --- a/src/test/java/org/store/clothstar/category/service/CategoryServiceTest.java +++ b/src/test/java/org/store/clothstar/category/service/CategoryServiceTest.java @@ -13,7 +13,7 @@ import org.store.clothstar.category.dto.request.CreateCategoryRequest; import org.store.clothstar.category.dto.response.CategoryDetailResponse; import org.store.clothstar.category.dto.response.CategoryResponse; -import org.store.clothstar.category.repository.CategoryMybatisRepository; +import org.store.clothstar.category.repository.CategoryJpaRepository; import java.util.ArrayList; import java.util.List; @@ -31,7 +31,7 @@ class CategoryServiceTest { private CategoryService categoryService; @Mock - private CategoryMybatisRepository categoryMybatisRepository; + private CategoryJpaRepository categoryMybatisRepository; @DisplayName("카테고리 리스트 조회에 성공한다.") @Test From 61b2d3b020666f339386fd7e29f6ed5eab8ead77 Mon Sep 17 00:00:00 2001 From: Ogu1208 Date: Tue, 2 Jul 2024 23:44:59 +0900 Subject: [PATCH 3/7] =?UTF-8?q?refactor:=20yml=EC=97=90=EC=84=9C=20Mybatis?= =?UTF-8?q?=20=EC=84=A4=EC=A0=95=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application-db.yml | 77 +-------------------------- src/main/resources/application.yml | 2 - 2 files changed, 1 insertion(+), 78 deletions(-) diff --git a/src/main/resources/application-db.yml b/src/main/resources/application-db.yml index 9bca0a19..2851e230 100644 --- a/src/main/resources/application-db.yml +++ b/src/main/resources/application-db.yml @@ -11,9 +11,6 @@ # init: # mode: always -app: - repository: - type: mybatis mybatis: mapper-locations: classpath:/mappers/**.xml config-location: classpath:/config/mybatis-config.xml @@ -28,47 +25,8 @@ jwt: accessTokenValidTimeMillis: 1200000 refreshTokenValidTimeMillis: 12000000 -#--- # local -#spring: -# config: -# activate: -# on-profile: "db-local" -# # jpa: -# # show-sql: true -# # database-platform: H2 -# # hibernate: -# # ddl-auto: create -# datasource: -# url: jdbc:h2:mem:localdb -# h2: -# console: -# enabled: true -#--- #dev -#spring: -# sql: -# init: -# platform: mysql -# config: -# activate: -# on-profile: "db-dev" -# thymeleaf: -# cache: false -# # jpa: -# # database-platform: org.hibernate.dialect.MySQLDialect -# jpa: -# #전송 쿼리 확인 -# show-sql: true -# properties: -# hibernate: -# format_sql: true -# datasource: -# driver-class-name: com.mysql.cj.jdbc.Driver -# url: ${DB_URL} -# username: ${DB_USERNAME} -# password: ${DB_PASSWORD} - --- # local 공통 설정 spring: config: @@ -81,21 +39,6 @@ spring: enabled: true thymeleaf: cache: false - ---- # local - MyBatis 활성화 -spring: - config: - activate: - on-profile: "db-local-mybatis" - mybatis: - mapper-locations: classpath:/mappers/**.xml - config-location: classpath:/config/mybatis-config.xml - ---- # local - JPA 활성화 -spring: - config: - activate: - on-profile: "db-local-jpa" jpa: show-sql: true database-platform: H2 @@ -103,8 +46,7 @@ spring: hibernate: format_sql: true default_batch_fetch_size: 100 -# hibernate: -# ddl-auto: create + --- # dev 공통 설정 jasypt: @@ -120,21 +62,6 @@ spring: url: ENC(tdT8e3e+B4Lnbjjca9DuylTI2WMxolkaTxUyT4yJKbFLHUV0YynNjMss5AsaQJxEyndmGmgUlYTuJhx6667fcyUxGiV43314vgEwX+/NaVaSjIa2wiAmMfZu3WPVlgp/) username: ENC(xhLiaQAS5lcTT2hgCMRDOw==) password: ENC(XzhtQOZIlb9QjMX3klXi1EMBAuVOqWrM) - ---- # dev - MyBatis 활성화 -spring: - config: - activate: - on-profile: "db-dev-mybatis" - mybatis: - mapper-locations: classpath:/mappers/**.xml - config-location: classpath:/config/mybatis-config.xml - ---- # dev - JPA 활성화 -spring: - config: - activate: - on-profile: "db-dev-jpa" jpa: show-sql: true database-platform: org.hibernate.dialect.MySQLDialect @@ -142,5 +69,3 @@ spring: hibernate: format_sql: true default_batch_fetch_size: 100 -# hibernate: -# ddl-auto: create \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 408a8a36..28aad418 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -5,10 +5,8 @@ spring: group: local: - db-local - - db-local-jpa # 또는 db-local-jpa dev: - db-dev - - db-dev-jpa # 또는 db-dev-jpa include: - db From 151f252291d68db2c150c1e3c318c6b5130127a7 Mon Sep 17 00:00:00 2001 From: Ogu1208 Date: Wed, 3 Jul 2024 01:41:49 +0900 Subject: [PATCH 4/7] =?UTF-8?q?refactor:=20JPAAdapter=20=EC=82=AD=EC=A0=9C?= =?UTF-8?q?,=20Service=EC=BD=94=EB=93=9C=20Repository=20=EC=9D=98=EC=A1=B4?= =?UTF-8?q?=EC=84=B1=20JPA=EB=A1=9C=20=EB=B3=80=EA=B2=BD,=20Service/Contro?= =?UTF-8?q?ller=20=EB=A1=9C=EC=A7=81,=20=EB=B0=98=ED=99=98=20=ED=83=80=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../adapter/JpaCategoryRepositoryAdapter.java | 38 -------- .../dto/request/CreateCategoryRequest.java | 6 +- .../dto/response/CategoryDetailResponse.java | 4 +- .../repository/CategoryJpaRepository.java | 3 +- .../repository/CategoryRepository.java | 17 ---- .../category/service/CategoryService.java | 18 ++-- .../dto/request/CreateProductRequest.java | 11 +++ .../product/dto/response/ProductResponse.java | 9 -- .../product/service/ProductService.java | 29 +++++-- .../ProductLineApplicationService.java | 44 ---------- .../controller/ProductLineController.java | 5 +- .../controller/ProductLineController_v2.java | 69 --------------- .../dto/request/CreateProductLineRequest.java | 4 +- .../dto/response/ProductLineResponse.java | 13 +++ .../ProductLineWithProductsJPAResponse.java | 10 ++- .../productLine/entity/ProductLineEntity.java | 4 +- .../service/ProductLineService.java | 66 +++++++------- .../service/ProductLineServiceV2.java | 86 ------------------- 18 files changed, 110 insertions(+), 326 deletions(-) delete mode 100644 src/main/java/org/store/clothstar/category/adapter/JpaCategoryRepositoryAdapter.java delete mode 100644 src/main/java/org/store/clothstar/category/repository/CategoryRepository.java delete mode 100644 src/main/java/org/store/clothstar/productLine/application/ProductLineApplicationService.java delete mode 100644 src/main/java/org/store/clothstar/productLine/controller/ProductLineController_v2.java delete mode 100644 src/main/java/org/store/clothstar/productLine/service/ProductLineServiceV2.java diff --git a/src/main/java/org/store/clothstar/category/adapter/JpaCategoryRepositoryAdapter.java b/src/main/java/org/store/clothstar/category/adapter/JpaCategoryRepositoryAdapter.java deleted file mode 100644 index 8e9b1f7a..00000000 --- a/src/main/java/org/store/clothstar/category/adapter/JpaCategoryRepositoryAdapter.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.store.clothstar.category.adapter; - -import lombok.RequiredArgsConstructor; -import org.springframework.context.annotation.Profile; -import org.springframework.stereotype.Repository; -import org.store.clothstar.category.domain.Category; -import org.store.clothstar.category.repository.CategoryJpaRepository; -import org.store.clothstar.category.repository.CategoryRepository; - -import java.util.List; -import java.util.Optional; - -@Repository -@RequiredArgsConstructor -public class JpaCategoryRepositoryAdapter implements CategoryRepository { - private final CategoryJpaRepository categoryJpaRepository; - - @Override - public List selectAllCategory() { - return categoryJpaRepository.findAll(); - } - - @Override - public Optional selectCategoryById(Long categoryId) { - return categoryJpaRepository.findById(categoryId); - } - - @Override - public int save(Category category) { - categoryJpaRepository.save(category); - return 1; - } - - @Override - public int updateCategory(Category category) { - return 0; - } -} diff --git a/src/main/java/org/store/clothstar/category/dto/request/CreateCategoryRequest.java b/src/main/java/org/store/clothstar/category/dto/request/CreateCategoryRequest.java index 1af11670..8403b338 100644 --- a/src/main/java/org/store/clothstar/category/dto/request/CreateCategoryRequest.java +++ b/src/main/java/org/store/clothstar/category/dto/request/CreateCategoryRequest.java @@ -6,7 +6,7 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import org.store.clothstar.category.domain.Category; +import org.store.clothstar.category.entity.CategoryEntity; @Getter @Builder @@ -18,8 +18,8 @@ public class CreateCategoryRequest { @NotBlank(message = "카테고리 타입을 입력해주세요.") private String categoryType; - public Category toCategory() { - return Category.builder() + public CategoryEntity toCategoryEntity() { + return CategoryEntity.builder() .categoryType(categoryType) .build(); } diff --git a/src/main/java/org/store/clothstar/category/dto/response/CategoryDetailResponse.java b/src/main/java/org/store/clothstar/category/dto/response/CategoryDetailResponse.java index 30e3f134..b3e8b8c1 100644 --- a/src/main/java/org/store/clothstar/category/dto/response/CategoryDetailResponse.java +++ b/src/main/java/org/store/clothstar/category/dto/response/CategoryDetailResponse.java @@ -2,7 +2,7 @@ import lombok.Builder; import lombok.Getter; -import org.store.clothstar.category.domain.Category; +import org.store.clothstar.category.entity.CategoryEntity; @Getter @Builder @@ -10,7 +10,7 @@ public class CategoryDetailResponse { private Long categoryId; private String categoryType; - public static CategoryDetailResponse from(Category category) { + public static CategoryDetailResponse from(CategoryEntity category) { return CategoryDetailResponse.builder() .categoryId(category.getCategoryId()) .categoryType(category.getCategoryType()) diff --git a/src/main/java/org/store/clothstar/category/repository/CategoryJpaRepository.java b/src/main/java/org/store/clothstar/category/repository/CategoryJpaRepository.java index a0662fd6..b152c417 100644 --- a/src/main/java/org/store/clothstar/category/repository/CategoryJpaRepository.java +++ b/src/main/java/org/store/clothstar/category/repository/CategoryJpaRepository.java @@ -3,8 +3,9 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import org.store.clothstar.category.domain.Category; +import org.store.clothstar.category.entity.CategoryEntity; @Repository -public interface CategoryJpaRepository extends JpaRepository { +public interface CategoryJpaRepository extends JpaRepository { } diff --git a/src/main/java/org/store/clothstar/category/repository/CategoryRepository.java b/src/main/java/org/store/clothstar/category/repository/CategoryRepository.java deleted file mode 100644 index 3583d842..00000000 --- a/src/main/java/org/store/clothstar/category/repository/CategoryRepository.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.store.clothstar.category.repository; - - -import org.store.clothstar.category.domain.Category; - -import java.util.List; -import java.util.Optional; - -public interface CategoryRepository { - List selectAllCategory(); - - Optional selectCategoryById(Long categoryId); - - int save(Category category); - - int updateCategory(Category category); -} diff --git a/src/main/java/org/store/clothstar/category/service/CategoryService.java b/src/main/java/org/store/clothstar/category/service/CategoryService.java index 4362f665..0056d8c1 100644 --- a/src/main/java/org/store/clothstar/category/service/CategoryService.java +++ b/src/main/java/org/store/clothstar/category/service/CategoryService.java @@ -10,7 +10,8 @@ import org.store.clothstar.category.dto.request.UpdateCategoryRequest; import org.store.clothstar.category.dto.response.CategoryDetailResponse; import org.store.clothstar.category.dto.response.CategoryResponse; -import org.store.clothstar.category.repository.CategoryRepository; +import org.store.clothstar.category.entity.CategoryEntity; +import org.store.clothstar.category.repository.CategoryJpaRepository; import java.util.List; import java.util.stream.Collectors; @@ -19,35 +20,36 @@ @RequiredArgsConstructor public class CategoryService { - private final CategoryRepository categoryRepository; + private final CategoryJpaRepository categoryRepository; @Transactional(readOnly = true) public List getAllCategories() { - return categoryRepository.selectAllCategory().stream() + return categoryRepository.findAll().stream() .map(CategoryResponse::from) .collect(Collectors.toList()); } @Transactional(readOnly = true) public CategoryDetailResponse getCategory(Long categoryId) { - Category category = categoryRepository.selectCategoryById(categoryId) + CategoryEntity category = categoryRepository.findById(categoryId) .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "해당 카테고리를 찾을 수 없습니다.")); + return CategoryDetailResponse.from(category); } @Transactional public Long createCategory(CreateCategoryRequest createCategoryRequest) { - Category category = createCategoryRequest.toCategory(); + CategoryEntity category = createCategoryRequest.toCategoryEntity(); categoryRepository.save(category); + return category.getCategoryId(); } @Transactional public void updateCategory(Long categoryId, UpdateCategoryRequest updateProductRequest) { - Category category = categoryRepository.selectCategoryById(categoryId) + CategoryEntity category = categoryRepository.findById(categoryId) .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "해당 카테고리를 찾을 수 없습니다.")); - category.updateCategory(updateProductRequest); - categoryRepository.updateCategory(category); + category.updateCategory(updateProductRequest); } } diff --git a/src/main/java/org/store/clothstar/product/dto/request/CreateProductRequest.java b/src/main/java/org/store/clothstar/product/dto/request/CreateProductRequest.java index 9037f7b6..9a2b5a4a 100644 --- a/src/main/java/org/store/clothstar/product/dto/request/CreateProductRequest.java +++ b/src/main/java/org/store/clothstar/product/dto/request/CreateProductRequest.java @@ -8,6 +8,8 @@ import lombok.Getter; import lombok.NoArgsConstructor; import org.store.clothstar.product.domain.Product; +import org.store.clothstar.product.entity.ProductEntity; +import org.store.clothstar.productLine.entity.ProductLineEntity; @Getter @AllArgsConstructor @@ -38,4 +40,13 @@ public Product toProduct() { .stock(stock) .build(); } + + public ProductEntity toProductEntity(ProductLineEntity productLine) { + return ProductEntity.builder() + .productLine(productLine) + .name(name) + .extraCharge(extraCharge) + .stock(stock) + .build(); + } } diff --git a/src/main/java/org/store/clothstar/product/dto/response/ProductResponse.java b/src/main/java/org/store/clothstar/product/dto/response/ProductResponse.java index 885b0927..f6f05262 100644 --- a/src/main/java/org/store/clothstar/product/dto/response/ProductResponse.java +++ b/src/main/java/org/store/clothstar/product/dto/response/ProductResponse.java @@ -14,15 +14,6 @@ public class ProductResponse { private int extraCharge; private Long stock; - public static ProductResponse from(Product product) { - return ProductResponse.builder() - .productId(product.getProductId()) - .productLineId(product.getProductLineId()) - .name(product.getName()) - .extraCharge(product.getExtraCharge()) - .stock(product.getStock()) - .build(); - } public static ProductResponse from(ProductEntity product) { return ProductResponse.builder() diff --git a/src/main/java/org/store/clothstar/product/service/ProductService.java b/src/main/java/org/store/clothstar/product/service/ProductService.java index de3445ac..7c5e08aa 100644 --- a/src/main/java/org/store/clothstar/product/service/ProductService.java +++ b/src/main/java/org/store/clothstar/product/service/ProductService.java @@ -11,12 +11,20 @@ import org.store.clothstar.product.dto.request.CreateProductRequest; import org.store.clothstar.product.dto.request.UpdateProductRequest; import org.store.clothstar.product.dto.response.ProductResponse; +import org.store.clothstar.product.entity.ProductEntity; +import org.store.clothstar.product.repository.ProductJPARepository; import org.store.clothstar.product.repository.ProductRepository; +import org.store.clothstar.productLine.entity.ProductLineEntity; +import org.store.clothstar.productLine.repository.ProductLineJPARepository; + +import java.util.stream.Collectors; @Service @RequiredArgsConstructor public class ProductService { - private final ProductRepository productRepository; + + private final ProductJPARepository productRepository; + private final ProductLineJPARepository productLineRepository; /* @Transactional(readOnly = true) @@ -28,7 +36,7 @@ public List getAllProduct() { */ @Transactional(readOnly = true) public ProductResponse getProduct(Long productId) { - return productRepository.selectByProductId(productId) + return productRepository.findById(productId) .map(ProductResponse::from) .orElseThrow(() -> new ResponseStatusException( HttpStatus.BAD_REQUEST, @@ -37,31 +45,34 @@ public ProductResponse getProduct(Long productId) { @Transactional public Long createProduct(@Validated @RequestBody CreateProductRequest createProductRequest) { - Product product = createProductRequest.toProduct(); + ProductLineEntity ProductLine = productLineRepository.findById(createProductRequest.getProductLineId()) + .orElseThrow(() -> new ResponseStatusException( + HttpStatus.BAD_REQUEST, + "productLineId :" + createProductRequest.getProductLineId() + "인 상품 라인 정보를 찾을 수 없습니다.")); + + ProductEntity product = createProductRequest.toProductEntity(ProductLine); productRepository.save(product); - return product.getProductLineId(); + return product.getProductId(); } @Transactional public void updateProduct(Long productId, UpdateProductRequest updateProductRequest) { - Product product = productRepository.selectByProductId(productId) + ProductEntity product = productRepository.findById(productId) .orElseThrow(() -> new ResponseStatusException( HttpStatus.BAD_REQUEST, "productId :" + productId + "인 상품 옵션 정보를 찾을 수 없습니다.")); product.updateOption(updateProductRequest); - - productRepository.updateProduct(product); } @Transactional public void deleteProduct(Long productId) { - Product product = productRepository.selectByProductId(productId) + ProductEntity product = productRepository.findById(productId) .orElseThrow(() -> new ResponseStatusException( HttpStatus.BAD_REQUEST, "productId :" + productId + "인 상품 옵션 정보를 찾을 수 없습니다.")); - productRepository.deleteProduct(productId); + productRepository.deleteById(productId); } } diff --git a/src/main/java/org/store/clothstar/productLine/application/ProductLineApplicationService.java b/src/main/java/org/store/clothstar/productLine/application/ProductLineApplicationService.java deleted file mode 100644 index 3e061bc4..00000000 --- a/src/main/java/org/store/clothstar/productLine/application/ProductLineApplicationService.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.store.clothstar.productLine.application; - -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import org.store.clothstar.productLine.dto.request.CreateProductLineRequest; -import org.store.clothstar.productLine.dto.request.UpdateProductLineRequest; -import org.store.clothstar.productLine.dto.response.ProductLineWithProductsJPAResponse; -import org.store.clothstar.productLine.service.ProductLineServiceV2; - -@Slf4j -@Service -@RequiredArgsConstructor -public class ProductLineApplicationService { - - private final ProductLineServiceV2 productLineServiceV2; - - public Page getAllProductLinesWithProducts(Pageable pageable) { - return productLineServiceV2.getAllProductLinesWithProducts(pageable); - } - - @Transactional(readOnly = true) - public ProductLineWithProductsJPAResponse getProductLineWithProductsById(Long productLineId) { - return productLineServiceV2.getProductLineWithProductsById(productLineId); - } - - @Transactional - public Long createProductLine(CreateProductLineRequest createProductLineRequest) { - return productLineServiceV2.createProductLine(createProductLineRequest); - } - - @Transactional - public void updateProductLine(Long productLineId, UpdateProductLineRequest updateProductLineRequest) { - productLineServiceV2.updateProductLine(productLineId, updateProductLineRequest); - } - - @Transactional - public void setDeletedAt(Long productId) { - productLineServiceV2.setDeletedAt(productId); - } -} diff --git a/src/main/java/org/store/clothstar/productLine/controller/ProductLineController.java b/src/main/java/org/store/clothstar/productLine/controller/ProductLineController.java index 8f7c28b7..3ddd6777 100644 --- a/src/main/java/org/store/clothstar/productLine/controller/ProductLineController.java +++ b/src/main/java/org/store/clothstar/productLine/controller/ProductLineController.java @@ -12,6 +12,7 @@ import org.store.clothstar.productLine.dto.request.CreateProductLineRequest; import org.store.clothstar.productLine.dto.request.UpdateProductLineRequest; import org.store.clothstar.productLine.dto.response.ProductLineResponse; +import org.store.clothstar.productLine.dto.response.ProductLineWithProductsJPAResponse; import org.store.clothstar.productLine.dto.response.ProductLineWithProductsResponse; import org.store.clothstar.productLine.service.ProductLineService; @@ -35,8 +36,8 @@ public ResponseEntity> getAllProductLines() { @Operation(summary = "상품 상세 조회", description = "productLineId로 상품과 하위 옵션들을 상세 조회한다.") @GetMapping("/{productLineId}") - public ResponseEntity getProductLine(@PathVariable("productLineId") Long productLineId) { - ProductLineWithProductsResponse productLineWithProducts = productLineService.getProductLineWithProducts(productLineId); + public ResponseEntity getProductLine(@PathVariable("productLineId") Long productLineId) { + ProductLineWithProductsJPAResponse productLineWithProducts = productLineService.getProductLineWithProducts(productLineId); return ResponseEntity.ok().body(productLineWithProducts); } diff --git a/src/main/java/org/store/clothstar/productLine/controller/ProductLineController_v2.java b/src/main/java/org/store/clothstar/productLine/controller/ProductLineController_v2.java deleted file mode 100644 index 0ca68c82..00000000 --- a/src/main/java/org/store/clothstar/productLine/controller/ProductLineController_v2.java +++ /dev/null @@ -1,69 +0,0 @@ -package org.store.clothstar.productLine.controller; - -import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.tags.Tag; -import lombok.RequiredArgsConstructor; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.*; -import org.store.clothstar.common.dto.MessageDTO; -import org.store.clothstar.common.util.URIBuilder; -import org.store.clothstar.productLine.application.ProductLineApplicationService; -import org.store.clothstar.productLine.dto.request.CreateProductLineRequest; -import org.store.clothstar.productLine.dto.request.UpdateProductLineRequest; -import org.store.clothstar.productLine.dto.response.ProductLineWithProductsJPAResponse; - -import java.net.URI; - -@Tag(name = "ProductLines-v2", description = "ProductLine 관련 API 입니다.") -@RestController -@RequiredArgsConstructor -@RequestMapping("/v2/productLines") -public class ProductLineController_v2 { - - private final ProductLineApplicationService productLineApplicationService; - - @Operation(summary = "전체 상품 조회", description = "삭제되지 않은 모든 상품을 조회한다.") - @GetMapping - public ResponseEntity> getAllProductLines(Pageable pageable) { - Page productLineResponses = productLineApplicationService.getAllProductLinesWithProducts(pageable); - return ResponseEntity.ok().body(productLineResponses); - } - - @Operation(summary = "상품 상세 조회", description = "productLineId로 상품과 하위 옵션들을 상세 조회한다.") - @GetMapping("/{productLineId}") - public ResponseEntity getProductLine(@PathVariable("productLineId") Long productLineId) { - ProductLineWithProductsJPAResponse productLineWithProducts = productLineApplicationService.getProductLineWithProductsById(productLineId); - return ResponseEntity.ok().body(productLineWithProducts); - } - - @Operation(summary = "상품 등록", description = "카테고리 아이디, 상품 이름, 내용, 가격, 상태를 입력하여 상품을 신규 등록한다.") - @PostMapping - public ResponseEntity createProductLine(@Validated @RequestBody CreateProductLineRequest createProductLineRequest) { - Long productLineId = productLineApplicationService.createProductLine(createProductLineRequest); - URI location = URIBuilder.buildURI(productLineId); - - return ResponseEntity.created(location).build(); - } - - @Operation(summary = "상품 수정", description = "상품 이름, 가격, 재고, 상태를 입력하여 상품 정보를 수정한다.") - @PutMapping("/{productLineId}") - public ResponseEntity updateProductLine( - @PathVariable Long productLineId, - @Validated @RequestBody UpdateProductLineRequest updateProductLineRequest) { - - productLineApplicationService.updateProductLine(productLineId, updateProductLineRequest); - - return ResponseEntity.ok().body(new MessageDTO(HttpStatus.OK.value(), "ProductLine updated successfully")); - } - - @DeleteMapping("/{productLineId}") - public ResponseEntity deleteProductLine(@PathVariable Long productLineId) { - productLineApplicationService.setDeletedAt(productLineId); - return ResponseEntity.noContent().build(); - } - -} diff --git a/src/main/java/org/store/clothstar/productLine/dto/request/CreateProductLineRequest.java b/src/main/java/org/store/clothstar/productLine/dto/request/CreateProductLineRequest.java index 167b72d9..40f8ca0c 100644 --- a/src/main/java/org/store/clothstar/productLine/dto/request/CreateProductLineRequest.java +++ b/src/main/java/org/store/clothstar/productLine/dto/request/CreateProductLineRequest.java @@ -8,7 +8,7 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import org.store.clothstar.category.domain.Category; +import org.store.clothstar.category.entity.CategoryEntity; import org.store.clothstar.member.entity.SellerEntity; import org.store.clothstar.productLine.domain.ProductLine; import org.store.clothstar.productLine.domain.type.ProductLineStatus; @@ -57,7 +57,7 @@ public ProductLine toProductLine(Long memberId) { .build(); } - public ProductLineEntity toProductLineEntity(Long memberId, SellerEntity seller, Category category) { + public ProductLineEntity toProductLineEntity(SellerEntity seller, CategoryEntity category) { return ProductLineEntity.builder() .seller(seller) .category(category) diff --git a/src/main/java/org/store/clothstar/productLine/dto/response/ProductLineResponse.java b/src/main/java/org/store/clothstar/productLine/dto/response/ProductLineResponse.java index 19ffa915..608445c8 100644 --- a/src/main/java/org/store/clothstar/productLine/dto/response/ProductLineResponse.java +++ b/src/main/java/org/store/clothstar/productLine/dto/response/ProductLineResponse.java @@ -5,6 +5,7 @@ import lombok.extern.slf4j.Slf4j; import org.store.clothstar.productLine.domain.ProductLine; import org.store.clothstar.productLine.domain.type.ProductLineStatus; +import org.store.clothstar.productLine.entity.ProductLineEntity; @Getter @Builder @@ -25,6 +26,18 @@ public static ProductLineResponse from(ProductLine productLine) { .name(productLine.getName()) .content(productLine.getContent()) .price(productLine.getPrice()) +// .totalStock(productLine.getTotalStock()) + .productLineStatus(productLine.getStatus()) + .build(); + } + + public static ProductLineResponse from(ProductLineEntity productLine) { + return ProductLineResponse.builder() + .productLineId(productLine.getProductLineId()) + .brandName(productLine.getSeller().getBrandName()) + .name(productLine.getName()) + .content(productLine.getContent()) + .price(productLine.getPrice()) // .totalStock(productLine.getTotalStock()) .productLineStatus(productLine.getStatus()) .build(); diff --git a/src/main/java/org/store/clothstar/productLine/dto/response/ProductLineWithProductsJPAResponse.java b/src/main/java/org/store/clothstar/productLine/dto/response/ProductLineWithProductsJPAResponse.java index 77086bbe..7cd7f9b9 100644 --- a/src/main/java/org/store/clothstar/productLine/dto/response/ProductLineWithProductsJPAResponse.java +++ b/src/main/java/org/store/clothstar/productLine/dto/response/ProductLineWithProductsJPAResponse.java @@ -6,6 +6,7 @@ import lombok.*; import org.store.clothstar.category.domain.Category; import org.store.clothstar.category.dto.response.CategoryResponse; +import org.store.clothstar.category.entity.CategoryEntity; import org.store.clothstar.member.dto.response.MemberSimpleResponse; import org.store.clothstar.member.dto.response.SellerSimpleResponse; import org.store.clothstar.member.entity.MemberEntity; @@ -64,14 +65,13 @@ public class ProductLineWithProductsJPAResponse { // } // 추가된 생성자 - // 추가된 생성자 - public ProductLineWithProductsJPAResponse(ProductLineEntity productLine, Category category, SellerEntity seller, MemberEntity member, Long totalStock, List productList) { + public ProductLineWithProductsJPAResponse(ProductLineEntity productLine, CategoryEntity category, SellerEntity seller, MemberEntity member, Long totalStock, List productList) { this(productLine, category, seller, member, totalStock); this.productList = productList != null ? productList : new ArrayList<>(); } @QueryProjection - public ProductLineWithProductsJPAResponse(ProductLineEntity productLine, Category category, SellerEntity seller, MemberEntity member, Long totalStock) { + public ProductLineWithProductsJPAResponse(ProductLineEntity productLine, CategoryEntity category, SellerEntity seller, MemberEntity member, Long totalStock) { this.productLineId = productLine.getProductLineId(); this.category = CategoryResponse.from(category); this.name = productLine.getName(); @@ -86,4 +86,8 @@ public ProductLineWithProductsJPAResponse(ProductLineEntity productLine, Categor this.productList = new ArrayList<>(); } + public void setProductList(List productList) { + this.productList = productList != null ? productList : new ArrayList<>(); + } + } diff --git a/src/main/java/org/store/clothstar/productLine/entity/ProductLineEntity.java b/src/main/java/org/store/clothstar/productLine/entity/ProductLineEntity.java index 62df4f9b..d9e7cd63 100644 --- a/src/main/java/org/store/clothstar/productLine/entity/ProductLineEntity.java +++ b/src/main/java/org/store/clothstar/productLine/entity/ProductLineEntity.java @@ -7,7 +7,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; import org.hibernate.annotations.BatchSize; -import org.store.clothstar.category.domain.Category; +import org.store.clothstar.category.entity.CategoryEntity; import org.store.clothstar.common.entity.BaseTimeEntity; import org.store.clothstar.member.entity.SellerEntity; import org.store.clothstar.product.entity.ProductEntity; @@ -37,7 +37,7 @@ public class ProductLineEntity extends BaseTimeEntity { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "category_id", nullable = false) - private Category category; + private CategoryEntity category; private String name; diff --git a/src/main/java/org/store/clothstar/productLine/service/ProductLineService.java b/src/main/java/org/store/clothstar/productLine/service/ProductLineService.java index e226eb6a..14f1e78d 100644 --- a/src/main/java/org/store/clothstar/productLine/service/ProductLineService.java +++ b/src/main/java/org/store/clothstar/productLine/service/ProductLineService.java @@ -6,14 +6,24 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.server.ResponseStatusException; +import org.store.clothstar.category.entity.CategoryEntity; +import org.store.clothstar.category.repository.CategoryJpaRepository; +import org.store.clothstar.member.entity.SellerEntity; +import org.store.clothstar.member.repository.SellerJpaRepository; +import org.store.clothstar.member.repository.SellerRepository; import org.store.clothstar.product.domain.Product; +import org.store.clothstar.product.entity.ProductEntity; import org.store.clothstar.productLine.domain.ProductLine; +import org.store.clothstar.productLine.domain.type.ProductLineStatus; import org.store.clothstar.productLine.dto.request.CreateProductLineRequest; import org.store.clothstar.productLine.dto.request.UpdateProductLineRequest; import org.store.clothstar.productLine.dto.response.ProductLineResponse; +import org.store.clothstar.productLine.dto.response.ProductLineWithProductsJPAResponse; import org.store.clothstar.productLine.dto.response.ProductLineWithProductsResponse; -import org.store.clothstar.productLine.repository.ProductLineRepository; +import org.store.clothstar.productLine.entity.ProductLineEntity; +import org.store.clothstar.productLine.repository.ProductLineJPARepository; +import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -23,35 +33,32 @@ @RequiredArgsConstructor public class ProductLineService { - private final ProductLineRepository productLineRepository; + private final ProductLineJPARepository productLineRepository; + private final SellerRepository sellerRepository; + private final CategoryJpaRepository categoryRepository; @Transactional(readOnly = true) public List getAllProductLines() { - return productLineRepository.selectAllProductLinesNotDeleted().stream() + return productLineRepository.findByDeletedAtIsNullAndStatusNotIn( + Arrays.asList(ProductLineStatus.HIDDEN)) + .stream() .map(ProductLineResponse::from) .collect(Collectors.toList()); } @Transactional(readOnly = true) public Optional getProductLine(Long productLineId) { - return productLineRepository.selectByProductLineId(productLineId) + return productLineRepository.findById(productLineId) .map(ProductLineResponse::from); } @Transactional(readOnly = true) - public ProductLineWithProductsResponse getProductLineWithProducts(Long productLineId) { - ProductLineWithProductsResponse productLineWithProducts = productLineRepository.selectProductLineWithOptions(productLineId) - .orElseThrow(() -> new ResponseStatusException( - HttpStatus.BAD_REQUEST, - "productLineId :" + productLineId + "인 상품 및 옵션 정보를 찾을 수 없습니다.")); - List productList = productLineWithProducts.getProductList(); + public ProductLineWithProductsJPAResponse getProductLineWithProducts(Long productLineId) { + ProductLineWithProductsJPAResponse productLineWithProducts = + productLineRepository.findProductLineWithOptionsById(productLineId) + .orElseThrow(() -> new ResponseStatusException( + HttpStatus.BAD_REQUEST, "productLineId :" + productLineId + "인 상품 및 옵션 정보를 찾을 수 없습니다.")); - Long totalStock = 0L; - for (Product product : productList) { - totalStock += product.getStock(); - } - - productLineWithProducts.setTotalStock(totalStock); return productLineWithProducts; } @@ -59,33 +66,30 @@ public ProductLineWithProductsResponse getProductLineWithProducts(Long productLi @Transactional public Long createProductLine(CreateProductLineRequest createProductLineRequest) { Long memberId = 1L; - ProductLine product = createProductLineRequest.toProductLine(memberId); - productLineRepository.save(product); - return product.getProductLineId(); + SellerEntity seller = sellerRepository.findById(memberId) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "판매자 정보를 찾을 수 없습니다.")); + + CategoryEntity category = categoryRepository.findById(createProductLineRequest.getCategoryId()) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "카테고리를 찾을 수 없습니다.")); + + ProductLineEntity productLine = createProductLineRequest.toProductLineEntity(seller, category); + productLineRepository.save(productLine); + return productLine.getProductLineId(); } @Transactional public void updateProductLine(Long productLineId, UpdateProductLineRequest updateProductLineRequest) { - ProductLine productLine = productLineRepository.selectByProductLineId(productLineId) + ProductLineEntity productLine = productLineRepository.findById(productLineId) .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "상품 정보를 찾을 수 없습니다.")); productLine.updateProductLine(updateProductLineRequest); - - productLineRepository.updateProductLine(productLine); } @Transactional public void setDeletedAt(Long productId) { - ProductLine productLine = productLineRepository.selectByProductLineId(productId) + ProductLineEntity productLine = productLineRepository.findById(productId) .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "상품 정보를 찾을 수 없습니다.")); - productLine.setDeletedAt(); - - ProductLine prodcutLine = productLineRepository.selectByProductLineId(productId) - .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "상품 정보를 찾을 수 없습니다.")); - - prodcutLine.setDeletedAt(); - - productLineRepository.setDeletedAt(prodcutLine); + productLine.delete(); } } \ No newline at end of file diff --git a/src/main/java/org/store/clothstar/productLine/service/ProductLineServiceV2.java b/src/main/java/org/store/clothstar/productLine/service/ProductLineServiceV2.java deleted file mode 100644 index 8a0f33aa..00000000 --- a/src/main/java/org/store/clothstar/productLine/service/ProductLineServiceV2.java +++ /dev/null @@ -1,86 +0,0 @@ -package org.store.clothstar.productLine.service; - -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.server.ResponseStatusException; -import org.store.clothstar.category.domain.Category; -import org.store.clothstar.category.repository.CategoryRepository; -import org.store.clothstar.member.entity.SellerEntity; -import org.store.clothstar.productLine.dto.request.CreateProductLineRequest; -import org.store.clothstar.productLine.dto.request.UpdateProductLineRequest; -import org.store.clothstar.productLine.dto.response.ProductLineWithProductsJPAResponse; -import org.store.clothstar.productLine.entity.ProductLineEntity; -import org.store.clothstar.productLine.exception.ProductLineException; -import org.store.clothstar.productLine.exception.ProductLineExceptionType; -import org.store.clothstar.productLine.repository.ProductLineJPARepository; - -@Slf4j -@Service -@RequiredArgsConstructor -public class ProductLineServiceV2 { - - private final ProductLineJPARepository productLineJPARepository; - private final CategoryRepository categoryRepository; - - @Transactional(readOnly = true) - public Page getAllProductLinesWithProducts(Pageable pageable) { - return productLineJPARepository.getProductLinesWithOptions(pageable); - } - - @Transactional(readOnly = true) - public ProductLineWithProductsJPAResponse getProductLineWithProductsById(Long productLineId) { - return productLineJPARepository.findProductLineWithOptionsById(productLineId) - .orElseThrow(() -> new ProductLineException(ProductLineExceptionType.NOT_FOUND_EXCEPTION)); - } - -// @Transactional(readOnly = true) -// public ProductLineWithProductsJPAResponse getProductLineWithProducts(Long productLineId) { -// ProductLineWithProductsJPAResponse productLineWithProducts = productLineJPARepository.selectProductLineWithOptions(productLineId) -// .orElseThrow(() -> new ResponseStatusException( -// HttpStatus.BAD_REQUEST, -// "productLineId :" + productLineId + "인 상품 및 옵션 정보를 찾을 수 없습니다.")); -// List productList = productLineWithProducts.getProductList(); -// -// Long totalStock = 0L; -// for (Product product : productList) { -// totalStock += product.getStock(); -// } -// -// productLineWithProducts.setTotalStock(totalStock); -// -// return productLineWithProducts; -// } - - @Transactional - public Long createProductLine(CreateProductLineRequest createProductLineRequest) { - Long memberId = 1L; - SellerEntity seller = null; // TODO: Seller 가져오기 - Category category = categoryRepository.selectCategoryById(createProductLineRequest.getCategoryId()) - .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "카테고리를 찾을 수 없습니다.")); - ProductLineEntity product = createProductLineRequest.toProductLineEntity(memberId, seller, category); - productLineJPARepository.save(product); - return product.getProductLineId(); - } - - @Transactional - public void updateProductLine(Long productLineId, UpdateProductLineRequest updateProductLineRequest) { - ProductLineEntity productLine = productLineJPARepository.findById(productLineId) - .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "상품을 찾을 수 없습니다.")); - - productLine.updateProductLine(updateProductLineRequest); - } - - @Transactional - public void setDeletedAt(Long productId) { - ProductLineEntity productLine = productLineJPARepository.findById(productId) - .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "상품을 찾을 수 없습니다.")); - - productLine.delete(); - } - -} From cb7f6ec150a65a097afb2b4581d324311ea76b93 Mon Sep 17 00:00:00 2001 From: Ogu1208 Date: Wed, 3 Jul 2024 02:15:55 +0900 Subject: [PATCH 5/7] =?UTF-8?q?refactor:=20Adapter,=20QueryDSlImpl?= =?UTF-8?q?=EC=97=90=EC=84=9C=20Category=20->=20CategoryEntity=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProductLineRepositoryCustomImpl.java | 39 +++---------------- .../ProductLineJPARepositoryAdapter.java | 3 +- 2 files changed, 7 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/store/clothstar/productLine/repository/ProductLineRepositoryCustomImpl.java b/src/main/java/org/store/clothstar/productLine/repository/ProductLineRepositoryCustomImpl.java index 17237481..5e21c700 100644 --- a/src/main/java/org/store/clothstar/productLine/repository/ProductLineRepositoryCustomImpl.java +++ b/src/main/java/org/store/clothstar/productLine/repository/ProductLineRepositoryCustomImpl.java @@ -12,7 +12,8 @@ import org.springframework.data.support.PageableExecutionUtils; import org.springframework.stereotype.Repository; import org.store.clothstar.category.domain.Category; -import org.store.clothstar.category.domain.QCategory; +import org.store.clothstar.category.entity.CategoryEntity; +import org.store.clothstar.category.entity.QCategoryEntity; import org.store.clothstar.member.entity.MemberEntity; import org.store.clothstar.member.entity.QMemberEntity; import org.store.clothstar.member.entity.QSellerEntity; @@ -35,41 +36,11 @@ public class ProductLineRepositoryCustomImpl implements ProductLineRepositoryCus private final JPAQueryFactory jpaQueryFactory; QProductLineEntity qProductLine = QProductLineEntity.productLineEntity; - QCategory qCategory = QCategory.category; + QCategoryEntity qCategory = QCategoryEntity.categoryEntity; QProductEntity qProduct = QProductEntity.productEntity; QSellerEntity qSeller = QSellerEntity.sellerEntity; QMemberEntity qMember = QMemberEntity.memberEntity; -/* - @Override - public Page getProductLinesWithOptions(Pageable pageable) { - - List> orderSpecifiers = getOrderSpecifiers(pageable.getSort()); - - List content = jpaQueryFactory - .select(new QProductLineWithProductsJPAResponse( - qProductLine, qCategory, qSeller, qMember, qProduct.stock.sum())) - .from(qProductLine) - .innerJoin(qProductLine.seller, qSeller) - .innerJoin(qSeller.member, qMember) - .leftJoin(qProductLine.products, qProduct).fetchJoin() -// .innerJoin(qProduct).on() - .where(qProductLine.deletedAt.isNull()) - .orderBy(orderSpecifiers.toArray(new OrderSpecifier[0])) - .offset(pageable.getOffset()) - .limit(pageable.getPageSize()) - .groupBy(qProductLine.productLineId) - .fetch(); - - JPAQuery totalCount = jpaQueryFactory - .select(qProductLine.count()) - .from(qProductLine) - .where(qProductLine.deletedAt.isNull()); - - return PageableExecutionUtils.getPage(content, pageable, totalCount::fetchOne); - } - - */ public Page getProductLinesWithOptions(Pageable pageable) { List> orderSpecifiers = getOrderSpecifiers(pageable.getSort()); @@ -90,7 +61,7 @@ public Page getProductLinesWithOptions(Pagea Map productLineMap = new HashMap<>(); for (Tuple tuple : results) { ProductLineEntity productLine = tuple.get(qProductLine); - Category category = tuple.get(qCategory); + CategoryEntity category = tuple.get(qCategory); SellerEntity seller = tuple.get(qSeller); MemberEntity member = tuple.get(qMember); ProductEntity product = tuple.get(qProduct); @@ -171,4 +142,4 @@ private List> getOrderSpecifiers(Sort sort) { return orderSpecifiers; } -} +} \ No newline at end of file diff --git a/src/main/java/org/store/clothstar/productLine/repository/adapter/ProductLineJPARepositoryAdapter.java b/src/main/java/org/store/clothstar/productLine/repository/adapter/ProductLineJPARepositoryAdapter.java index 9e739301..9107d779 100644 --- a/src/main/java/org/store/clothstar/productLine/repository/adapter/ProductLineJPARepositoryAdapter.java +++ b/src/main/java/org/store/clothstar/productLine/repository/adapter/ProductLineJPARepositoryAdapter.java @@ -6,6 +6,7 @@ import org.springframework.stereotype.Repository; import org.springframework.web.server.ResponseStatusException; import org.store.clothstar.category.domain.Category; +import org.store.clothstar.category.entity.CategoryEntity; import org.store.clothstar.category.repository.CategoryJpaRepository; import org.store.clothstar.member.entity.SellerEntity; import org.store.clothstar.member.repository.SellerRepository; @@ -85,7 +86,7 @@ ProductLineEntity convertToProductLineEntity(ProductLine productLine) { SellerEntity seller = sellerJpaRepository.findById(productLine.getMemberId()) .orElseThrow(() -> new IllegalArgumentException("Member not found with id: " + productLine.getMemberId())); - Category category = categoryJpaRepository.findById(productLine.getCategoryId()) + CategoryEntity category = categoryJpaRepository.findById(productLine.getCategoryId()) .orElseThrow(() -> new IllegalArgumentException("Category not found with id: " + productLine.getCategoryId())); List productEntities = productJPARepository.findAllByProductId(productLine.getProductLineId()); From e9385892370ab4cda7f9f1310ff77bb72bf8def2 Mon Sep 17 00:00:00 2001 From: Ogu1208 Date: Wed, 3 Jul 2024 04:08:12 +0900 Subject: [PATCH 6/7] =?UTF-8?q?test:=20ProductLine,=20=20Product,=20Catego?= =?UTF-8?q?ry=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95,=20Service=20Id=EA=B0=92=20=EB=B0=98?= =?UTF-8?q?=ED=99=98=20=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../category/service/CategoryService.java | 4 +- .../dto/request/CreateProductRequest.java | 9 - .../dto/request/UpdateProductRequest.java | 6 +- .../ProductJPARepositoryAdapter.java | 74 ------- .../product/service/ProductService.java | 4 +- .../ProductLineRepositoryCustomImpl.java | 1 - .../ProductLineJPARepositoryAdapter.java | 107 ---------- .../service/ProductLineService.java | 4 +- .../category/service/CategoryServiceTest.java | 81 +++----- .../product/service/ProductServiceTest.java | 126 +++++++----- .../service/ProductLineServiceTest.java | 193 +++++++----------- 11 files changed, 187 insertions(+), 422 deletions(-) delete mode 100644 src/main/java/org/store/clothstar/product/repository/ProductJPARepositoryAdapter.java delete mode 100644 src/main/java/org/store/clothstar/productLine/repository/adapter/ProductLineJPARepositoryAdapter.java diff --git a/src/main/java/org/store/clothstar/category/service/CategoryService.java b/src/main/java/org/store/clothstar/category/service/CategoryService.java index 0056d8c1..fc458b0f 100644 --- a/src/main/java/org/store/clothstar/category/service/CategoryService.java +++ b/src/main/java/org/store/clothstar/category/service/CategoryService.java @@ -40,9 +40,9 @@ public CategoryDetailResponse getCategory(Long categoryId) { @Transactional public Long createCategory(CreateCategoryRequest createCategoryRequest) { CategoryEntity category = createCategoryRequest.toCategoryEntity(); - categoryRepository.save(category); + CategoryEntity savedCategory = categoryRepository.save(category); - return category.getCategoryId(); + return savedCategory.getCategoryId(); } @Transactional diff --git a/src/main/java/org/store/clothstar/product/dto/request/CreateProductRequest.java b/src/main/java/org/store/clothstar/product/dto/request/CreateProductRequest.java index 9a2b5a4a..493ff963 100644 --- a/src/main/java/org/store/clothstar/product/dto/request/CreateProductRequest.java +++ b/src/main/java/org/store/clothstar/product/dto/request/CreateProductRequest.java @@ -32,15 +32,6 @@ public class CreateProductRequest { @PositiveOrZero(message = "0이상 양수를 입력해주세요") private Long stock; - public Product toProduct() { - return Product.builder() - .productLineId(productLineId) - .name(name) - .extraCharge(extraCharge) - .stock(stock) - .build(); - } - public ProductEntity toProductEntity(ProductLineEntity productLine) { return ProductEntity.builder() .productLine(productLine) diff --git a/src/main/java/org/store/clothstar/product/dto/request/UpdateProductRequest.java b/src/main/java/org/store/clothstar/product/dto/request/UpdateProductRequest.java index 72a6468a..148216bd 100644 --- a/src/main/java/org/store/clothstar/product/dto/request/UpdateProductRequest.java +++ b/src/main/java/org/store/clothstar/product/dto/request/UpdateProductRequest.java @@ -7,7 +7,7 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import org.store.clothstar.product.domain.Product; +import org.store.clothstar.product.entity.ProductEntity; @Getter @AllArgsConstructor @@ -27,8 +27,8 @@ public class UpdateProductRequest { @PositiveOrZero(message = "0포함 양수를 입력해주세요") private Long stock; - public Product toProduct() { - return Product.builder() + public ProductEntity toProduct() { + return ProductEntity.builder() .name(name) .extraCharge(extraCharge) .stock(stock) diff --git a/src/main/java/org/store/clothstar/product/repository/ProductJPARepositoryAdapter.java b/src/main/java/org/store/clothstar/product/repository/ProductJPARepositoryAdapter.java deleted file mode 100644 index 739456b6..00000000 --- a/src/main/java/org/store/clothstar/product/repository/ProductJPARepositoryAdapter.java +++ /dev/null @@ -1,74 +0,0 @@ -package org.store.clothstar.product.repository; - -import lombok.RequiredArgsConstructor; -import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Repository; -import org.springframework.web.server.ResponseStatusException; -import org.store.clothstar.product.domain.Product; -import org.store.clothstar.product.entity.ProductEntity; -import org.store.clothstar.productLine.entity.ProductLineEntity; -import org.store.clothstar.productLine.repository.ProductLineJPARepository; - -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; - -@Repository -@RequiredArgsConstructor -public class ProductJPARepositoryAdapter implements ProductRepository { - - private final ProductJPARepository productJPARepository; - private final ProductLineJPARepository productLineJPARepository; - - public List selectAllProductByProductLineId(Long productId) { - return productJPARepository.findAllByProductId(productId) - .stream() - .map(Product::from) - .collect(Collectors.toList()); - } - - @Override - public Optional selectByProductId(Long productId) { - return productJPARepository.findById(productId) - .map(Product::from); - } - - @Override - public int save(Product product) { - ProductEntity productEntity = convertToProductEntity(product); - productJPARepository.save(productEntity); - return 1; - } - - @Override - public int updateProduct(Product product) { - ProductEntity productEntity = productJPARepository.findById(product.getProductId()) - .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found with id: " + product.getProductLineId())); - - productEntity.updateOption(product); - - return 1; - } - - @Override - public int deleteProduct(Long productId) { - ProductEntity productEntity = productJPARepository.findById(productId) - .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found with id: " + productId)); - - productJPARepository.delete(productEntity); - return 0; - } - - private ProductEntity convertToProductEntity(Product product) { - ProductLineEntity productLine = productLineJPARepository.findById(product.getProductLineId()) - .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "ProductLine not found with id: " + product.getProductLineId())); - - return ProductEntity.builder() - .productId(product.getProductId()) - .productLine(productLine) - .name(product.getName()) - .extraCharge(product.getExtraCharge()) - .stock(product.getStock()) - .build(); - } -} diff --git a/src/main/java/org/store/clothstar/product/service/ProductService.java b/src/main/java/org/store/clothstar/product/service/ProductService.java index 7c5e08aa..cdebec5a 100644 --- a/src/main/java/org/store/clothstar/product/service/ProductService.java +++ b/src/main/java/org/store/clothstar/product/service/ProductService.java @@ -51,9 +51,9 @@ public Long createProduct(@Validated @RequestBody CreateProductRequest createPro "productLineId :" + createProductRequest.getProductLineId() + "인 상품 라인 정보를 찾을 수 없습니다.")); ProductEntity product = createProductRequest.toProductEntity(ProductLine); - productRepository.save(product); + ProductEntity savedProduct = productRepository.save(product); - return product.getProductId(); + return savedProduct.getProductId(); } @Transactional diff --git a/src/main/java/org/store/clothstar/productLine/repository/ProductLineRepositoryCustomImpl.java b/src/main/java/org/store/clothstar/productLine/repository/ProductLineRepositoryCustomImpl.java index 5e21c700..fb12b7f7 100644 --- a/src/main/java/org/store/clothstar/productLine/repository/ProductLineRepositoryCustomImpl.java +++ b/src/main/java/org/store/clothstar/productLine/repository/ProductLineRepositoryCustomImpl.java @@ -11,7 +11,6 @@ import org.springframework.data.domain.Sort; import org.springframework.data.support.PageableExecutionUtils; import org.springframework.stereotype.Repository; -import org.store.clothstar.category.domain.Category; import org.store.clothstar.category.entity.CategoryEntity; import org.store.clothstar.category.entity.QCategoryEntity; import org.store.clothstar.member.entity.MemberEntity; diff --git a/src/main/java/org/store/clothstar/productLine/repository/adapter/ProductLineJPARepositoryAdapter.java b/src/main/java/org/store/clothstar/productLine/repository/adapter/ProductLineJPARepositoryAdapter.java deleted file mode 100644 index 9107d779..00000000 --- a/src/main/java/org/store/clothstar/productLine/repository/adapter/ProductLineJPARepositoryAdapter.java +++ /dev/null @@ -1,107 +0,0 @@ -package org.store.clothstar.productLine.repository.adapter; - -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Repository; -import org.springframework.web.server.ResponseStatusException; -import org.store.clothstar.category.domain.Category; -import org.store.clothstar.category.entity.CategoryEntity; -import org.store.clothstar.category.repository.CategoryJpaRepository; -import org.store.clothstar.member.entity.SellerEntity; -import org.store.clothstar.member.repository.SellerRepository; -import org.store.clothstar.product.entity.ProductEntity; -import org.store.clothstar.product.repository.ProductJPARepository; -import org.store.clothstar.productLine.domain.ProductLine; -import org.store.clothstar.productLine.domain.type.ProductLineStatus; -import org.store.clothstar.productLine.dto.response.ProductLineWithProductsResponse; -import org.store.clothstar.productLine.entity.ProductLineEntity; -import org.store.clothstar.productLine.repository.ProductLineJPARepository; -import org.store.clothstar.productLine.repository.ProductLineRepository; - -import java.util.Arrays; -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; - -@Slf4j -@Repository -@RequiredArgsConstructor -public class ProductLineJPARepositoryAdapter implements ProductLineRepository { - private final CategoryJpaRepository categoryJpaRepository; - private final SellerRepository sellerJpaRepository; - - private final ProductLineJPARepository productLineJPARepository; - private final ProductJPARepository productJPARepository; - - - @Override - public List selectAllProductLinesNotDeleted() { - return productLineJPARepository.findByDeletedAtIsNullAndStatusNotIn( - Arrays.asList(ProductLineStatus.HIDDEN, ProductLineStatus.DISCONTINUED)) - .stream() - .map(ProductLine::from) - .collect(Collectors.toList()); - } - - @Override - public Optional selectByProductLineId(Long productId) { - return productLineJPARepository.findById(productId) - .map(ProductLine::from); - } - - @Override - public Optional selectProductLineWithOptions(Long productId) { - return productLineJPARepository.findProductLineWithOptionsById(productId) - .map(ProductLineWithProductsResponse::from); - } - - @Override - public int save(ProductLine productLine) { - ProductLineEntity productLineEntity = convertToProductLineEntity(productLine); - productLineJPARepository.save(productLineEntity); - return 1; - } - - @Override - public int updateProductLine(ProductLine productLine) { - ProductLineEntity productLineEntity = productLineJPARepository.findById(productLine.getProductLineId()) - .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "ProductLine not found with id: " + productLine.getProductLineId())); - - productLineEntity.updateProductLine(productLine); - - return 1; - } - - @Override - public int setDeletedAt(ProductLine productLine) { - ProductLineEntity productLineEntity = productLineJPARepository.findById(productLine.getProductLineId()) - .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "ProductLine not found with id: " + productLine.getProductLineId())); - - productLineEntity.delete(); - return 1; - } - - ProductLineEntity convertToProductLineEntity(ProductLine productLine) { - SellerEntity seller = sellerJpaRepository.findById(productLine.getMemberId()) - .orElseThrow(() -> new IllegalArgumentException("Member not found with id: " + productLine.getMemberId())); - - CategoryEntity category = categoryJpaRepository.findById(productLine.getCategoryId()) - .orElseThrow(() -> new IllegalArgumentException("Category not found with id: " + productLine.getCategoryId())); - - List productEntities = productJPARepository.findAllByProductId(productLine.getProductLineId()); - - - return ProductLineEntity.builder() - .productLineId(productLine.getProductLineId()) - .seller(seller) - .category(category) - .name(productLine.getName()) - .content(productLine.getContent()) - .price(productLine.getPrice()) - .status(productLine.getStatus()) - .saleCount(productLine.getSaleCount()) - .products(productEntities) - .build(); - } -} diff --git a/src/main/java/org/store/clothstar/productLine/service/ProductLineService.java b/src/main/java/org/store/clothstar/productLine/service/ProductLineService.java index 14f1e78d..112cac53 100644 --- a/src/main/java/org/store/clothstar/productLine/service/ProductLineService.java +++ b/src/main/java/org/store/clothstar/productLine/service/ProductLineService.java @@ -73,8 +73,8 @@ public Long createProductLine(CreateProductLineRequest createProductLineRequest) .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "카테고리를 찾을 수 없습니다.")); ProductLineEntity productLine = createProductLineRequest.toProductLineEntity(seller, category); - productLineRepository.save(productLine); - return productLine.getProductLineId(); + ProductLineEntity savedProductLine = productLineRepository.save(productLine); + return savedProductLine.getProductLineId(); } @Transactional diff --git a/src/test/java/org/store/clothstar/category/service/CategoryServiceTest.java b/src/test/java/org/store/clothstar/category/service/CategoryServiceTest.java index 49588d0b..5976f570 100644 --- a/src/test/java/org/store/clothstar/category/service/CategoryServiceTest.java +++ b/src/test/java/org/store/clothstar/category/service/CategoryServiceTest.java @@ -3,16 +3,13 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.BDDMockito; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; +import org.mockito.*; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.test.context.ActiveProfiles; -import org.store.clothstar.category.domain.Category; import org.store.clothstar.category.dto.request.CreateCategoryRequest; import org.store.clothstar.category.dto.response.CategoryDetailResponse; import org.store.clothstar.category.dto.response.CategoryResponse; +import org.store.clothstar.category.entity.CategoryEntity; import org.store.clothstar.category.repository.CategoryJpaRepository; import java.util.ArrayList; @@ -31,55 +28,29 @@ class CategoryServiceTest { private CategoryService categoryService; @Mock - private CategoryJpaRepository categoryMybatisRepository; + private CategoryJpaRepository categoryRepository; @DisplayName("카테고리 리스트 조회에 성공한다.") @Test void givenCategories_whenGetAllCategories_thenGetAllCategories() { //given - List categories = new ArrayList<>(); - Category category1 = Category.builder() - .categoryId(1L) - .categoryType("OUTER") - .build(); - Category category2 = Category.builder() - .categoryId(2L) - .categoryType("TOP") - .build(); - Category category3 = Category.builder() - .categoryId(3L) - .categoryType("PANTS") - .build(); - Category category4 = Category.builder() - .categoryId(4L) - .categoryType("SKIRT") - .build(); - Category category5 = Category.builder() - .categoryId(5L) - .categoryType("BAG") - .build(); - Category category6 = Category.builder() - .categoryId(6L) - .categoryType("HEADWEAR") - .build(); + List categories = new ArrayList<>(); + categories.add(CategoryEntity.builder().categoryId(1L).categoryType("OUTER").build()); + categories.add(CategoryEntity.builder().categoryId(2L).categoryType("TOP").build()); + categories.add(CategoryEntity.builder().categoryId(3L).categoryType("PANTS").build()); + categories.add(CategoryEntity.builder().categoryId(4L).categoryType("SKIRT").build()); + categories.add(CategoryEntity.builder().categoryId(5L).categoryType("BAG").build()); + categories.add(CategoryEntity.builder().categoryId(6L).categoryType("HEADWEAR").build()); - categories.add(category1); - categories.add(category2); - categories.add(category3); - categories.add(category4); - categories.add(category5); - categories.add(category6); - - BDDMockito.given(categoryMybatisRepository.selectAllCategory()).willReturn(categories); + BDDMockito.given(categoryRepository.findAll()).willReturn(categories); // when List response = categoryService.getAllCategories(); // then - Mockito.verify(categoryMybatisRepository, Mockito.times(1)) - .selectAllCategory(); + Mockito.verify(categoryRepository, Mockito.times(1)).findAll(); assertThat(response).isNotNull(); - assertThat(response.size()).isEqualTo(6); + assertThat(response).hasSize(6); assertThat(response.get(0).getCategoryType()).isEqualTo("OUTER"); assertThat(response.get(1).getCategoryType()).isEqualTo("TOP"); assertThat(response.get(2).getCategoryType()).isEqualTo("PANTS"); @@ -93,20 +64,18 @@ void givenCategories_whenGetAllCategories_thenGetAllCategories() { void givenCategoryId_whenCategoryId_thenCategoryReturned() { // given Long categoryId = 1L; - - Category category = Category.builder() + CategoryEntity category = CategoryEntity.builder() .categoryId(1L) .categoryType("OUTER") .build(); - BDDMockito.given(categoryMybatisRepository.selectCategoryById(anyLong())).willReturn(Optional.ofNullable(category)); + BDDMockito.given(categoryRepository.findById(anyLong())).willReturn(Optional.of(category)); // when CategoryDetailResponse response = categoryService.getCategory(categoryId); // then - Mockito.verify(categoryMybatisRepository, Mockito.times(1)) - .selectCategoryById(anyLong()); + Mockito.verify(categoryRepository, Mockito.times(1)).findById(anyLong()); assertThat(response).isNotNull(); assertThat(response.getCategoryId()).isEqualTo(1L); assertThat(response.getCategoryType()).isEqualTo("OUTER"); @@ -120,26 +89,36 @@ void givenValidCreateCategoryRequest_whenCreateCategory_thenCategoryCreated() { .categoryType("OUTER") .build(); - BDDMockito.given(categoryMybatisRepository.save(Mockito.any(Category.class))).willReturn(1); + CategoryEntity savedCategory = CategoryEntity.builder() + .categoryId(1L) + .categoryType("OUTER") + .build(); + + BDDMockito.given(categoryRepository.save(Mockito.any(CategoryEntity.class))).willReturn(savedCategory); // when Long categoryId = categoryService.createCategory(createCategoryRequest); // then - Mockito.verify(categoryMybatisRepository, Mockito.times(1)) - .save(Mockito.any(Category.class)); + ArgumentCaptor categoryCaptor = ArgumentCaptor.forClass(CategoryEntity.class); + Mockito.verify(categoryRepository, Mockito.times(1)).save(categoryCaptor.capture()); + + CategoryEntity capturedCategory = categoryCaptor.getValue(); + assertThat(categoryId).isEqualTo(1L); + assertThat(capturedCategory.getCategoryType()).isEqualTo("OUTER"); } @DisplayName("중복된 카테고리 타입을 생성하려고 시도할 경우, 카테고리 생성에 실패한다.") @Test void givenDuplicateCategoryType_whenCreateCategory_thenFailToCreateCategory() { - + // 이 테스트 케이스는 아직 구현되지 않았습니다. } @Test void updateCategory() { // given String duplicateCategoryType = "OUTER"; + // 이 테스트 케이스는 아직 구현되지 않았습니다. } } \ No newline at end of file diff --git a/src/test/java/org/store/clothstar/product/service/ProductServiceTest.java b/src/test/java/org/store/clothstar/product/service/ProductServiceTest.java index 26e17cf5..33d72be6 100644 --- a/src/test/java/org/store/clothstar/product/service/ProductServiceTest.java +++ b/src/test/java/org/store/clothstar/product/service/ProductServiceTest.java @@ -7,15 +7,28 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.test.context.ActiveProfiles; +import org.springframework.web.server.ResponseStatusException; +import org.store.clothstar.category.entity.CategoryEntity; +import org.store.clothstar.category.repository.CategoryJpaRepository; +import org.store.clothstar.member.entity.SellerEntity; +import org.store.clothstar.member.repository.SellerRepository; import org.store.clothstar.product.domain.Product; import org.store.clothstar.product.dto.request.CreateProductRequest; import org.store.clothstar.product.dto.request.UpdateProductRequest; import org.store.clothstar.product.dto.response.ProductResponse; -import org.store.clothstar.product.repository.ProductRepository; +import org.store.clothstar.product.entity.ProductEntity; +import org.store.clothstar.product.repository.ProductJPARepository; +import org.store.clothstar.productLine.domain.ProductLine; +import org.store.clothstar.productLine.domain.type.ProductLineStatus; +import org.store.clothstar.productLine.dto.request.CreateProductLineRequest; +import org.store.clothstar.productLine.entity.ProductLineEntity; +import org.store.clothstar.productLine.repository.ProductLineJPARepository; +import org.store.clothstar.productLine.service.ProductLineService; import java.util.Optional; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.*; @@ -29,67 +42,99 @@ class ProductServiceTest { private ProductService productService; @Mock - private ProductRepository productMybatisRepository; + ProductLineService productLineService; + + @Mock + private ProductJPARepository productRepository; + + @Mock + private ProductLineJPARepository productLineRepository; + + @Mock + private CategoryJpaRepository categoryRepository; + + @Mock + private SellerRepository sellerRepository; @DisplayName("product_id로 상품 옵션 단건 조회에 성공한다.") @Test public void givenProductId_whenGetProductById_thenProductReturned() { // given Long productId = 1L; + ProductLineEntity productLine = mock(ProductLineEntity.class); + when(productLine.getProductLineId()).thenReturn(1L); - Product product = Product.builder() - .productId(1L) - .productLineId(1L) + ProductEntity product = ProductEntity.builder() + .productId(productId) + .productLine(productLine) .name("곰돌이 블랙") .extraCharge(1000) .stock(30L) .build(); - given(productMybatisRepository.selectByProductId(anyLong())).willReturn(Optional.ofNullable(product)); + given(productRepository.findById(productId)).willReturn(Optional.of(product)); // when ProductResponse response = productService.getProduct(productId); // then assertThat(response).isNotNull(); - assertThat(response.getProductId()).isEqualTo(1L); - assertThat(response.getProductLineId()).isEqualTo(1L); + assertThat(response.getProductId()).isEqualTo(productId); + assertThat(response.getProductLineId()).isEqualTo(productLine.getProductLineId()); + assertThat(response.getName()).isEqualTo("곰돌이 블랙"); assertThat(response.getExtraCharge()).isEqualTo(1000); assertThat(response.getStock()).isEqualTo(30L); } - @DisplayName("유효한 createProductRequest 가 들어오면, product_line_id로 1:N 관계의 상품 옵션 생성에 성공한다.") + @DisplayName("존재하지 않는 product_id로 상품 옵션 조회 시 예외가 발생한다") @Test - void givenCreateProductRequest_whenCreateProduct_thenCreatedProductReturned() { + public void givenNonExistentProductId_whenGetProduct_thenThrowException() { + // given + Long nonExistentProductId = 999L; + given(productRepository.findById(nonExistentProductId)).willReturn(Optional.empty()); - Long productLineId = 1L; + // when & then + assertThatThrownBy(() -> productService.getProduct(nonExistentProductId)) + .isInstanceOf(ResponseStatusException.class) + .hasMessageContaining("상품 옵션 정보를 찾을 수 없습니다"); + } + @DisplayName("유효한 상품 생성 Request가 들어오면 상품 생성에 성공한다.") + @Test + public void givenValidCreateProductRequest_whenCreateProduct_thenProductCreated() { + // given + Long expectedProductId = 1L; + Long productLineId = 1L; CreateProductRequest createProductRequest = CreateProductRequest.builder() .productLineId(productLineId) - .name("곰돌이 블랙") + .name("데님 자켓 - 블랙") .extraCharge(1000) - .stock(200L) + .stock(100L) .build(); - given(productMybatisRepository.save(any(Product.class))).willReturn(1); + ProductLineEntity mockProductLine = mock(ProductLineEntity.class); + ProductEntity mockProduct = mock(ProductEntity.class); + when(mockProduct.getProductId()).thenReturn(expectedProductId); + + when(productLineRepository.findById(eq(productLineId))).thenReturn(Optional.of(mockProductLine)); + when(productRepository.save(any(ProductEntity.class))).thenReturn(mockProduct); // when - Long createdProductId = productService.createProduct(createProductRequest); + Long actualProductId = productService.createProduct(createProductRequest); // then - verify(productMybatisRepository, times(1)) - .save(any(Product.class)); - assertThat(createdProductId).isNotNull(); + assertThat(actualProductId).isEqualTo(expectedProductId); + verify(productLineRepository).findById(eq(productLineId)); + verify(productRepository).save(any(ProductEntity.class)); } @DisplayName("유효한 productId와 UpdateProductRequest 가 들어오면 product 수정에 성공한다.") @Test void givenValidProductIdWithUpdateProductRequest_whenUpdateProduct_thenUpdateProductSuccess() { + // given Long productId = 1L; - - Product product = Product.builder() - .productId(1L) - .productLineId(1L) + ProductEntity product = ProductEntity.builder() + .productId(productId) .name("곰돌이 블랙") .extraCharge(1000) .stock(30L) @@ -101,42 +146,15 @@ void givenValidProductIdWithUpdateProductRequest_whenUpdateProduct_thenUpdatePro .stock(180L) .build(); - given(productMybatisRepository.selectByProductId(anyLong())).willReturn(Optional.ofNullable(product)); - given(productMybatisRepository.updateProduct(any(Product.class))).willReturn(1); + given(productRepository.findById(productId)).willReturn(Optional.of(product)); // when productService.updateProduct(productId, updateProductRequest); // then - verify(productMybatisRepository, times(1)) - .selectByProductId(anyLong()); - verify(productMybatisRepository, times(1)) - .updateProduct(any(Product.class)); - } - - @DisplayName("해당 productId의 product 가 존재하면 삭제에 성공한다.") - @Test - void deleteProduct() { - Long productId = 1L; - - Product product = Product.builder() - .productId(1L) - .productLineId(1L) - .name("곰돌이 블랙") - .extraCharge(1000) - .stock(30L) - .build(); - - given(productMybatisRepository.selectByProductId(anyLong())).willReturn(Optional.ofNullable(product)); - given(productMybatisRepository.deleteProduct(anyLong())).willReturn(1); - - // when - productService.deleteProduct(productId); - - // then - verify(productMybatisRepository, times(1)) - .selectByProductId(anyLong()); - verify(productMybatisRepository, times(1)) - .deleteProduct(anyLong()); + verify(productRepository).findById(productId); + assertThat(product.getName()).isEqualTo("곰돌이 블랙진"); + assertThat(product.getExtraCharge()).isEqualTo(1000); + assertThat(product.getStock()).isEqualTo(180L); } } \ No newline at end of file diff --git a/src/test/java/org/store/clothstar/productLine/service/ProductLineServiceTest.java b/src/test/java/org/store/clothstar/productLine/service/ProductLineServiceTest.java index 90d813b0..568c491f 100644 --- a/src/test/java/org/store/clothstar/productLine/service/ProductLineServiceTest.java +++ b/src/test/java/org/store/clothstar/productLine/service/ProductLineServiceTest.java @@ -7,17 +7,20 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.test.context.ActiveProfiles; -import org.store.clothstar.product.domain.Product; +import org.store.clothstar.category.entity.CategoryEntity; +import org.store.clothstar.category.repository.CategoryJpaRepository; +import org.store.clothstar.member.entity.SellerEntity; +import org.store.clothstar.member.repository.SellerRepository; import org.store.clothstar.productLine.domain.ProductLine; import org.store.clothstar.productLine.domain.type.ProductLineStatus; import org.store.clothstar.productLine.dto.request.CreateProductLineRequest; import org.store.clothstar.productLine.dto.request.UpdateProductLineRequest; import org.store.clothstar.productLine.dto.response.ProductLineResponse; -import org.store.clothstar.productLine.dto.response.ProductLineWithProductsResponse; -import org.store.clothstar.productLine.repository.ProductLineRepository; +import org.store.clothstar.productLine.dto.response.ProductLineWithProductsJPAResponse; +import org.store.clothstar.productLine.entity.ProductLineEntity; +import org.store.clothstar.productLine.repository.ProductLineJPARepository; import java.time.LocalDateTime; -import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -34,65 +37,86 @@ class ProductLineServiceTest { private ProductLineService productLineService; @Mock - private ProductLineRepository productLineMybatisRepository; + private ProductLineJPARepository productLineRepository; + + @Mock + private CategoryJpaRepository categoryRepository; + + @Mock + private SellerRepository sellerRepository; @DisplayName("상품 리스트 조회에 성공한다.") @Test public void givenProductLines_whenGetProductLineList_thenGetProductLines() { // given - ProductLine productLine1 = mock(ProductLine.class); - ProductLine productLine2 = mock(ProductLine.class); - ProductLine productLine3 = mock(ProductLine.class); + ProductLineEntity productLine1 = mock(ProductLineEntity.class); + ProductLineEntity productLine2 = mock(ProductLineEntity.class); + ProductLineEntity productLine3 = mock(ProductLineEntity.class); + SellerEntity seller1 = mock(SellerEntity.class); + SellerEntity seller2 = mock(SellerEntity.class); + SellerEntity seller3 = mock(SellerEntity.class); + + when(productLine1.getProductLineId()).thenReturn(1L); + when(productLine1.getSeller()).thenReturn(seller1); + when(seller1.getBrandName()).thenReturn("브랜드1"); when(productLine1.getName()).thenReturn("오구 키링"); when(productLine1.getPrice()).thenReturn(13000); when(productLine1.getStatus()).thenReturn(ProductLineStatus.COMING_SOON); + when(productLine2.getProductLineId()).thenReturn(2L); + when(productLine2.getSeller()).thenReturn(seller2); + when(seller2.getBrandName()).thenReturn("브랜드2"); when(productLine2.getName()).thenReturn("오구 바디 필로우"); when(productLine2.getPrice()).thenReturn(57000); when(productLine2.getStatus()).thenReturn(ProductLineStatus.FOR_SALE); + when(productLine3.getProductLineId()).thenReturn(3L); + when(productLine3.getSeller()).thenReturn(seller3); + when(seller3.getBrandName()).thenReturn("브랜드3"); when(productLine3.getName()).thenReturn("오구 볼펜"); when(productLine3.getPrice()).thenReturn(7900); when(productLine3.getStatus()).thenReturn(ProductLineStatus.SOLD_OUT); - List productLines = List.of(productLine1, productLine2, productLine3); - when(productLineMybatisRepository.selectAllProductLinesNotDeleted()).thenReturn(productLines); + List productLines = List.of(productLine1, productLine2, productLine3); + when(productLineRepository.findByDeletedAtIsNullAndStatusNotIn(any())).thenReturn(productLines); // when List response = productLineService.getAllProductLines(); // then - verify(productLineMybatisRepository, times(1)) - .selectAllProductLinesNotDeleted(); - assertThat(response).isNotNull(); - assertThat(response.size()).isEqualTo(3); + verify(productLineRepository, times(1)) + .findByDeletedAtIsNullAndStatusNotIn(any()); + assertThat(response).hasSize(3); assertThat(response.get(0).getName()).isEqualTo("오구 키링"); assertThat(response.get(0).getPrice()).isEqualTo(13000); -// assertThat(response.get(0).getTotalStock()).isEqualTo(20); assertThat(response.get(0).getProductLineStatus()).isEqualTo(ProductLineStatus.COMING_SOON); + assertThat(response.get(0).getBrandName()).isEqualTo("브랜드1"); } @DisplayName("product_line_id로 상품 단건 조회에 성공한다.") @Test public void givenProductLineId_whenGetProductLineById_thenProductLineReturned() { // given - ProductLine productLine = mock(ProductLine.class); - when(productLine.getBrandName()).thenReturn("내셔널지오그래픽키즈 제주점"); + Long productLineId = 1L; + ProductLineEntity productLine = mock(ProductLineEntity.class); + SellerEntity seller = mock(SellerEntity.class); + + when(productLine.getProductLineId()).thenReturn(productLineId); + when(productLine.getSeller()).thenReturn(seller); + when(seller.getBrandName()).thenReturn("내셔널지오그래픽키즈 제주점"); when(productLine.getName()).thenReturn("내셔널지오그래픽 곰돌이 후드티"); when(productLine.getContent()).thenReturn("귀여운 곰돌이가 그려진 후드티에요!"); when(productLine.getPrice()).thenReturn(69000); when(productLine.getStatus()).thenReturn(ProductLineStatus.ON_SALE); - given(productLineMybatisRepository.selectByProductLineId(anyLong())).willReturn(Optional.ofNullable(productLine)); + given(productLineRepository.findById(productLineId)).willReturn(Optional.of(productLine)); // when - Optional response = productLineService.getProductLine(productLine.getProductLineId()); + Optional response = productLineService.getProductLine(productLineId); // then - assertThat(response).isPresent(); // Optional이 비어있지 않은지 확인 - - // Optional이 비어있지 않은 경우에만 값을 가져와서 검증 + assertThat(response).isPresent(); response.ifPresent(productLineResponse -> { assertThat(productLineResponse.getBrandName()).isEqualTo("내셔널지오그래픽키즈 제주점"); assertThat(productLineResponse.getName()).isEqualTo("내셔널지오그래픽 곰돌이 후드티"); @@ -100,7 +124,6 @@ public void givenProductLineId_whenGetProductLineById_thenProductLineReturned() assertThat(productLineResponse.getPrice()).isEqualTo(69000); assertThat(productLineResponse.getProductLineStatus()).isEqualTo(ProductLineStatus.ON_SALE); }); - } @DisplayName("상품 id와 상품과 1:N 관계에 있는 상품 옵션 리스트를 조회한다.") @@ -108,67 +131,27 @@ public void givenProductLineId_whenGetProductLineById_thenProductLineReturned() public void givenProductLineId_whenGetProductLineWithProducts_thenProductLineWithProducts() { // given Long productLineId = 1L; - Product product1 = Product.builder() - .productId(1L) - .productLineId(1L) - .name("블랙") - .extraCharge(0) - .stock(30L) - .build(); - - Product product2 = Product.builder() - .productId(2L) - .productLineId(1L) - .name("화이트") - .extraCharge(1000) - .stock(30L) - .build(); + ProductLineWithProductsJPAResponse mockResponse = mock(ProductLineWithProductsJPAResponse.class); + when(mockResponse.getProductLineId()).thenReturn(productLineId); + when(mockResponse.getTotalStock()).thenReturn(90L); - Product product3 = Product.builder() - .productId(3L) - .productLineId(1L) - .name("네이비") - .extraCharge(1000) - .stock(30L) - .build(); - - List productList = new ArrayList<>(); - productList.add(product1); - productList.add(product2); - productList.add(product3); - - ProductLineWithProductsResponse productLineWithProductsResponse = ProductLineWithProductsResponse.builder() - .productLineId(1L) - .memberId(1L) - .categoryId(2L) - .name("내셔널지오그래픽 곰돌이 후드티") - .content("귀여운 곰돌이가 그려진 후드티에요!") - .price(69000) - .status(ProductLineStatus.ON_SALE) - .createdAt(LocalDateTime.now()) - .modifiedAt(null) - .deletedAt(null) - .brandName("내셔널지오그래픽키즈 제주점") - .biz_no("232-05-02861") - .productList(productList) - .build(); - - given(productLineMybatisRepository.selectProductLineWithOptions(anyLong())).willReturn(Optional.ofNullable(productLineWithProductsResponse)); + given(productLineRepository.findProductLineWithOptionsById(productLineId)).willReturn(Optional.of(mockResponse)); // when - ProductLineWithProductsResponse response = productLineService.getProductLineWithProducts(productLineId); + ProductLineWithProductsJPAResponse response = productLineService.getProductLineWithProducts(productLineId); // then assertThat(response).isNotNull(); - assertThat(response.getProductLineId()).isEqualTo(1L); + assertThat(response.getProductLineId()).isEqualTo(productLineId); assertThat(response.getTotalStock()).isEqualTo(90L); } - @DisplayName("유효한 상품 생성 Request가 들어오면 상품 생성에 성공한다.") + @DisplayName("유효한 상품 라인 생성 Request가 들어오면 상품 라인 생성에 성공한다.") @Test public void givenValidCreateProductLineRequest_whenCreateProductLine_thenProductLineCreated() { // given Long productLineId = 1L; + Long memberId = 1L; // 하드코딩된 memberId CreateProductLineRequest createProductLineRequest = CreateProductLineRequest.builder() .categoryId(1L) .name("데님 자켓") @@ -177,14 +160,23 @@ public void givenValidCreateProductLineRequest_whenCreateProductLine_thenProduct .status(ProductLineStatus.ON_SALE) .build(); - given(productLineMybatisRepository.save(any(ProductLine.class))).willReturn(1); + SellerEntity mockSeller = mock(SellerEntity.class); + CategoryEntity mockCategory = mock(CategoryEntity.class); + ProductLineEntity mockProductLine = mock(ProductLineEntity.class); + + when(mockProductLine.getProductLineId()).thenReturn(productLineId); + when(sellerRepository.findById(eq(memberId))).thenReturn(Optional.of(mockSeller)); + when(categoryRepository.findById(eq(1L))).thenReturn(Optional.of(mockCategory)); + when(productLineRepository.save(any(ProductLineEntity.class))).thenReturn(mockProductLine); // when Long responseProductLineId = productLineService.createProductLine(createProductLineRequest); // then - verify(productLineMybatisRepository, times(1)) - .save(any(ProductLine.class)); + assertThat(responseProductLineId).isEqualTo(productLineId); + verify(sellerRepository).findById(eq(memberId)); + verify(categoryRepository).findById(eq(1L)); + verify(productLineRepository).save(any(ProductLineEntity.class)); } @DisplayName("유효한 UpdateProductLineRequest가 들어오면 상품 수정에 성공한다.") @@ -199,65 +191,32 @@ public void givenValidUpdateProductLineRequest_whenUpdateProductLine_thenProduct .status(ProductLineStatus.ON_SALE) .build(); - ProductLine productLine = ProductLine.builder() - .productLineId(productLineId) - .memberId(1L) - .categoryId(1L) - .name("데님 자켓") - .price(19000) - .totalStock(50L) - .status(ProductLineStatus.ON_SALE) - .createdAt(LocalDateTime.now()) - .modifiedAt(null) - .deletedAt(null) - .brandName("내셔널지오그래픽키즈 제주점") - .biz_no("232-05-02861") - .build(); + ProductLineEntity productLine = mock(ProductLineEntity.class); - given(productLineMybatisRepository.selectByProductLineId(anyLong())).willReturn(Optional.ofNullable(productLine)); - given(productLineMybatisRepository.updateProductLine(any(ProductLine.class))).willReturn(1); + given(productLineRepository.findById(productLineId)).willReturn(Optional.of(productLine)); // when productLineService.updateProductLine(productLineId, updateProductLineRequest); // then - verify(productLineMybatisRepository, times(1)) - .selectByProductLineId(anyLong()); - verify(productLineMybatisRepository, times(1)) - .updateProductLine(any(ProductLine.class)); + verify(productLineRepository, times(1)).findById(productLineId); + verify(productLine, times(1)).updateProductLine(updateProductLineRequest); } - @DisplayName("유효한 UpdateProductLineRequest가 들어오면 상품 수정에 성공한다.") + @DisplayName("상품 삭제 요청 시 deletedAt이 설정된다.") @Test - public void givenProductLineId_whenDeleteProducctLine_thenSetDeletedAt() { + public void givenProductLineId_whenDeleteProductLine_thenSetDeletedAt() { // given Long productLineId = 1L; + ProductLineEntity productLine = mock(ProductLineEntity.class); - ProductLine productLine = ProductLine.builder() - .productLineId(productLineId) - .memberId(1L) - .categoryId(1L) - .name("데님 자켓") - .price(19000) - .totalStock(50L) - .status(ProductLineStatus.ON_SALE) - .createdAt(LocalDateTime.now()) - .modifiedAt(null) - .deletedAt(null) - .brandName("내셔널지오그래픽키즈 제주점") - .biz_no("232-05-02861") - .build(); - - given(productLineMybatisRepository.selectByProductLineId(anyLong())).willReturn(Optional.ofNullable(productLine)); - given(productLineMybatisRepository.setDeletedAt(any(ProductLine.class))).willReturn(1); + given(productLineRepository.findById(productLineId)).willReturn(Optional.of(productLine)); // when productLineService.setDeletedAt(productLineId); // then - verify(productLineMybatisRepository, times(2)) - .selectByProductLineId(anyLong()); - verify(productLineMybatisRepository, times(1)) - .setDeletedAt(any(ProductLine.class)); + verify(productLineRepository, times(1)).findById(productLineId); + verify(productLine, times(1)).delete(); } } \ No newline at end of file From 7b7a930e5ac73269a323108f529521b39aaa95ac Mon Sep 17 00:00:00 2001 From: Ogu1208 Date: Wed, 3 Jul 2024 23:33:22 +0900 Subject: [PATCH 7/7] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20import=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/store/clothstar/Application.java | 1 - .../clothstar/category/repository/CategoryJpaRepository.java | 1 - .../store/clothstar/category/service/CategoryService.java | 1 - .../store/clothstar/order/controller/OrderController.java | 1 - .../clothstar/order/controller/OrderSellerController.java | 1 - .../store/clothstar/order/service/OrderSellerService.java | 1 - .../java/org/store/clothstar/product/domain/Product.java | 2 -- .../clothstar/product/dto/request/CreateProductRequest.java | 1 - .../clothstar/product/dto/response/ProductResponse.java | 1 - .../clothstar/product/repository/ProductJPARepository.java | 1 - .../org/store/clothstar/product/service/ProductService.java | 4 ---- .../productLine/controller/ProductLineController.java | 1 - .../org/store/clothstar/productLine/domain/ProductLine.java | 1 - .../dto/response/ProductLineWithProductsJPAResponse.java | 1 - .../clothstar/productLine/service/ProductLineService.java | 5 ----- 15 files changed, 23 deletions(-) diff --git a/src/main/java/org/store/clothstar/Application.java b/src/main/java/org/store/clothstar/Application.java index 3a05c7ff..a4717575 100644 --- a/src/main/java/org/store/clothstar/Application.java +++ b/src/main/java/org/store/clothstar/Application.java @@ -2,7 +2,6 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.ComponentScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; //@EnableJpaAuditing diff --git a/src/main/java/org/store/clothstar/category/repository/CategoryJpaRepository.java b/src/main/java/org/store/clothstar/category/repository/CategoryJpaRepository.java index b152c417..927004ab 100644 --- a/src/main/java/org/store/clothstar/category/repository/CategoryJpaRepository.java +++ b/src/main/java/org/store/clothstar/category/repository/CategoryJpaRepository.java @@ -2,7 +2,6 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import org.store.clothstar.category.domain.Category; import org.store.clothstar.category.entity.CategoryEntity; @Repository diff --git a/src/main/java/org/store/clothstar/category/service/CategoryService.java b/src/main/java/org/store/clothstar/category/service/CategoryService.java index fc458b0f..92013a4d 100644 --- a/src/main/java/org/store/clothstar/category/service/CategoryService.java +++ b/src/main/java/org/store/clothstar/category/service/CategoryService.java @@ -5,7 +5,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.server.ResponseStatusException; -import org.store.clothstar.category.domain.Category; import org.store.clothstar.category.dto.request.CreateCategoryRequest; import org.store.clothstar.category.dto.request.UpdateCategoryRequest; import org.store.clothstar.category.dto.response.CategoryDetailResponse; diff --git a/src/main/java/org/store/clothstar/order/controller/OrderController.java b/src/main/java/org/store/clothstar/order/controller/OrderController.java index b0818524..6fe7b1f7 100644 --- a/src/main/java/org/store/clothstar/order/controller/OrderController.java +++ b/src/main/java/org/store/clothstar/order/controller/OrderController.java @@ -2,7 +2,6 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; diff --git a/src/main/java/org/store/clothstar/order/controller/OrderSellerController.java b/src/main/java/org/store/clothstar/order/controller/OrderSellerController.java index 2655a4e1..3fd745d7 100644 --- a/src/main/java/org/store/clothstar/order/controller/OrderSellerController.java +++ b/src/main/java/org/store/clothstar/order/controller/OrderSellerController.java @@ -6,7 +6,6 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.store.clothstar.common.dto.MessageDTO; -import org.store.clothstar.order.dto.reponse.OrderResponse; import org.store.clothstar.order.entity.OrderEntity; import org.store.clothstar.order.service.OrderSellerService; diff --git a/src/main/java/org/store/clothstar/order/service/OrderSellerService.java b/src/main/java/org/store/clothstar/order/service/OrderSellerService.java index 62c3e25d..8ddf25f2 100644 --- a/src/main/java/org/store/clothstar/order/service/OrderSellerService.java +++ b/src/main/java/org/store/clothstar/order/service/OrderSellerService.java @@ -9,7 +9,6 @@ import org.store.clothstar.order.dto.reponse.OrderResponse; import org.store.clothstar.order.entity.OrderEntity; import org.store.clothstar.order.repository.order.OrderRepository; -import org.store.clothstar.order.repository.orderSeller.JpaOrderSellerRepository; import org.store.clothstar.order.repository.orderSeller.OrderSellerRepository; import org.store.clothstar.order.type.Status; import org.store.clothstar.orderDetail.service.OrderDetailService; diff --git a/src/main/java/org/store/clothstar/product/domain/Product.java b/src/main/java/org/store/clothstar/product/domain/Product.java index c541f514..74cc7a05 100644 --- a/src/main/java/org/store/clothstar/product/domain/Product.java +++ b/src/main/java/org/store/clothstar/product/domain/Product.java @@ -1,9 +1,7 @@ package org.store.clothstar.product.domain; import lombok.*; -import org.store.clothstar.orderDetail.entity.OrderDetailEntity; import org.store.clothstar.product.dto.request.UpdateProductRequest; -import org.store.clothstar.product.dto.response.ProductResponse; import org.store.clothstar.product.entity.ProductEntity; @Builder diff --git a/src/main/java/org/store/clothstar/product/dto/request/CreateProductRequest.java b/src/main/java/org/store/clothstar/product/dto/request/CreateProductRequest.java index 493ff963..81023806 100644 --- a/src/main/java/org/store/clothstar/product/dto/request/CreateProductRequest.java +++ b/src/main/java/org/store/clothstar/product/dto/request/CreateProductRequest.java @@ -7,7 +7,6 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import org.store.clothstar.product.domain.Product; import org.store.clothstar.product.entity.ProductEntity; import org.store.clothstar.productLine.entity.ProductLineEntity; diff --git a/src/main/java/org/store/clothstar/product/dto/response/ProductResponse.java b/src/main/java/org/store/clothstar/product/dto/response/ProductResponse.java index f6f05262..a1aca4e3 100644 --- a/src/main/java/org/store/clothstar/product/dto/response/ProductResponse.java +++ b/src/main/java/org/store/clothstar/product/dto/response/ProductResponse.java @@ -2,7 +2,6 @@ import lombok.Builder; import lombok.Getter; -import org.store.clothstar.product.domain.Product; import org.store.clothstar.product.entity.ProductEntity; @Getter diff --git a/src/main/java/org/store/clothstar/product/repository/ProductJPARepository.java b/src/main/java/org/store/clothstar/product/repository/ProductJPARepository.java index aada334b..ccbdd4c0 100644 --- a/src/main/java/org/store/clothstar/product/repository/ProductJPARepository.java +++ b/src/main/java/org/store/clothstar/product/repository/ProductJPARepository.java @@ -2,7 +2,6 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import org.store.clothstar.product.domain.Product; import org.store.clothstar.product.entity.ProductEntity; import java.util.List; diff --git a/src/main/java/org/store/clothstar/product/service/ProductService.java b/src/main/java/org/store/clothstar/product/service/ProductService.java index cdebec5a..a70ccecf 100644 --- a/src/main/java/org/store/clothstar/product/service/ProductService.java +++ b/src/main/java/org/store/clothstar/product/service/ProductService.java @@ -7,18 +7,14 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.server.ResponseStatusException; -import org.store.clothstar.product.domain.Product; import org.store.clothstar.product.dto.request.CreateProductRequest; import org.store.clothstar.product.dto.request.UpdateProductRequest; import org.store.clothstar.product.dto.response.ProductResponse; import org.store.clothstar.product.entity.ProductEntity; import org.store.clothstar.product.repository.ProductJPARepository; -import org.store.clothstar.product.repository.ProductRepository; import org.store.clothstar.productLine.entity.ProductLineEntity; import org.store.clothstar.productLine.repository.ProductLineJPARepository; -import java.util.stream.Collectors; - @Service @RequiredArgsConstructor public class ProductService { diff --git a/src/main/java/org/store/clothstar/productLine/controller/ProductLineController.java b/src/main/java/org/store/clothstar/productLine/controller/ProductLineController.java index 3ddd6777..ddf1e0ae 100644 --- a/src/main/java/org/store/clothstar/productLine/controller/ProductLineController.java +++ b/src/main/java/org/store/clothstar/productLine/controller/ProductLineController.java @@ -13,7 +13,6 @@ import org.store.clothstar.productLine.dto.request.UpdateProductLineRequest; import org.store.clothstar.productLine.dto.response.ProductLineResponse; import org.store.clothstar.productLine.dto.response.ProductLineWithProductsJPAResponse; -import org.store.clothstar.productLine.dto.response.ProductLineWithProductsResponse; import org.store.clothstar.productLine.service.ProductLineService; import java.net.URI; diff --git a/src/main/java/org/store/clothstar/productLine/domain/ProductLine.java b/src/main/java/org/store/clothstar/productLine/domain/ProductLine.java index 17482e96..b2413775 100644 --- a/src/main/java/org/store/clothstar/productLine/domain/ProductLine.java +++ b/src/main/java/org/store/clothstar/productLine/domain/ProductLine.java @@ -1,7 +1,6 @@ package org.store.clothstar.productLine.domain; import lombok.*; -import org.store.clothstar.product.domain.Product; import org.store.clothstar.product.entity.ProductEntity; import org.store.clothstar.productLine.domain.type.ProductLineStatus; import org.store.clothstar.productLine.dto.request.UpdateProductLineRequest; diff --git a/src/main/java/org/store/clothstar/productLine/dto/response/ProductLineWithProductsJPAResponse.java b/src/main/java/org/store/clothstar/productLine/dto/response/ProductLineWithProductsJPAResponse.java index 7cd7f9b9..bd407ae9 100644 --- a/src/main/java/org/store/clothstar/productLine/dto/response/ProductLineWithProductsJPAResponse.java +++ b/src/main/java/org/store/clothstar/productLine/dto/response/ProductLineWithProductsJPAResponse.java @@ -4,7 +4,6 @@ import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import com.querydsl.core.annotations.QueryProjection; import lombok.*; -import org.store.clothstar.category.domain.Category; import org.store.clothstar.category.dto.response.CategoryResponse; import org.store.clothstar.category.entity.CategoryEntity; import org.store.clothstar.member.dto.response.MemberSimpleResponse; diff --git a/src/main/java/org/store/clothstar/productLine/service/ProductLineService.java b/src/main/java/org/store/clothstar/productLine/service/ProductLineService.java index 112cac53..dea9e966 100644 --- a/src/main/java/org/store/clothstar/productLine/service/ProductLineService.java +++ b/src/main/java/org/store/clothstar/productLine/service/ProductLineService.java @@ -9,17 +9,12 @@ import org.store.clothstar.category.entity.CategoryEntity; import org.store.clothstar.category.repository.CategoryJpaRepository; import org.store.clothstar.member.entity.SellerEntity; -import org.store.clothstar.member.repository.SellerJpaRepository; import org.store.clothstar.member.repository.SellerRepository; -import org.store.clothstar.product.domain.Product; -import org.store.clothstar.product.entity.ProductEntity; -import org.store.clothstar.productLine.domain.ProductLine; import org.store.clothstar.productLine.domain.type.ProductLineStatus; import org.store.clothstar.productLine.dto.request.CreateProductLineRequest; import org.store.clothstar.productLine.dto.request.UpdateProductLineRequest; import org.store.clothstar.productLine.dto.response.ProductLineResponse; import org.store.clothstar.productLine.dto.response.ProductLineWithProductsJPAResponse; -import org.store.clothstar.productLine.dto.response.ProductLineWithProductsResponse; import org.store.clothstar.productLine.entity.ProductLineEntity; import org.store.clothstar.productLine.repository.ProductLineJPARepository;