From 2fba1945ce0d4dde3ee07f6ded8d76f735c12c3f Mon Sep 17 00:00:00 2001 From: suhaoh Date: Thu, 29 Aug 2024 23:35:35 +0900 Subject: [PATCH 1/9] =?UTF-8?q?[10-product-CRUD]=20DDL=20sql=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/DDL.sql | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 docs/DDL.sql diff --git a/docs/DDL.sql b/docs/DDL.sql new file mode 100644 index 0000000..c6abb27 --- /dev/null +++ b/docs/DDL.sql @@ -0,0 +1,165 @@ +-- ecommerce_site.users definition +CREATE TABLE `users` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `name` varchar(20) NOT NULL, + `password` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL, + `email` varchar(30) NOT NULL, + `status` varchar(30) NOT NULL DEFAULT 'ACTIVE', + `created_at` datetime NOT NULL, + `updated_at` datetime DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='회원'; + +-- ecommerce_site.partners definition +CREATE TABLE `partners` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `business_number` varchar(50) NOT NULL, + `name` varchar(20) NOT NULL, + `password` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL, + `email` varchar(30) NOT NULL, + `status` varchar(30) NOT NULL DEFAULT 'ACTIVE', + `created_at` datetime NOT NULL, + `updated_at` datetime DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='파트너'; + +-- ecommerce_site.categories definition +CREATE TABLE `categories` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `name` varchar(50) NOT NULL, + `description` varchar(100) DEFAULT NULL, + `parent_category_id` bigint DEFAULT NULL, + `created_at` datetime NOT NULL, + `updated_at` datetime DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='카테고리'; + +-- ecommerce_site.addresses definition + +CREATE TABLE `addresses` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `user_id` bigint NOT NULL, + `phone_number` varchar(11) NOT NULL, + `zip_code` varchar(5) NOT NULL, + `address` varchar(50) NOT NULL, + `address_detail` varchar(50) DEFAULT NULL, + `created_at` datetime NOT NULL, + `updated_at` datetime DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `FK_user_TO_address` (`user_id`), + CONSTRAINT `FK_user_TO_address` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='배송지'; + +-- ecommerce_site.products definition + +CREATE TABLE `products` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `partner_id` bigint NOT NULL, + `category_id` bigint NOT NULL, + `name` varchar(100) NOT NULL, + `description` json NOT NULL, + `price` bigint NOT NULL, + `stock_quantity` bigint NOT NULL DEFAULT '1', + `is_enable` tinyint(1) NOT NULL DEFAULT '1', + `created_at` datetime DEFAULT NULL, + `updated_at` datetime DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `FK_partner_TO_product` (`partner_id`), + KEY `FK_category_TO_product` (`category_id`), + CONSTRAINT `FK_category_TO_product` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`), + CONSTRAINT `FK_partner_TO_product` FOREIGN KEY (`partner_id`) REFERENCES `partners` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='상품'; + +-- ecommerce_site.carts definition + +CREATE TABLE `carts` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `user_id` bigint NOT NULL, + `product_id` bigint NOT NULL, + `quantity` bigint NOT NULL, + `created_at` datetime NOT NULL, + `updated_at` datetime DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `FK_user_TO_cart` (`user_id`), + KEY `FK_product_TO_cart` (`product_id`), + CONSTRAINT `FK_product_TO_cart` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`), + CONSTRAINT `FK_user_TO_cart` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='장바구니'; + +-- ecommerce_site.orders definition + +CREATE TABLE `orders` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `user_id` bigint NOT NULL, + `total_amount` bigint NOT NULL, + `status` varchar(30) NOT NULL DEFAULT 'PENDING', + `created_at` datetime NOT NULL, + `updated_at` datetime DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `FK_user_TO_order` (`user_id`), + CONSTRAINT `FK_user_TO_order` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='주문'; + +-- ecommerce_site.order_details definition + +CREATE TABLE `order_details` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `order_id` bigint NOT NULL, + `product_id` bigint NOT NULL, + `quantity` bigint NOT NULL, + `unit_price` bigint NOT NULL, + `created_at` datetime NOT NULL, + PRIMARY KEY (`id`), + KEY `FK_order_TO_order_detail` (`order_id`), + KEY `FK_product_TO_order_detail` (`product_id`), + CONSTRAINT `FK_order_TO_order_detail` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`), + CONSTRAINT `FK_product_TO_order_detail` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='주문 상세'; + +-- ecommerce_site.payments definition + +CREATE TABLE `payments` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `order_id` bigint NOT NULL, + `payment_method` varchar(30) NOT NULL DEFAULT 'CREDIT_CARD', + `status` varchar(30) NOT NULL DEFAULT 'PENDING', + `amount` bigint NOT NULL, + `payment_date` datetime NOT NULL, + PRIMARY KEY (`id`), + KEY `FK_order_TO_payment` (`order_id`), + CONSTRAINT `FK_order_TO_payment` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='결제'; + +-- ecommerce_site.reviews definition + +CREATE TABLE `reviews` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `user_id` bigint NOT NULL, + `product_id` bigint NOT NULL, + `rating` int NOT NULL, + `comment` longtext NOT NULL, + `created_at` datetime DEFAULT NULL, + `updated_at` datetime DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `FK_user_TO_review` (`user_id`), + KEY `FK_product_TO_review` (`product_id`), + CONSTRAINT `FK_product_TO_review` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`), + CONSTRAINT `FK_user_TO_review` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='리뷰'; + +-- ecommerce_site.shipments definition + +CREATE TABLE `shipments` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `order_id` bigint NOT NULL, + `order_detail_id` bigint NOT NULL, + `tracking_number` varchar(30) NOT NULL, + `status` varchar(30) NOT NULL DEFAULT 'SHIPPED', + `created_at` datetime NOT NULL, + `updated_at` datetime DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `FK_order_TO_shipment` (`order_id`), + KEY `FK_order_detail_TO_shipment` (`order_detail_id`), + CONSTRAINT `FK_order_detail_TO_shipment` FOREIGN KEY (`order_detail_id`) REFERENCES `order_details` (`id`), + CONSTRAINT `FK_order_TO_shipment` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='배송'; \ No newline at end of file From f1eb85d80bf332b9bd4e60510812757d588d046f Mon Sep 17 00:00:00 2001 From: suhaoh Date: Fri, 30 Aug 2024 00:40:18 +0900 Subject: [PATCH 2/9] =?UTF-8?q?[10-prduct-CRUD]=20=EC=83=81=ED=92=88=20?= =?UTF-8?q?=EB=93=B1=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 상품 등록을 위한 카테고리 조회 - 상품 등록 --- build.gradle | 4 ++ .../application/category/CategoryService.java | 21 ++++++++++ .../application/partner/PartnerService.java | 1 + .../application/product/ProductFacade.java | 22 ++++++++++ .../application/product/ProductService.java | 20 +++++++++ .../product/dto/ProductRequestDto.java | 33 +++++++++++++++ .../product/dto/ProductResponseDto.java | 37 ++++++++++++++++ .../application/user/UserService.java | 1 + .../commerce_site/domain/Category.java | 21 ++++++++++ .../example/commerce_site/domain/Product.java | 36 ++++++++++++++++ .../infrastructure/CategoryRepository.java | 9 ++++ .../infrastructure/PartnerRepository.java | 4 +- .../infrastructure/ProductRepository.java | 9 ++++ .../infrastructure/UserRepository.java | 2 +- .../product/ProductController.java | 25 +++++++++++ .../product/request/ProductRequest.java | 42 +++++++++++++++++++ .../product/response/ProductResponse.java | 37 ++++++++++++++++ 17 files changed, 322 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/example/commerce_site/application/category/CategoryService.java create mode 100644 src/main/java/org/example/commerce_site/application/product/ProductFacade.java create mode 100644 src/main/java/org/example/commerce_site/application/product/ProductService.java create mode 100644 src/main/java/org/example/commerce_site/application/product/dto/ProductRequestDto.java create mode 100644 src/main/java/org/example/commerce_site/application/product/dto/ProductResponseDto.java create mode 100644 src/main/java/org/example/commerce_site/domain/Category.java create mode 100644 src/main/java/org/example/commerce_site/domain/Product.java create mode 100644 src/main/java/org/example/commerce_site/infrastructure/CategoryRepository.java create mode 100644 src/main/java/org/example/commerce_site/infrastructure/ProductRepository.java create mode 100644 src/main/java/org/example/commerce_site/representation/product/ProductController.java create mode 100644 src/main/java/org/example/commerce_site/representation/product/request/ProductRequest.java create mode 100644 src/main/java/org/example/commerce_site/representation/product/response/ProductResponse.java diff --git a/build.gradle b/build.gradle index 2acc06b..bc2ccac 100644 --- a/build.gradle +++ b/build.gradle @@ -28,6 +28,10 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-validation' + + implementation 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.0' + implementation 'org.glassfish.jaxb:jaxb-runtime:4.0.0' + compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.mysql:mysql-connector-j' annotationProcessor 'org.projectlombok:lombok' diff --git a/src/main/java/org/example/commerce_site/application/category/CategoryService.java b/src/main/java/org/example/commerce_site/application/category/CategoryService.java new file mode 100644 index 0000000..636f852 --- /dev/null +++ b/src/main/java/org/example/commerce_site/application/category/CategoryService.java @@ -0,0 +1,21 @@ +package org.example.commerce_site.application.category; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.example.commerce_site.domain.Category; +import org.example.commerce_site.infrastructure.CategoryRepository; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +@RequiredArgsConstructor +public class CategoryService { + private final CategoryRepository categoryRepository; + + public Category getCategoryById(Long id) { + return categoryRepository.findById(id).orElseThrow( + //TODO exception 재정의 + () -> new RuntimeException("Category with id " + id + " not found") + ); + } +} diff --git a/src/main/java/org/example/commerce_site/application/partner/PartnerService.java b/src/main/java/org/example/commerce_site/application/partner/PartnerService.java index a60496f..f01bfba 100644 --- a/src/main/java/org/example/commerce_site/application/partner/PartnerService.java +++ b/src/main/java/org/example/commerce_site/application/partner/PartnerService.java @@ -14,6 +14,7 @@ public class PartnerService { private final PartnerRepository partnerRepository; public PartnerResponseDto.Create create(PartnerRequestDto.Create dto) { + // TODO : email 중복 체크 return PartnerResponseDto.Create.of(partnerRepository.save(PartnerRequestDto.Create.toEntity(dto))); } } diff --git a/src/main/java/org/example/commerce_site/application/product/ProductFacade.java b/src/main/java/org/example/commerce_site/application/product/ProductFacade.java new file mode 100644 index 0000000..4810f56 --- /dev/null +++ b/src/main/java/org/example/commerce_site/application/product/ProductFacade.java @@ -0,0 +1,22 @@ +package org.example.commerce_site.application.product; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.example.commerce_site.application.category.CategoryService; +import org.example.commerce_site.application.product.dto.ProductRequestDto; +import org.example.commerce_site.application.product.dto.ProductResponseDto; +import org.example.commerce_site.domain.Category; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +@RequiredArgsConstructor +public class ProductFacade { + private final ProductService productService; + private final CategoryService categoryService; + + public ProductResponseDto.Create createProduct(ProductRequestDto.Create request) { + Category category = categoryService.getCategoryById(request.getCategoryId()); + return ProductResponseDto.Create.of(productService.create(request, category)); + } +} diff --git a/src/main/java/org/example/commerce_site/application/product/ProductService.java b/src/main/java/org/example/commerce_site/application/product/ProductService.java new file mode 100644 index 0000000..a3fadab --- /dev/null +++ b/src/main/java/org/example/commerce_site/application/product/ProductService.java @@ -0,0 +1,20 @@ +package org.example.commerce_site.application.product; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.example.commerce_site.application.product.dto.ProductRequestDto; +import org.example.commerce_site.domain.Category; +import org.example.commerce_site.domain.Product; +import org.example.commerce_site.infrastructure.ProductRepository; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +@RequiredArgsConstructor +public class ProductService { + private final ProductRepository productRepository; + + public Product create(ProductRequestDto.Create productRequest, Category category) { + return productRepository.save(ProductRequestDto.Create.toEntity(productRequest, category)); + } +} diff --git a/src/main/java/org/example/commerce_site/application/product/dto/ProductRequestDto.java b/src/main/java/org/example/commerce_site/application/product/dto/ProductRequestDto.java new file mode 100644 index 0000000..c26a5f1 --- /dev/null +++ b/src/main/java/org/example/commerce_site/application/product/dto/ProductRequestDto.java @@ -0,0 +1,33 @@ +package org.example.commerce_site.application.product.dto; + +import lombok.Builder; +import lombok.Getter; +import lombok.ToString; +import org.example.commerce_site.domain.Category; +import org.example.commerce_site.domain.Product; + +public class ProductRequestDto { + @Getter + @Builder + @ToString + public static class Create { + private Long partnerId; + private Long categoryId; + private String name; + private String description; + private Long price; + private Long stockQuantity; + + public static Product toEntity(ProductRequestDto.Create dto, Category category) { + return Product.builder() + .partnerId(dto.getPartnerId()) + .category(category) + .name(dto.getName()) + .description(dto.getDescription()) + .price(dto.getPrice()) + .stockQuantity(dto.getStockQuantity()) + .isEnable(Boolean.TRUE) + .build(); + } + } +} diff --git a/src/main/java/org/example/commerce_site/application/product/dto/ProductResponseDto.java b/src/main/java/org/example/commerce_site/application/product/dto/ProductResponseDto.java new file mode 100644 index 0000000..193ee7b --- /dev/null +++ b/src/main/java/org/example/commerce_site/application/product/dto/ProductResponseDto.java @@ -0,0 +1,37 @@ +package org.example.commerce_site.application.product.dto; + +import lombok.Builder; +import lombok.Getter; +import lombok.ToString; +import org.example.commerce_site.domain.Product; + +import java.time.LocalDateTime; + +public class ProductResponseDto { + @Builder + @Getter + @ToString + public static class Create { + private Long id; + private Long partnerId; + private Long categoryId; + private String name; + private String description; + private Long price; + private Long stockQuantity; + private LocalDateTime createdAt; + + public static ProductResponseDto.Create of(Product product) { + return Create.builder() + .id(product.getId()) + .partnerId(product.getPartnerId()) + .categoryId(product.getCategory().getId()) + .name(product.getName()) + .description(product.getDescription()) + .price(product.getPrice()) + .stockQuantity(product.getStockQuantity()) + .createdAt(product.getCreatedAt()) + .build(); + } + } +} diff --git a/src/main/java/org/example/commerce_site/application/user/UserService.java b/src/main/java/org/example/commerce_site/application/user/UserService.java index f0928a0..66e7a00 100644 --- a/src/main/java/org/example/commerce_site/application/user/UserService.java +++ b/src/main/java/org/example/commerce_site/application/user/UserService.java @@ -14,6 +14,7 @@ public class UserService { private final UserRepository userRepository; public UserResponseDto.Create create(UserRequestDto.Create dto) { + // TODO : email 중복 체크 return UserResponseDto.Create.of(userRepository.save(UserRequestDto.Create.toEntity(dto))); } } diff --git a/src/main/java/org/example/commerce_site/domain/Category.java b/src/main/java/org/example/commerce_site/domain/Category.java new file mode 100644 index 0000000..d8ed97f --- /dev/null +++ b/src/main/java/org/example/commerce_site/domain/Category.java @@ -0,0 +1,21 @@ +package org.example.commerce_site.domain; + +import jakarta.persistence.Entity; +import jakarta.persistence.Table; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.experimental.SuperBuilder; +import org.example.commerce_site.common.domain.BaseTimeEntity; + +@Entity +@Getter +@SuperBuilder +@NoArgsConstructor +@AllArgsConstructor +@Table(name = "categories") +public class Category extends BaseTimeEntity { + private String name; + private String description; + private Long parentCategoryId; +} diff --git a/src/main/java/org/example/commerce_site/domain/Product.java b/src/main/java/org/example/commerce_site/domain/Product.java new file mode 100644 index 0000000..05ca6e6 --- /dev/null +++ b/src/main/java/org/example/commerce_site/domain/Product.java @@ -0,0 +1,36 @@ +package org.example.commerce_site.domain; + +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.experimental.SuperBuilder; +import org.example.commerce_site.common.domain.BaseTimeEntity; +import org.hibernate.annotations.JdbcTypeCode; +import org.hibernate.type.SqlTypes; + +@Entity +@Getter +@SuperBuilder +@NoArgsConstructor +@AllArgsConstructor +@Table(name = "products") +public class Product extends BaseTimeEntity { + private Long partnerId; + + @ManyToOne + @JoinColumn(name = "category_id") + private Category category; + + private String name; + + @JdbcTypeCode(SqlTypes.JSON) + @Column(columnDefinition = "json") + private String description; + + private Long price; + + private Long stockQuantity; + + private Boolean isEnable; +} diff --git a/src/main/java/org/example/commerce_site/infrastructure/CategoryRepository.java b/src/main/java/org/example/commerce_site/infrastructure/CategoryRepository.java new file mode 100644 index 0000000..2afec98 --- /dev/null +++ b/src/main/java/org/example/commerce_site/infrastructure/CategoryRepository.java @@ -0,0 +1,9 @@ +package org.example.commerce_site.infrastructure; + +import org.example.commerce_site.domain.Category; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CategoryRepository extends JpaRepository { +} diff --git a/src/main/java/org/example/commerce_site/infrastructure/PartnerRepository.java b/src/main/java/org/example/commerce_site/infrastructure/PartnerRepository.java index 4d7b0e7..e7981c9 100644 --- a/src/main/java/org/example/commerce_site/infrastructure/PartnerRepository.java +++ b/src/main/java/org/example/commerce_site/infrastructure/PartnerRepository.java @@ -2,6 +2,8 @@ import org.example.commerce_site.domain.Partner; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; -public interface PartnerRepository extends JpaRepository { +@Repository +public interface PartnerRepository extends JpaRepository { } diff --git a/src/main/java/org/example/commerce_site/infrastructure/ProductRepository.java b/src/main/java/org/example/commerce_site/infrastructure/ProductRepository.java new file mode 100644 index 0000000..6b4ba10 --- /dev/null +++ b/src/main/java/org/example/commerce_site/infrastructure/ProductRepository.java @@ -0,0 +1,9 @@ +package org.example.commerce_site.infrastructure; + +import org.example.commerce_site.domain.Product; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ProductRepository extends JpaRepository { +} diff --git a/src/main/java/org/example/commerce_site/infrastructure/UserRepository.java b/src/main/java/org/example/commerce_site/infrastructure/UserRepository.java index 05a27ed..a7e05ac 100644 --- a/src/main/java/org/example/commerce_site/infrastructure/UserRepository.java +++ b/src/main/java/org/example/commerce_site/infrastructure/UserRepository.java @@ -5,5 +5,5 @@ import org.springframework.stereotype.Repository; @Repository -public interface UserRepository extends JpaRepository { +public interface UserRepository extends JpaRepository { } diff --git a/src/main/java/org/example/commerce_site/representation/product/ProductController.java b/src/main/java/org/example/commerce_site/representation/product/ProductController.java new file mode 100644 index 0000000..661b3f8 --- /dev/null +++ b/src/main/java/org/example/commerce_site/representation/product/ProductController.java @@ -0,0 +1,25 @@ +package org.example.commerce_site.representation.product; + +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.example.commerce_site.application.product.ProductFacade; +import org.example.commerce_site.representation.product.request.ProductRequest; +import org.example.commerce_site.representation.product.response.ProductResponse; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + + +@RestController +@RequiredArgsConstructor +@RequestMapping("/product") +public class ProductController { + private final ProductFacade productFacade; + + @PostMapping() + public ProductResponse.Create createProduct(@Valid @RequestBody ProductRequest.Create request) { + //TODO Partner 회원 외에는 접근할 수 없는 API 임 + return ProductResponse.Create.of(productFacade.createProduct(ProductRequest.Create.toDTO(request))); + } +} diff --git a/src/main/java/org/example/commerce_site/representation/product/request/ProductRequest.java b/src/main/java/org/example/commerce_site/representation/product/request/ProductRequest.java new file mode 100644 index 0000000..f92534c --- /dev/null +++ b/src/main/java/org/example/commerce_site/representation/product/request/ProductRequest.java @@ -0,0 +1,42 @@ +package org.example.commerce_site.representation.product.request; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import lombok.Getter; +import lombok.ToString; +import org.example.commerce_site.application.product.dto.ProductRequestDto; + +public class ProductRequest { + @Getter + @ToString + public static class Create { + @NotNull + private Long partnerId; + + @NotNull + private Long categoryId; + + @NotBlank + private String name; + + private String description; + + @NotNull + private Long price; + + @NotNull + private Long stockQuantity; + + public static ProductRequestDto.Create toDTO(ProductRequest.Create request) { + + return ProductRequestDto.Create.builder() + .partnerId(request.getPartnerId()) + .categoryId(request.getCategoryId()) + .name(request.getName()) + .description(request.getDescription()) + .price(request.getPrice()) + .stockQuantity(request.getStockQuantity()) + .build(); + } + } +} diff --git a/src/main/java/org/example/commerce_site/representation/product/response/ProductResponse.java b/src/main/java/org/example/commerce_site/representation/product/response/ProductResponse.java new file mode 100644 index 0000000..e64a35f --- /dev/null +++ b/src/main/java/org/example/commerce_site/representation/product/response/ProductResponse.java @@ -0,0 +1,37 @@ +package org.example.commerce_site.representation.product.response; + +import lombok.Builder; +import lombok.Getter; +import lombok.ToString; +import org.example.commerce_site.application.product.dto.ProductResponseDto; + +import java.time.LocalDateTime; + +public class ProductResponse { + @Getter + @Builder + @ToString + public static class Create { + private Long id; + private Long partnerId; + private Long categoryId; + private String name; + private String description; + private Long price; + private Long stockQuantity; + private LocalDateTime createdAt; + + public static ProductResponse.Create of(ProductResponseDto.Create dto) { + return ProductResponse.Create.builder() + .id(dto.getId()) + .partnerId(dto.getPartnerId()) + .categoryId(dto.getCategoryId()) + .name(dto.getName()) + .description(dto.getDescription()) + .price(dto.getPrice()) + .stockQuantity(dto.getStockQuantity()) + .createdAt(dto.getCreatedAt()) + .build(); + } + } +} From 6bf3bb1c484af237efe16b4189e971221d0b65d5 Mon Sep 17 00:00:00 2001 From: suhaoh Date: Fri, 30 Aug 2024 19:59:38 +0900 Subject: [PATCH 3/9] =?UTF-8?q?[6]=20@superBuilder=20=EB=A5=BC=20@Builder?= =?UTF-8?q?=20=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/commerce_site/common/domain/BaseTimeEntity.java | 2 -- .../org/example/commerce_site/common/domain/IdKeyEntity.java | 2 -- src/main/java/org/example/commerce_site/domain/Partner.java | 4 ++-- src/main/java/org/example/commerce_site/domain/User.java | 4 ++-- 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/example/commerce_site/common/domain/BaseTimeEntity.java b/src/main/java/org/example/commerce_site/common/domain/BaseTimeEntity.java index 7fea193..f3f7a13 100644 --- a/src/main/java/org/example/commerce_site/common/domain/BaseTimeEntity.java +++ b/src/main/java/org/example/commerce_site/common/domain/BaseTimeEntity.java @@ -7,7 +7,6 @@ import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; -import lombok.experimental.SuperBuilder; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; @@ -16,7 +15,6 @@ @Getter @MappedSuperclass -@SuperBuilder @EntityListeners(AuditingEntityListener.class) @AllArgsConstructor @NoArgsConstructor(access = AccessLevel.PROTECTED) diff --git a/src/main/java/org/example/commerce_site/common/domain/IdKeyEntity.java b/src/main/java/org/example/commerce_site/common/domain/IdKeyEntity.java index 001e9c5..a87dc31 100644 --- a/src/main/java/org/example/commerce_site/common/domain/IdKeyEntity.java +++ b/src/main/java/org/example/commerce_site/common/domain/IdKeyEntity.java @@ -8,11 +8,9 @@ import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; -import lombok.experimental.SuperBuilder; @Getter @MappedSuperclass -@SuperBuilder @NoArgsConstructor(access = AccessLevel.PROTECTED) @AllArgsConstructor public abstract class IdKeyEntity { diff --git a/src/main/java/org/example/commerce_site/domain/Partner.java b/src/main/java/org/example/commerce_site/domain/Partner.java index b35a1ef..7e50b75 100644 --- a/src/main/java/org/example/commerce_site/domain/Partner.java +++ b/src/main/java/org/example/commerce_site/domain/Partner.java @@ -5,15 +5,15 @@ import jakarta.persistence.Enumerated; import jakarta.persistence.Table; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import lombok.experimental.SuperBuilder; import org.example.commerce_site.attribute.PartnerStatus; import org.example.commerce_site.common.domain.BaseTimeEntity; @Entity @Getter -@SuperBuilder +@Builder @NoArgsConstructor @AllArgsConstructor @Table(name = "partners") diff --git a/src/main/java/org/example/commerce_site/domain/User.java b/src/main/java/org/example/commerce_site/domain/User.java index 7bbe2c4..a46519e 100644 --- a/src/main/java/org/example/commerce_site/domain/User.java +++ b/src/main/java/org/example/commerce_site/domain/User.java @@ -5,15 +5,15 @@ import jakarta.persistence.Enumerated; import jakarta.persistence.Table; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import lombok.experimental.SuperBuilder; import org.example.commerce_site.attribute.UserStatus; import org.example.commerce_site.common.domain.BaseTimeEntity; @Entity @Getter -@SuperBuilder +@Builder @NoArgsConstructor @AllArgsConstructor @Table(name = "users") From ac166a419f60e7b4074d2e52b0c7b78c2d998aab Mon Sep 17 00:00:00 2001 From: suhaoh Date: Fri, 30 Aug 2024 21:12:17 +0900 Subject: [PATCH 4/9] =?UTF-8?q?[10]=20@SuperBuilder=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/example/commerce_site/domain/Category.java | 1 - src/main/java/org/example/commerce_site/domain/Product.java | 1 - 2 files changed, 2 deletions(-) diff --git a/src/main/java/org/example/commerce_site/domain/Category.java b/src/main/java/org/example/commerce_site/domain/Category.java index d8ed97f..614830c 100644 --- a/src/main/java/org/example/commerce_site/domain/Category.java +++ b/src/main/java/org/example/commerce_site/domain/Category.java @@ -10,7 +10,6 @@ @Entity @Getter -@SuperBuilder @NoArgsConstructor @AllArgsConstructor @Table(name = "categories") diff --git a/src/main/java/org/example/commerce_site/domain/Product.java b/src/main/java/org/example/commerce_site/domain/Product.java index 05ca6e6..8364fe9 100644 --- a/src/main/java/org/example/commerce_site/domain/Product.java +++ b/src/main/java/org/example/commerce_site/domain/Product.java @@ -11,7 +11,6 @@ @Entity @Getter -@SuperBuilder @NoArgsConstructor @AllArgsConstructor @Table(name = "products") From a7267605ca7be875096c144ef915e4b36e661847 Mon Sep 17 00:00:00 2001 From: suhaoh Date: Fri, 30 Aug 2024 21:38:39 +0900 Subject: [PATCH 5/9] =?UTF-8?q?[10]=20@Builder=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/example/commerce_site/domain/Category.java | 2 ++ src/main/java/org/example/commerce_site/domain/Product.java | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/main/java/org/example/commerce_site/domain/Category.java b/src/main/java/org/example/commerce_site/domain/Category.java index 614830c..de72e95 100644 --- a/src/main/java/org/example/commerce_site/domain/Category.java +++ b/src/main/java/org/example/commerce_site/domain/Category.java @@ -3,6 +3,7 @@ import jakarta.persistence.Entity; import jakarta.persistence.Table; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; @@ -10,6 +11,7 @@ @Entity @Getter +@Builder @NoArgsConstructor @AllArgsConstructor @Table(name = "categories") diff --git a/src/main/java/org/example/commerce_site/domain/Product.java b/src/main/java/org/example/commerce_site/domain/Product.java index 8364fe9..ce53e9c 100644 --- a/src/main/java/org/example/commerce_site/domain/Product.java +++ b/src/main/java/org/example/commerce_site/domain/Product.java @@ -2,6 +2,7 @@ import jakarta.persistence.*; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; @@ -11,6 +12,7 @@ @Entity @Getter +@Builder @NoArgsConstructor @AllArgsConstructor @Table(name = "products") From a370c31afc9135854a5bb7d93ff83b99ac16869f Mon Sep 17 00:00:00 2001 From: suhaoh Date: Fri, 30 Aug 2024 21:38:52 +0900 Subject: [PATCH 6/9] =?UTF-8?q?[10]=20naver=20intellij=20formmater=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/naver-intellij-formatter.xml | 62 +++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 docs/naver-intellij-formatter.xml diff --git a/docs/naver-intellij-formatter.xml b/docs/naver-intellij-formatter.xml new file mode 100644 index 0000000..658fc65 --- /dev/null +++ b/docs/naver-intellij-formatter.xml @@ -0,0 +1,62 @@ + + + From 0af8f034a5dec7e526cd1ab5e2340af9d2210b17 Mon Sep 17 00:00:00 2001 From: suhaoh Date: Fri, 30 Aug 2024 21:45:40 +0900 Subject: [PATCH 7/9] =?UTF-8?q?[10]=20=EA=B3=B5=ED=86=B5=20=EC=98=88?= =?UTF-8?q?=EC=99=B8,=20=EC=9D=91=EB=8B=B5=20=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - CustomException - CommonResponse --- .../application/category/CategoryService.java | 5 +- .../exception/CommonExceptionHandler.java | 44 ++++++++++++++++ .../common/exception/CustomException.java | 15 ++++++ .../common/exception/ErrorCode.java | 34 ++++++++++++ .../common/response/CommonResponse.java | 52 +++++++++++++++++++ .../partner/PartnerController.java | 5 +- .../product/ProductController.java | 5 +- .../representation/user/UserController.java | 18 ++++--- 8 files changed, 164 insertions(+), 14 deletions(-) create mode 100644 src/main/java/org/example/commerce_site/common/exception/CommonExceptionHandler.java create mode 100644 src/main/java/org/example/commerce_site/common/exception/CustomException.java create mode 100644 src/main/java/org/example/commerce_site/common/exception/ErrorCode.java create mode 100644 src/main/java/org/example/commerce_site/common/response/CommonResponse.java diff --git a/src/main/java/org/example/commerce_site/application/category/CategoryService.java b/src/main/java/org/example/commerce_site/application/category/CategoryService.java index 636f852..1e8d25b 100644 --- a/src/main/java/org/example/commerce_site/application/category/CategoryService.java +++ b/src/main/java/org/example/commerce_site/application/category/CategoryService.java @@ -2,6 +2,8 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.example.commerce_site.common.exception.CustomException; +import org.example.commerce_site.common.exception.ErrorCode; import org.example.commerce_site.domain.Category; import org.example.commerce_site.infrastructure.CategoryRepository; import org.springframework.stereotype.Service; @@ -14,8 +16,7 @@ public class CategoryService { public Category getCategoryById(Long id) { return categoryRepository.findById(id).orElseThrow( - //TODO exception 재정의 - () -> new RuntimeException("Category with id " + id + " not found") + () -> new CustomException(ErrorCode.CATEGORY_NOT_FOUND) ); } } diff --git a/src/main/java/org/example/commerce_site/common/exception/CommonExceptionHandler.java b/src/main/java/org/example/commerce_site/common/exception/CommonExceptionHandler.java new file mode 100644 index 0000000..d28bb39 --- /dev/null +++ b/src/main/java/org/example/commerce_site/common/exception/CommonExceptionHandler.java @@ -0,0 +1,44 @@ +package org.example.commerce_site.common.exception; + +import lombok.extern.slf4j.Slf4j; +import org.example.commerce_site.common.response.CommonResponse; +import org.springframework.beans.TypeMismatchException; +import org.springframework.core.NestedExceptionUtils; +import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.web.HttpRequestMethodNotSupportedException; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.MissingServletRequestParameterException; +import org.springframework.web.bind.ServletRequestBindingException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.multipart.support.MissingServletRequestPartException; + +@Slf4j +@RestControllerAdvice +public class CommonExceptionHandler { + + @ExceptionHandler({MissingServletRequestParameterException.class, ServletRequestBindingException.class, + TypeMismatchException.class, HttpMessageNotReadableException.class, MissingServletRequestPartException.class, + HttpMessageNotReadableException.class, MethodArgumentNotValidException.class}) + protected ResponseEntity handleBadRequestException(final Exception e) { + ErrorCode errorCode = ErrorCode.BAD_REQUEST; + log.warn("[BadRequestException] error code : {}, error message : {}", errorCode, NestedExceptionUtils.getMostSpecificCause(e).getMessage()); + return CommonResponse.fail(errorCode); + } + + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) + protected ResponseEntity handleHttpRequestMethodNotSupportedException( + final HttpRequestMethodNotSupportedException e) { + log.error("[HttpRequestMethodNotSupportedException]", e); + ErrorCode errorCode = ErrorCode.METHOD_NOT_ALLOWED; + return CommonResponse.fail(errorCode); + } + + @ExceptionHandler(CustomException.class) + protected ResponseEntity handleCustomException(CustomException e) { + ErrorCode errorCode = e.getErrorCode(); + log.warn("[CustomException] error code : {}, error message : {}", errorCode, NestedExceptionUtils.getMostSpecificCause(e).getMessage()); + return CommonResponse.fail(errorCode); + } +} \ No newline at end of file diff --git a/src/main/java/org/example/commerce_site/common/exception/CustomException.java b/src/main/java/org/example/commerce_site/common/exception/CustomException.java new file mode 100644 index 0000000..97fb9e3 --- /dev/null +++ b/src/main/java/org/example/commerce_site/common/exception/CustomException.java @@ -0,0 +1,15 @@ +package org.example.commerce_site.common.exception; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class CustomException extends RuntimeException { + private final ErrorCode errorCode; + + public CustomException(String message, ErrorCode errorCode) { + super(message); + this.errorCode = errorCode; + } +} diff --git a/src/main/java/org/example/commerce_site/common/exception/ErrorCode.java b/src/main/java/org/example/commerce_site/common/exception/ErrorCode.java new file mode 100644 index 0000000..25b8884 --- /dev/null +++ b/src/main/java/org/example/commerce_site/common/exception/ErrorCode.java @@ -0,0 +1,34 @@ +package org.example.commerce_site.common.exception; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import org.springframework.http.HttpStatus; + +@Getter +@AllArgsConstructor +public enum ErrorCode { + //API RuntimeException + NOT_FOUND(HttpStatus.NOT_FOUND, 404, "데이터를 찾을 수 없습니다."), + INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, 500, "내부 서버 오류."), + UNAUTHORIZED(HttpStatus.UNAUTHORIZED, 401, "접근 할수없습니다."), + BAD_REQUEST(HttpStatus.BAD_REQUEST, 400, "잘못된 요청입니다."), + INVALID_PARAM(HttpStatus.BAD_REQUEST, 400, "잘못된 parameter 입니다."), + ACCESS_DENIED(HttpStatus.FORBIDDEN, 403, "권한이 부족합니다."), + METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, 405, "허용되지 않은 메소드 입니다."), + + //user + USER_NOT_FOUND(HttpStatus.NOT_FOUND, 404, "회원 정보를 찾을 수 없습니다."), + + //partner + PARTNER_NOT_FOUND(HttpStatus.NOT_FOUND, 404, "파트너 회원 정보를 찾을 수 없습니다."), + + //product + PRODUCT_NOT_FOUND(HttpStatus.NOT_FOUND, 404, "상품 정보를 찾을 수 없습니다."), + + //category + CATEGORY_NOT_FOUND(HttpStatus.NOT_FOUND, 404, "카테고리 정보를 찾을 수 없습니다."); + + private final HttpStatus httpStatus; + private final int code; + private final String message; +} diff --git a/src/main/java/org/example/commerce_site/common/response/CommonResponse.java b/src/main/java/org/example/commerce_site/common/response/CommonResponse.java new file mode 100644 index 0000000..7a5d33e --- /dev/null +++ b/src/main/java/org/example/commerce_site/common/response/CommonResponse.java @@ -0,0 +1,52 @@ +package org.example.commerce_site.common.response; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.example.commerce_site.common.exception.ErrorCode; +import org.springframework.http.ResponseEntity; + +@Getter +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class CommonResponse { + private static final String MESSAGE_SUCCESS = "SUCCESS"; + private int code; + private String message; + + public static CommonResponse.CommonData success(T data) { + return (CommonResponse.CommonData) CommonData.builder() + .message(MESSAGE_SUCCESS) + .data(data) + .build(); + } + + //for void api + public static CommonResponse success() { + return CommonResponse.builder() + .message(MESSAGE_SUCCESS) + .build(); + } + + public static ResponseEntity fail(ErrorCode errorCode) { + return (ResponseEntity) ResponseEntity + .status(errorCode.getHttpStatus()) + .body(CommonResponse.builder() + .code(errorCode.getCode()) + .message(errorCode.getMessage()) + .build() + ); + } + + @Getter + @Builder + @NoArgsConstructor + @AllArgsConstructor + public static class CommonData { + private int code; + private String message; + private T data; + } +} diff --git a/src/main/java/org/example/commerce_site/representation/partner/PartnerController.java b/src/main/java/org/example/commerce_site/representation/partner/PartnerController.java index 459339f..8b36b15 100644 --- a/src/main/java/org/example/commerce_site/representation/partner/PartnerController.java +++ b/src/main/java/org/example/commerce_site/representation/partner/PartnerController.java @@ -4,6 +4,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.example.commerce_site.application.partner.PartnerService; +import org.example.commerce_site.common.response.CommonResponse; import org.example.commerce_site.representation.partner.request.PartnerRequest; import org.example.commerce_site.representation.partner.response.PartnerResponse; import org.springframework.web.bind.annotation.PostMapping; @@ -19,7 +20,7 @@ public class PartnerController { private final PartnerService partnerService; @PostMapping() - public PartnerResponse.Create createPartner(@Valid @RequestBody PartnerRequest.Create request) { - return PartnerResponse.Create.of(partnerService.create(PartnerRequest.Create.toDTO(request))); + public CommonResponse.CommonData createPartner(@Valid @RequestBody PartnerRequest.Create request) { + return CommonResponse.success(PartnerResponse.Create.of(partnerService.create(PartnerRequest.Create.toDTO(request)))); } } diff --git a/src/main/java/org/example/commerce_site/representation/product/ProductController.java b/src/main/java/org/example/commerce_site/representation/product/ProductController.java index 661b3f8..64f7b7c 100644 --- a/src/main/java/org/example/commerce_site/representation/product/ProductController.java +++ b/src/main/java/org/example/commerce_site/representation/product/ProductController.java @@ -3,6 +3,7 @@ import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.example.commerce_site.application.product.ProductFacade; +import org.example.commerce_site.common.response.CommonResponse; import org.example.commerce_site.representation.product.request.ProductRequest; import org.example.commerce_site.representation.product.response.ProductResponse; import org.springframework.web.bind.annotation.PostMapping; @@ -18,8 +19,8 @@ public class ProductController { private final ProductFacade productFacade; @PostMapping() - public ProductResponse.Create createProduct(@Valid @RequestBody ProductRequest.Create request) { + public CommonResponse.CommonData createProduct(@Valid @RequestBody ProductRequest.Create request) { //TODO Partner 회원 외에는 접근할 수 없는 API 임 - return ProductResponse.Create.of(productFacade.createProduct(ProductRequest.Create.toDTO(request))); + return CommonResponse.success(ProductResponse.Create.of(productFacade.createProduct(ProductRequest.Create.toDTO(request)))); } } diff --git a/src/main/java/org/example/commerce_site/representation/user/UserController.java b/src/main/java/org/example/commerce_site/representation/user/UserController.java index 14f2b13..037b966 100644 --- a/src/main/java/org/example/commerce_site/representation/user/UserController.java +++ b/src/main/java/org/example/commerce_site/representation/user/UserController.java @@ -1,9 +1,7 @@ package org.example.commerce_site.representation.user; -import jakarta.validation.Valid; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import org.example.commerce_site.application.user.UserService; +import org.example.commerce_site.common.response.CommonResponse; import org.example.commerce_site.representation.user.request.UserRequest; import org.example.commerce_site.representation.user.response.UserResponse; import org.springframework.web.bind.annotation.PostMapping; @@ -11,15 +9,19 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + @Slf4j @RestController @RequiredArgsConstructor @RequestMapping("/user") public class UserController { - private final UserService userService; + private final UserService userService; - @PostMapping() - public UserResponse.Create createUser(@Valid @RequestBody UserRequest.Create request) { - return UserResponse.Create.of(userService.create(UserRequest.Create.toDTO(request))); - } + @PostMapping() + public CommonResponse.CommonData createUser(@Valid @RequestBody UserRequest.Create request) { + return CommonResponse.success(UserResponse.Create.of(userService.create(UserRequest.Create.toDTO(request)))); + } } From 73ada8c310e89cdfce55c1b347ab6778643a717b Mon Sep 17 00:00:00 2001 From: suhaoh Date: Fri, 30 Aug 2024 21:46:08 +0900 Subject: [PATCH 8/9] [10] code formatter --- .../CommerceSiteApplication.java | 6 +- .../application/category/CategoryService.java | 17 ++--- .../application/partner/PartnerService.java | 15 ++-- .../partner/dto/PartnerRequestDto.java | 41 +++++------ .../partner/dto/PartnerResponseDto.java | 33 ++++----- .../application/product/ProductFacade.java | 17 ++--- .../application/product/ProductService.java | 13 ++-- .../product/dto/ProductRequestDto.java | 49 ++++++------- .../product/dto/ProductResponseDto.java | 57 +++++++-------- .../application/user/UserService.java | 15 ++-- .../application/user/dto/UserRequestDto.java | 37 +++++----- .../application/user/dto/UserResponseDto.java | 29 ++++---- .../attribute/PartnerStatus.java | 6 +- .../commerce_site/attribute/UserStatus.java | 6 +- .../common/domain/BaseTimeEntity.java | 23 +++--- .../common/domain/IdKeyEntity.java | 6 +- .../exception/CommonExceptionHandler.java | 47 ++++++------ .../common/exception/CustomException.java | 10 +-- .../common/exception/ErrorCode.java | 51 ++++++------- .../common/response/CommonResponse.java | 71 ++++++++++--------- .../commerce_site/domain/Category.java | 10 +-- .../example/commerce_site/domain/Partner.java | 17 ++--- .../example/commerce_site/domain/Product.java | 36 +++++----- .../example/commerce_site/domain/User.java | 15 ++-- .../partner/PartnerController.java | 19 ++--- .../partner/request/PartnerRequest.java | 49 ++++++------- .../partner/response/PartnerResponse.java | 33 ++++----- .../product/ProductController.java | 18 ++--- .../product/request/ProductRequest.java | 67 ++++++++--------- .../product/response/ProductResponse.java | 57 +++++++-------- .../user/request/UserRequest.java | 43 +++++------ .../user/response/UserResponse.java | 29 ++++---- 32 files changed, 488 insertions(+), 454 deletions(-) diff --git a/src/main/java/org/example/commerce_site/CommerceSiteApplication.java b/src/main/java/org/example/commerce_site/CommerceSiteApplication.java index 4bbeee8..79c08ce 100644 --- a/src/main/java/org/example/commerce_site/CommerceSiteApplication.java +++ b/src/main/java/org/example/commerce_site/CommerceSiteApplication.java @@ -8,7 +8,7 @@ @EnableJpaAuditing @SpringBootApplication(exclude = SecurityAutoConfiguration.class) public class CommerceSiteApplication { - public static void main(String[] args) { - SpringApplication.run(CommerceSiteApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(CommerceSiteApplication.class, args); + } } diff --git a/src/main/java/org/example/commerce_site/application/category/CategoryService.java b/src/main/java/org/example/commerce_site/application/category/CategoryService.java index 1e8d25b..0c86c1b 100644 --- a/src/main/java/org/example/commerce_site/application/category/CategoryService.java +++ b/src/main/java/org/example/commerce_site/application/category/CategoryService.java @@ -1,22 +1,23 @@ package org.example.commerce_site.application.category; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import org.example.commerce_site.common.exception.CustomException; import org.example.commerce_site.common.exception.ErrorCode; import org.example.commerce_site.domain.Category; import org.example.commerce_site.infrastructure.CategoryRepository; import org.springframework.stereotype.Service; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + @Slf4j @Service @RequiredArgsConstructor public class CategoryService { - private final CategoryRepository categoryRepository; + private final CategoryRepository categoryRepository; - public Category getCategoryById(Long id) { - return categoryRepository.findById(id).orElseThrow( - () -> new CustomException(ErrorCode.CATEGORY_NOT_FOUND) - ); - } + public Category getCategoryById(Long id) { + return categoryRepository.findById(id).orElseThrow( + () -> new CustomException(ErrorCode.CATEGORY_NOT_FOUND) + ); + } } diff --git a/src/main/java/org/example/commerce_site/application/partner/PartnerService.java b/src/main/java/org/example/commerce_site/application/partner/PartnerService.java index f01bfba..1322573 100644 --- a/src/main/java/org/example/commerce_site/application/partner/PartnerService.java +++ b/src/main/java/org/example/commerce_site/application/partner/PartnerService.java @@ -1,20 +1,21 @@ package org.example.commerce_site.application.partner; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import org.example.commerce_site.application.partner.dto.PartnerRequestDto; import org.example.commerce_site.application.partner.dto.PartnerResponseDto; import org.example.commerce_site.infrastructure.PartnerRepository; import org.springframework.stereotype.Service; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + @Slf4j @Service @RequiredArgsConstructor public class PartnerService { - private final PartnerRepository partnerRepository; + private final PartnerRepository partnerRepository; - public PartnerResponseDto.Create create(PartnerRequestDto.Create dto) { - // TODO : email 중복 체크 - return PartnerResponseDto.Create.of(partnerRepository.save(PartnerRequestDto.Create.toEntity(dto))); - } + public PartnerResponseDto.Create create(PartnerRequestDto.Create dto) { + // TODO : email 중복 체크 + return PartnerResponseDto.Create.of(partnerRepository.save(PartnerRequestDto.Create.toEntity(dto))); + } } diff --git a/src/main/java/org/example/commerce_site/application/partner/dto/PartnerRequestDto.java b/src/main/java/org/example/commerce_site/application/partner/dto/PartnerRequestDto.java index 3b6c13e..827e463 100644 --- a/src/main/java/org/example/commerce_site/application/partner/dto/PartnerRequestDto.java +++ b/src/main/java/org/example/commerce_site/application/partner/dto/PartnerRequestDto.java @@ -1,29 +1,30 @@ package org.example.commerce_site.application.partner.dto; +import org.example.commerce_site.attribute.PartnerStatus; +import org.example.commerce_site.domain.Partner; + import lombok.Builder; import lombok.Getter; import lombok.ToString; -import org.example.commerce_site.attribute.PartnerStatus; -import org.example.commerce_site.domain.Partner; public class PartnerRequestDto { - @Getter - @Builder - @ToString - public static class Create { - private String name; - private String email; - private String password; - private String businessNumber; + @Getter + @Builder + @ToString + public static class Create { + private String name; + private String email; + private String password; + private String businessNumber; - public static Partner toEntity(PartnerRequestDto.Create dto) { - return Partner.builder() - .name(dto.getName()) - .email(dto.getEmail()) - .password(dto.getPassword()) - .status(PartnerStatus.ACTIVE) - .businessNumber(dto.getBusinessNumber()) - .build(); - } - } + public static Partner toEntity(PartnerRequestDto.Create dto) { + return Partner.builder() + .name(dto.getName()) + .email(dto.getEmail()) + .password(dto.getPassword()) + .status(PartnerStatus.ACTIVE) + .businessNumber(dto.getBusinessNumber()) + .build(); + } + } } diff --git a/src/main/java/org/example/commerce_site/application/partner/dto/PartnerResponseDto.java b/src/main/java/org/example/commerce_site/application/partner/dto/PartnerResponseDto.java index ada7d08..19d190d 100644 --- a/src/main/java/org/example/commerce_site/application/partner/dto/PartnerResponseDto.java +++ b/src/main/java/org/example/commerce_site/application/partner/dto/PartnerResponseDto.java @@ -1,25 +1,26 @@ package org.example.commerce_site.application.partner.dto; +import org.example.commerce_site.domain.Partner; + import lombok.Builder; import lombok.Getter; import lombok.ToString; -import org.example.commerce_site.domain.Partner; public class PartnerResponseDto { - @Builder - @Getter - @ToString - public static class Create { - private String name; - private String email; - private String businessNumber; + @Builder + @Getter + @ToString + public static class Create { + private String name; + private String email; + private String businessNumber; - public static PartnerResponseDto.Create of(Partner partner) { - return Create.builder() - .name(partner.getName()) - .email(partner.getEmail()) - .businessNumber(partner.getBusinessNumber()) - .build(); - } - } + public static PartnerResponseDto.Create of(Partner partner) { + return Create.builder() + .name(partner.getName()) + .email(partner.getEmail()) + .businessNumber(partner.getBusinessNumber()) + .build(); + } + } } diff --git a/src/main/java/org/example/commerce_site/application/product/ProductFacade.java b/src/main/java/org/example/commerce_site/application/product/ProductFacade.java index 4810f56..7e23aa4 100644 --- a/src/main/java/org/example/commerce_site/application/product/ProductFacade.java +++ b/src/main/java/org/example/commerce_site/application/product/ProductFacade.java @@ -1,22 +1,23 @@ package org.example.commerce_site.application.product; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import org.example.commerce_site.application.category.CategoryService; import org.example.commerce_site.application.product.dto.ProductRequestDto; import org.example.commerce_site.application.product.dto.ProductResponseDto; import org.example.commerce_site.domain.Category; import org.springframework.stereotype.Service; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + @Slf4j @Service @RequiredArgsConstructor public class ProductFacade { - private final ProductService productService; - private final CategoryService categoryService; + private final ProductService productService; + private final CategoryService categoryService; - public ProductResponseDto.Create createProduct(ProductRequestDto.Create request) { - Category category = categoryService.getCategoryById(request.getCategoryId()); - return ProductResponseDto.Create.of(productService.create(request, category)); - } + public ProductResponseDto.Create createProduct(ProductRequestDto.Create request) { + Category category = categoryService.getCategoryById(request.getCategoryId()); + return ProductResponseDto.Create.of(productService.create(request, category)); + } } diff --git a/src/main/java/org/example/commerce_site/application/product/ProductService.java b/src/main/java/org/example/commerce_site/application/product/ProductService.java index a3fadab..12217bb 100644 --- a/src/main/java/org/example/commerce_site/application/product/ProductService.java +++ b/src/main/java/org/example/commerce_site/application/product/ProductService.java @@ -1,20 +1,21 @@ package org.example.commerce_site.application.product; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import org.example.commerce_site.application.product.dto.ProductRequestDto; import org.example.commerce_site.domain.Category; import org.example.commerce_site.domain.Product; import org.example.commerce_site.infrastructure.ProductRepository; import org.springframework.stereotype.Service; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + @Slf4j @Service @RequiredArgsConstructor public class ProductService { - private final ProductRepository productRepository; + private final ProductRepository productRepository; - public Product create(ProductRequestDto.Create productRequest, Category category) { - return productRepository.save(ProductRequestDto.Create.toEntity(productRequest, category)); - } + public Product create(ProductRequestDto.Create productRequest, Category category) { + return productRepository.save(ProductRequestDto.Create.toEntity(productRequest, category)); + } } diff --git a/src/main/java/org/example/commerce_site/application/product/dto/ProductRequestDto.java b/src/main/java/org/example/commerce_site/application/product/dto/ProductRequestDto.java index c26a5f1..ca768f6 100644 --- a/src/main/java/org/example/commerce_site/application/product/dto/ProductRequestDto.java +++ b/src/main/java/org/example/commerce_site/application/product/dto/ProductRequestDto.java @@ -1,33 +1,34 @@ package org.example.commerce_site.application.product.dto; +import org.example.commerce_site.domain.Category; +import org.example.commerce_site.domain.Product; + import lombok.Builder; import lombok.Getter; import lombok.ToString; -import org.example.commerce_site.domain.Category; -import org.example.commerce_site.domain.Product; public class ProductRequestDto { - @Getter - @Builder - @ToString - public static class Create { - private Long partnerId; - private Long categoryId; - private String name; - private String description; - private Long price; - private Long stockQuantity; + @Getter + @Builder + @ToString + public static class Create { + private Long partnerId; + private Long categoryId; + private String name; + private String description; + private Long price; + private Long stockQuantity; - public static Product toEntity(ProductRequestDto.Create dto, Category category) { - return Product.builder() - .partnerId(dto.getPartnerId()) - .category(category) - .name(dto.getName()) - .description(dto.getDescription()) - .price(dto.getPrice()) - .stockQuantity(dto.getStockQuantity()) - .isEnable(Boolean.TRUE) - .build(); - } - } + public static Product toEntity(ProductRequestDto.Create dto, Category category) { + return Product.builder() + .partnerId(dto.getPartnerId()) + .category(category) + .name(dto.getName()) + .description(dto.getDescription()) + .price(dto.getPrice()) + .stockQuantity(dto.getStockQuantity()) + .isEnable(Boolean.TRUE) + .build(); + } + } } diff --git a/src/main/java/org/example/commerce_site/application/product/dto/ProductResponseDto.java b/src/main/java/org/example/commerce_site/application/product/dto/ProductResponseDto.java index 193ee7b..639d1c3 100644 --- a/src/main/java/org/example/commerce_site/application/product/dto/ProductResponseDto.java +++ b/src/main/java/org/example/commerce_site/application/product/dto/ProductResponseDto.java @@ -1,37 +1,38 @@ package org.example.commerce_site.application.product.dto; +import java.time.LocalDateTime; + +import org.example.commerce_site.domain.Product; + import lombok.Builder; import lombok.Getter; import lombok.ToString; -import org.example.commerce_site.domain.Product; - -import java.time.LocalDateTime; public class ProductResponseDto { - @Builder - @Getter - @ToString - public static class Create { - private Long id; - private Long partnerId; - private Long categoryId; - private String name; - private String description; - private Long price; - private Long stockQuantity; - private LocalDateTime createdAt; + @Builder + @Getter + @ToString + public static class Create { + private Long id; + private Long partnerId; + private Long categoryId; + private String name; + private String description; + private Long price; + private Long stockQuantity; + private LocalDateTime createdAt; - public static ProductResponseDto.Create of(Product product) { - return Create.builder() - .id(product.getId()) - .partnerId(product.getPartnerId()) - .categoryId(product.getCategory().getId()) - .name(product.getName()) - .description(product.getDescription()) - .price(product.getPrice()) - .stockQuantity(product.getStockQuantity()) - .createdAt(product.getCreatedAt()) - .build(); - } - } + public static ProductResponseDto.Create of(Product product) { + return Create.builder() + .id(product.getId()) + .partnerId(product.getPartnerId()) + .categoryId(product.getCategory().getId()) + .name(product.getName()) + .description(product.getDescription()) + .price(product.getPrice()) + .stockQuantity(product.getStockQuantity()) + .createdAt(product.getCreatedAt()) + .build(); + } + } } diff --git a/src/main/java/org/example/commerce_site/application/user/UserService.java b/src/main/java/org/example/commerce_site/application/user/UserService.java index 66e7a00..bd5481c 100644 --- a/src/main/java/org/example/commerce_site/application/user/UserService.java +++ b/src/main/java/org/example/commerce_site/application/user/UserService.java @@ -1,20 +1,21 @@ package org.example.commerce_site.application.user; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import org.example.commerce_site.application.user.dto.UserRequestDto; import org.example.commerce_site.application.user.dto.UserResponseDto; import org.example.commerce_site.infrastructure.UserRepository; import org.springframework.stereotype.Service; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + @Slf4j @Service @RequiredArgsConstructor public class UserService { - private final UserRepository userRepository; + private final UserRepository userRepository; - public UserResponseDto.Create create(UserRequestDto.Create dto) { - // TODO : email 중복 체크 - return UserResponseDto.Create.of(userRepository.save(UserRequestDto.Create.toEntity(dto))); - } + public UserResponseDto.Create create(UserRequestDto.Create dto) { + // TODO : email 중복 체크 + return UserResponseDto.Create.of(userRepository.save(UserRequestDto.Create.toEntity(dto))); + } } diff --git a/src/main/java/org/example/commerce_site/application/user/dto/UserRequestDto.java b/src/main/java/org/example/commerce_site/application/user/dto/UserRequestDto.java index 7b22471..df5d37e 100644 --- a/src/main/java/org/example/commerce_site/application/user/dto/UserRequestDto.java +++ b/src/main/java/org/example/commerce_site/application/user/dto/UserRequestDto.java @@ -1,27 +1,28 @@ package org.example.commerce_site.application.user.dto; +import org.example.commerce_site.attribute.UserStatus; +import org.example.commerce_site.domain.User; + import lombok.Builder; import lombok.Getter; import lombok.ToString; -import org.example.commerce_site.attribute.UserStatus; -import org.example.commerce_site.domain.User; public class UserRequestDto { - @Getter - @Builder - @ToString - public static class Create { - private String name; - private String email; - private String password; + @Getter + @Builder + @ToString + public static class Create { + private String name; + private String email; + private String password; - public static User toEntity(UserRequestDto.Create dto) { - return User.builder() - .name(dto.getName()) - .email(dto.getEmail()) - .password(dto.getPassword()) - .status(UserStatus.ACTIVE) - .build(); - } - } + public static User toEntity(UserRequestDto.Create dto) { + return User.builder() + .name(dto.getName()) + .email(dto.getEmail()) + .password(dto.getPassword()) + .status(UserStatus.ACTIVE) + .build(); + } + } } diff --git a/src/main/java/org/example/commerce_site/application/user/dto/UserResponseDto.java b/src/main/java/org/example/commerce_site/application/user/dto/UserResponseDto.java index 3b85175..3080ac7 100644 --- a/src/main/java/org/example/commerce_site/application/user/dto/UserResponseDto.java +++ b/src/main/java/org/example/commerce_site/application/user/dto/UserResponseDto.java @@ -1,23 +1,24 @@ package org.example.commerce_site.application.user.dto; +import org.example.commerce_site.domain.User; + import lombok.Builder; import lombok.Getter; import lombok.ToString; -import org.example.commerce_site.domain.User; public class UserResponseDto { - @Builder - @Getter - @ToString - public static class Create { - private String name; - private String email; + @Builder + @Getter + @ToString + public static class Create { + private String name; + private String email; - public static UserResponseDto.Create of(User user) { - return Create.builder() - .name(user.getName()) - .email(user.getEmail()) - .build(); - } - } + public static UserResponseDto.Create of(User user) { + return Create.builder() + .name(user.getName()) + .email(user.getEmail()) + .build(); + } + } } diff --git a/src/main/java/org/example/commerce_site/attribute/PartnerStatus.java b/src/main/java/org/example/commerce_site/attribute/PartnerStatus.java index bd7e45b..702536d 100644 --- a/src/main/java/org/example/commerce_site/attribute/PartnerStatus.java +++ b/src/main/java/org/example/commerce_site/attribute/PartnerStatus.java @@ -1,7 +1,7 @@ package org.example.commerce_site.attribute; public enum PartnerStatus { - ACTIVE, - INACTIVE, - DORMANT, + ACTIVE, + INACTIVE, + DORMANT, } diff --git a/src/main/java/org/example/commerce_site/attribute/UserStatus.java b/src/main/java/org/example/commerce_site/attribute/UserStatus.java index cb5e7d1..112821a 100644 --- a/src/main/java/org/example/commerce_site/attribute/UserStatus.java +++ b/src/main/java/org/example/commerce_site/attribute/UserStatus.java @@ -1,7 +1,7 @@ package org.example.commerce_site.attribute; public enum UserStatus { - ACTIVE, - INACTIVE, - DORMANT, + ACTIVE, + INACTIVE, + DORMANT, } diff --git a/src/main/java/org/example/commerce_site/common/domain/BaseTimeEntity.java b/src/main/java/org/example/commerce_site/common/domain/BaseTimeEntity.java index f3f7a13..cd8d4d2 100644 --- a/src/main/java/org/example/commerce_site/common/domain/BaseTimeEntity.java +++ b/src/main/java/org/example/commerce_site/common/domain/BaseTimeEntity.java @@ -1,5 +1,11 @@ package org.example.commerce_site.common.domain; +import java.time.LocalDateTime; + +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + import jakarta.persistence.Column; import jakarta.persistence.EntityListeners; import jakarta.persistence.MappedSuperclass; @@ -7,11 +13,6 @@ import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; -import org.springframework.data.annotation.CreatedDate; -import org.springframework.data.annotation.LastModifiedDate; -import org.springframework.data.jpa.domain.support.AuditingEntityListener; - -import java.time.LocalDateTime; @Getter @MappedSuperclass @@ -19,11 +20,11 @@ @AllArgsConstructor @NoArgsConstructor(access = AccessLevel.PROTECTED) public abstract class BaseTimeEntity extends IdKeyEntity { - @CreatedDate - @Column(name = "created_at") - protected LocalDateTime createdAt; + @CreatedDate + @Column(name = "created_at") + protected LocalDateTime createdAt; - @LastModifiedDate - @Column(name = "updated_at") - protected LocalDateTime updatedAt; + @LastModifiedDate + @Column(name = "updated_at") + protected LocalDateTime updatedAt; } diff --git a/src/main/java/org/example/commerce_site/common/domain/IdKeyEntity.java b/src/main/java/org/example/commerce_site/common/domain/IdKeyEntity.java index a87dc31..ba91788 100644 --- a/src/main/java/org/example/commerce_site/common/domain/IdKeyEntity.java +++ b/src/main/java/org/example/commerce_site/common/domain/IdKeyEntity.java @@ -14,7 +14,7 @@ @NoArgsConstructor(access = AccessLevel.PROTECTED) @AllArgsConstructor public abstract class IdKeyEntity { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; } diff --git a/src/main/java/org/example/commerce_site/common/exception/CommonExceptionHandler.java b/src/main/java/org/example/commerce_site/common/exception/CommonExceptionHandler.java index d28bb39..22b4b4f 100644 --- a/src/main/java/org/example/commerce_site/common/exception/CommonExceptionHandler.java +++ b/src/main/java/org/example/commerce_site/common/exception/CommonExceptionHandler.java @@ -1,6 +1,5 @@ package org.example.commerce_site.common.exception; -import lombok.extern.slf4j.Slf4j; import org.example.commerce_site.common.response.CommonResponse; import org.springframework.beans.TypeMismatchException; import org.springframework.core.NestedExceptionUtils; @@ -14,31 +13,35 @@ import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.multipart.support.MissingServletRequestPartException; +import lombok.extern.slf4j.Slf4j; + @Slf4j @RestControllerAdvice public class CommonExceptionHandler { - @ExceptionHandler({MissingServletRequestParameterException.class, ServletRequestBindingException.class, - TypeMismatchException.class, HttpMessageNotReadableException.class, MissingServletRequestPartException.class, - HttpMessageNotReadableException.class, MethodArgumentNotValidException.class}) - protected ResponseEntity handleBadRequestException(final Exception e) { - ErrorCode errorCode = ErrorCode.BAD_REQUEST; - log.warn("[BadRequestException] error code : {}, error message : {}", errorCode, NestedExceptionUtils.getMostSpecificCause(e).getMessage()); - return CommonResponse.fail(errorCode); - } + @ExceptionHandler({MissingServletRequestParameterException.class, ServletRequestBindingException.class, + TypeMismatchException.class, HttpMessageNotReadableException.class, MissingServletRequestPartException.class, + HttpMessageNotReadableException.class, MethodArgumentNotValidException.class}) + protected ResponseEntity handleBadRequestException(final Exception e) { + ErrorCode errorCode = ErrorCode.BAD_REQUEST; + log.warn("[BadRequestException] error code : {}, error message : {}", errorCode, + NestedExceptionUtils.getMostSpecificCause(e).getMessage()); + return CommonResponse.fail(errorCode); + } - @ExceptionHandler(HttpRequestMethodNotSupportedException.class) - protected ResponseEntity handleHttpRequestMethodNotSupportedException( - final HttpRequestMethodNotSupportedException e) { - log.error("[HttpRequestMethodNotSupportedException]", e); - ErrorCode errorCode = ErrorCode.METHOD_NOT_ALLOWED; - return CommonResponse.fail(errorCode); - } + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) + protected ResponseEntity handleHttpRequestMethodNotSupportedException( + final HttpRequestMethodNotSupportedException e) { + log.error("[HttpRequestMethodNotSupportedException]", e); + ErrorCode errorCode = ErrorCode.METHOD_NOT_ALLOWED; + return CommonResponse.fail(errorCode); + } - @ExceptionHandler(CustomException.class) - protected ResponseEntity handleCustomException(CustomException e) { - ErrorCode errorCode = e.getErrorCode(); - log.warn("[CustomException] error code : {}, error message : {}", errorCode, NestedExceptionUtils.getMostSpecificCause(e).getMessage()); - return CommonResponse.fail(errorCode); - } + @ExceptionHandler(CustomException.class) + protected ResponseEntity handleCustomException(CustomException e) { + ErrorCode errorCode = e.getErrorCode(); + log.warn("[CustomException] error code : {}, error message : {}", errorCode, + NestedExceptionUtils.getMostSpecificCause(e).getMessage()); + return CommonResponse.fail(errorCode); + } } \ No newline at end of file diff --git a/src/main/java/org/example/commerce_site/common/exception/CustomException.java b/src/main/java/org/example/commerce_site/common/exception/CustomException.java index 97fb9e3..820a6ef 100644 --- a/src/main/java/org/example/commerce_site/common/exception/CustomException.java +++ b/src/main/java/org/example/commerce_site/common/exception/CustomException.java @@ -6,10 +6,10 @@ @Getter @AllArgsConstructor public class CustomException extends RuntimeException { - private final ErrorCode errorCode; + private final ErrorCode errorCode; - public CustomException(String message, ErrorCode errorCode) { - super(message); - this.errorCode = errorCode; - } + public CustomException(String message, ErrorCode errorCode) { + super(message); + this.errorCode = errorCode; + } } diff --git a/src/main/java/org/example/commerce_site/common/exception/ErrorCode.java b/src/main/java/org/example/commerce_site/common/exception/ErrorCode.java index 25b8884..c187d43 100644 --- a/src/main/java/org/example/commerce_site/common/exception/ErrorCode.java +++ b/src/main/java/org/example/commerce_site/common/exception/ErrorCode.java @@ -1,34 +1,35 @@ package org.example.commerce_site.common.exception; +import org.springframework.http.HttpStatus; + import lombok.AllArgsConstructor; import lombok.Getter; -import org.springframework.http.HttpStatus; @Getter @AllArgsConstructor public enum ErrorCode { - //API RuntimeException - NOT_FOUND(HttpStatus.NOT_FOUND, 404, "데이터를 찾을 수 없습니다."), - INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, 500, "내부 서버 오류."), - UNAUTHORIZED(HttpStatus.UNAUTHORIZED, 401, "접근 할수없습니다."), - BAD_REQUEST(HttpStatus.BAD_REQUEST, 400, "잘못된 요청입니다."), - INVALID_PARAM(HttpStatus.BAD_REQUEST, 400, "잘못된 parameter 입니다."), - ACCESS_DENIED(HttpStatus.FORBIDDEN, 403, "권한이 부족합니다."), - METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, 405, "허용되지 않은 메소드 입니다."), - - //user - USER_NOT_FOUND(HttpStatus.NOT_FOUND, 404, "회원 정보를 찾을 수 없습니다."), - - //partner - PARTNER_NOT_FOUND(HttpStatus.NOT_FOUND, 404, "파트너 회원 정보를 찾을 수 없습니다."), - - //product - PRODUCT_NOT_FOUND(HttpStatus.NOT_FOUND, 404, "상품 정보를 찾을 수 없습니다."), - - //category - CATEGORY_NOT_FOUND(HttpStatus.NOT_FOUND, 404, "카테고리 정보를 찾을 수 없습니다."); - - private final HttpStatus httpStatus; - private final int code; - private final String message; + //API RuntimeException + NOT_FOUND(HttpStatus.NOT_FOUND, 404, "데이터를 찾을 수 없습니다."), + INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, 500, "내부 서버 오류."), + UNAUTHORIZED(HttpStatus.UNAUTHORIZED, 401, "접근 할수없습니다."), + BAD_REQUEST(HttpStatus.BAD_REQUEST, 400, "잘못된 요청입니다."), + INVALID_PARAM(HttpStatus.BAD_REQUEST, 400, "잘못된 parameter 입니다."), + ACCESS_DENIED(HttpStatus.FORBIDDEN, 403, "권한이 부족합니다."), + METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, 405, "허용되지 않은 메소드 입니다."), + + //user + USER_NOT_FOUND(HttpStatus.NOT_FOUND, 404, "회원 정보를 찾을 수 없습니다."), + + //partner + PARTNER_NOT_FOUND(HttpStatus.NOT_FOUND, 404, "파트너 회원 정보를 찾을 수 없습니다."), + + //product + PRODUCT_NOT_FOUND(HttpStatus.NOT_FOUND, 404, "상품 정보를 찾을 수 없습니다."), + + //category + CATEGORY_NOT_FOUND(HttpStatus.NOT_FOUND, 404, "카테고리 정보를 찾을 수 없습니다."); + + private final HttpStatus httpStatus; + private final int code; + private final String message; } diff --git a/src/main/java/org/example/commerce_site/common/response/CommonResponse.java b/src/main/java/org/example/commerce_site/common/response/CommonResponse.java index 7a5d33e..ea3e2d7 100644 --- a/src/main/java/org/example/commerce_site/common/response/CommonResponse.java +++ b/src/main/java/org/example/commerce_site/common/response/CommonResponse.java @@ -1,52 +1,53 @@ package org.example.commerce_site.common.response; +import org.example.commerce_site.common.exception.ErrorCode; +import org.springframework.http.ResponseEntity; + import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import org.example.commerce_site.common.exception.ErrorCode; -import org.springframework.http.ResponseEntity; @Getter @Builder @AllArgsConstructor @NoArgsConstructor public class CommonResponse { - private static final String MESSAGE_SUCCESS = "SUCCESS"; - private int code; - private String message; + private static final String MESSAGE_SUCCESS = "SUCCESS"; + private int code; + private String message; - public static CommonResponse.CommonData success(T data) { - return (CommonResponse.CommonData) CommonData.builder() - .message(MESSAGE_SUCCESS) - .data(data) - .build(); - } + public static CommonResponse.CommonData success(T data) { + return (CommonResponse.CommonData)CommonData.builder() + .message(MESSAGE_SUCCESS) + .data(data) + .build(); + } - //for void api - public static CommonResponse success() { - return CommonResponse.builder() - .message(MESSAGE_SUCCESS) - .build(); - } + //for void api + public static CommonResponse success() { + return CommonResponse.builder() + .message(MESSAGE_SUCCESS) + .build(); + } - public static ResponseEntity fail(ErrorCode errorCode) { - return (ResponseEntity) ResponseEntity - .status(errorCode.getHttpStatus()) - .body(CommonResponse.builder() - .code(errorCode.getCode()) - .message(errorCode.getMessage()) - .build() - ); - } + public static ResponseEntity fail(ErrorCode errorCode) { + return (ResponseEntity)ResponseEntity + .status(errorCode.getHttpStatus()) + .body(CommonResponse.builder() + .code(errorCode.getCode()) + .message(errorCode.getMessage()) + .build() + ); + } - @Getter - @Builder - @NoArgsConstructor - @AllArgsConstructor - public static class CommonData { - private int code; - private String message; - private T data; - } + @Getter + @Builder + @NoArgsConstructor + @AllArgsConstructor + public static class CommonData { + private int code; + private String message; + private T data; + } } diff --git a/src/main/java/org/example/commerce_site/domain/Category.java b/src/main/java/org/example/commerce_site/domain/Category.java index de72e95..81afda2 100644 --- a/src/main/java/org/example/commerce_site/domain/Category.java +++ b/src/main/java/org/example/commerce_site/domain/Category.java @@ -1,13 +1,13 @@ package org.example.commerce_site.domain; +import org.example.commerce_site.common.domain.BaseTimeEntity; + import jakarta.persistence.Entity; import jakarta.persistence.Table; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import lombok.experimental.SuperBuilder; -import org.example.commerce_site.common.domain.BaseTimeEntity; @Entity @Getter @@ -16,7 +16,7 @@ @AllArgsConstructor @Table(name = "categories") public class Category extends BaseTimeEntity { - private String name; - private String description; - private Long parentCategoryId; + private String name; + private String description; + private Long parentCategoryId; } diff --git a/src/main/java/org/example/commerce_site/domain/Partner.java b/src/main/java/org/example/commerce_site/domain/Partner.java index 7e50b75..d830a7f 100644 --- a/src/main/java/org/example/commerce_site/domain/Partner.java +++ b/src/main/java/org/example/commerce_site/domain/Partner.java @@ -1,5 +1,8 @@ package org.example.commerce_site.domain; +import org.example.commerce_site.attribute.PartnerStatus; +import org.example.commerce_site.common.domain.BaseTimeEntity; + import jakarta.persistence.Entity; import jakarta.persistence.EnumType; import jakarta.persistence.Enumerated; @@ -8,8 +11,6 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import org.example.commerce_site.attribute.PartnerStatus; -import org.example.commerce_site.common.domain.BaseTimeEntity; @Entity @Getter @@ -18,10 +19,10 @@ @AllArgsConstructor @Table(name = "partners") public class Partner extends BaseTimeEntity { - private String name; - private String businessNumber; - private String password; - private String email; - @Enumerated(EnumType.STRING) - private PartnerStatus status; + private String name; + private String businessNumber; + private String password; + private String email; + @Enumerated(EnumType.STRING) + private PartnerStatus status; } diff --git a/src/main/java/org/example/commerce_site/domain/Product.java b/src/main/java/org/example/commerce_site/domain/Product.java index ce53e9c..ed6b88d 100644 --- a/src/main/java/org/example/commerce_site/domain/Product.java +++ b/src/main/java/org/example/commerce_site/domain/Product.java @@ -1,14 +1,18 @@ package org.example.commerce_site.domain; -import jakarta.persistence.*; +import org.example.commerce_site.common.domain.BaseTimeEntity; +import org.hibernate.annotations.JdbcTypeCode; +import org.hibernate.type.SqlTypes; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import lombok.experimental.SuperBuilder; -import org.example.commerce_site.common.domain.BaseTimeEntity; -import org.hibernate.annotations.JdbcTypeCode; -import org.hibernate.type.SqlTypes; @Entity @Getter @@ -17,21 +21,21 @@ @AllArgsConstructor @Table(name = "products") public class Product extends BaseTimeEntity { - private Long partnerId; + private Long partnerId; - @ManyToOne - @JoinColumn(name = "category_id") - private Category category; + @ManyToOne + @JoinColumn(name = "category_id") + private Category category; - private String name; + private String name; - @JdbcTypeCode(SqlTypes.JSON) - @Column(columnDefinition = "json") - private String description; + @JdbcTypeCode(SqlTypes.JSON) + @Column(columnDefinition = "json") + private String description; - private Long price; + private Long price; - private Long stockQuantity; + private Long stockQuantity; - private Boolean isEnable; + private Boolean isEnable; } diff --git a/src/main/java/org/example/commerce_site/domain/User.java b/src/main/java/org/example/commerce_site/domain/User.java index a46519e..2512f53 100644 --- a/src/main/java/org/example/commerce_site/domain/User.java +++ b/src/main/java/org/example/commerce_site/domain/User.java @@ -1,5 +1,8 @@ package org.example.commerce_site.domain; +import org.example.commerce_site.attribute.UserStatus; +import org.example.commerce_site.common.domain.BaseTimeEntity; + import jakarta.persistence.Entity; import jakarta.persistence.EnumType; import jakarta.persistence.Enumerated; @@ -8,8 +11,6 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import org.example.commerce_site.attribute.UserStatus; -import org.example.commerce_site.common.domain.BaseTimeEntity; @Entity @Getter @@ -18,9 +19,9 @@ @AllArgsConstructor @Table(name = "users") public class User extends BaseTimeEntity { - private String name; - private String email; - private String password; - @Enumerated(EnumType.STRING) - private UserStatus status; + private String name; + private String email; + private String password; + @Enumerated(EnumType.STRING) + private UserStatus status; } diff --git a/src/main/java/org/example/commerce_site/representation/partner/PartnerController.java b/src/main/java/org/example/commerce_site/representation/partner/PartnerController.java index 8b36b15..0a2166c 100644 --- a/src/main/java/org/example/commerce_site/representation/partner/PartnerController.java +++ b/src/main/java/org/example/commerce_site/representation/partner/PartnerController.java @@ -1,8 +1,5 @@ package org.example.commerce_site.representation.partner; -import jakarta.validation.Valid; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import org.example.commerce_site.application.partner.PartnerService; import org.example.commerce_site.common.response.CommonResponse; import org.example.commerce_site.representation.partner.request.PartnerRequest; @@ -12,15 +9,21 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + @Slf4j @RestController @RequiredArgsConstructor @RequestMapping("/partner") public class PartnerController { - private final PartnerService partnerService; + private final PartnerService partnerService; - @PostMapping() - public CommonResponse.CommonData createPartner(@Valid @RequestBody PartnerRequest.Create request) { - return CommonResponse.success(PartnerResponse.Create.of(partnerService.create(PartnerRequest.Create.toDTO(request)))); - } + @PostMapping() + public CommonResponse.CommonData createPartner( + @Valid @RequestBody PartnerRequest.Create request) { + return CommonResponse.success( + PartnerResponse.Create.of(partnerService.create(PartnerRequest.Create.toDTO(request)))); + } } diff --git a/src/main/java/org/example/commerce_site/representation/partner/request/PartnerRequest.java b/src/main/java/org/example/commerce_site/representation/partner/request/PartnerRequest.java index 18bc29b..2f6760a 100644 --- a/src/main/java/org/example/commerce_site/representation/partner/request/PartnerRequest.java +++ b/src/main/java/org/example/commerce_site/representation/partner/request/PartnerRequest.java @@ -1,36 +1,37 @@ package org.example.commerce_site.representation.partner.request; +import org.example.commerce_site.application.partner.dto.PartnerRequestDto; + import jakarta.validation.constraints.NotBlank; import lombok.Getter; import lombok.ToString; -import org.example.commerce_site.application.partner.dto.PartnerRequestDto; public class PartnerRequest { - @Getter - @ToString - public static class Create { - @NotBlank - private String name; + @Getter + @ToString + public static class Create { + @NotBlank + private String name; - @NotBlank - //TODO email 형식 체크 - private String email; + @NotBlank + //TODO email 형식 체크 + private String email; - @NotBlank - //TODO 패스워드 형식 체크 (8자리 이상 20자리 이하 영문 + 숫자) - private String password; + @NotBlank + //TODO 패스워드 형식 체크 (8자리 이상 20자리 이하 영문 + 숫자) + private String password; - @NotBlank - private String businessNumber; + @NotBlank + private String businessNumber; - public static PartnerRequestDto.Create toDTO(PartnerRequest.Create request) { - return PartnerRequestDto.Create.builder() - .name(request.getName()) - .email(request.getEmail()) - //TODO PWD 암호화 - .password(request.getPassword()) - .businessNumber(request.getBusinessNumber()) - .build(); - } - } + public static PartnerRequestDto.Create toDTO(PartnerRequest.Create request) { + return PartnerRequestDto.Create.builder() + .name(request.getName()) + .email(request.getEmail()) + //TODO PWD 암호화 + .password(request.getPassword()) + .businessNumber(request.getBusinessNumber()) + .build(); + } + } } diff --git a/src/main/java/org/example/commerce_site/representation/partner/response/PartnerResponse.java b/src/main/java/org/example/commerce_site/representation/partner/response/PartnerResponse.java index 145cbc5..0c508dc 100644 --- a/src/main/java/org/example/commerce_site/representation/partner/response/PartnerResponse.java +++ b/src/main/java/org/example/commerce_site/representation/partner/response/PartnerResponse.java @@ -1,26 +1,27 @@ package org.example.commerce_site.representation.partner.response; +import org.example.commerce_site.application.partner.dto.PartnerResponseDto; + import lombok.Builder; import lombok.Getter; import lombok.ToString; -import org.example.commerce_site.application.partner.dto.PartnerResponseDto; public class PartnerResponse { - @Getter - @Builder - @ToString - public static class Create { - private String name; - private String email; - private String businessNumber; + @Getter + @Builder + @ToString + public static class Create { + private String name; + private String email; + private String businessNumber; - public static PartnerResponse.Create of(PartnerResponseDto.Create dto) { - return PartnerResponse.Create.builder() - .name(dto.getName()) - .email(dto.getEmail()) - .businessNumber(dto.getBusinessNumber()) - .build(); - } - } + public static PartnerResponse.Create of(PartnerResponseDto.Create dto) { + return PartnerResponse.Create.builder() + .name(dto.getName()) + .email(dto.getEmail()) + .businessNumber(dto.getBusinessNumber()) + .build(); + } + } } diff --git a/src/main/java/org/example/commerce_site/representation/product/ProductController.java b/src/main/java/org/example/commerce_site/representation/product/ProductController.java index 64f7b7c..67ceaf1 100644 --- a/src/main/java/org/example/commerce_site/representation/product/ProductController.java +++ b/src/main/java/org/example/commerce_site/representation/product/ProductController.java @@ -1,7 +1,5 @@ package org.example.commerce_site.representation.product; -import jakarta.validation.Valid; -import lombok.RequiredArgsConstructor; import org.example.commerce_site.application.product.ProductFacade; import org.example.commerce_site.common.response.CommonResponse; import org.example.commerce_site.representation.product.request.ProductRequest; @@ -11,16 +9,20 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; @RestController @RequiredArgsConstructor @RequestMapping("/product") public class ProductController { - private final ProductFacade productFacade; + private final ProductFacade productFacade; - @PostMapping() - public CommonResponse.CommonData createProduct(@Valid @RequestBody ProductRequest.Create request) { - //TODO Partner 회원 외에는 접근할 수 없는 API 임 - return CommonResponse.success(ProductResponse.Create.of(productFacade.createProduct(ProductRequest.Create.toDTO(request)))); - } + @PostMapping() + public CommonResponse.CommonData createProduct( + @Valid @RequestBody ProductRequest.Create request) { + //TODO Partner 회원 외에는 접근할 수 없는 API 임 + return CommonResponse.success( + ProductResponse.Create.of(productFacade.createProduct(ProductRequest.Create.toDTO(request)))); + } } diff --git a/src/main/java/org/example/commerce_site/representation/product/request/ProductRequest.java b/src/main/java/org/example/commerce_site/representation/product/request/ProductRequest.java index f92534c..d848272 100644 --- a/src/main/java/org/example/commerce_site/representation/product/request/ProductRequest.java +++ b/src/main/java/org/example/commerce_site/representation/product/request/ProductRequest.java @@ -1,42 +1,43 @@ package org.example.commerce_site.representation.product.request; +import org.example.commerce_site.application.product.dto.ProductRequestDto; + import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import lombok.Getter; import lombok.ToString; -import org.example.commerce_site.application.product.dto.ProductRequestDto; public class ProductRequest { - @Getter - @ToString - public static class Create { - @NotNull - private Long partnerId; - - @NotNull - private Long categoryId; - - @NotBlank - private String name; - - private String description; - - @NotNull - private Long price; - - @NotNull - private Long stockQuantity; - - public static ProductRequestDto.Create toDTO(ProductRequest.Create request) { - - return ProductRequestDto.Create.builder() - .partnerId(request.getPartnerId()) - .categoryId(request.getCategoryId()) - .name(request.getName()) - .description(request.getDescription()) - .price(request.getPrice()) - .stockQuantity(request.getStockQuantity()) - .build(); - } - } + @Getter + @ToString + public static class Create { + @NotNull + private Long partnerId; + + @NotNull + private Long categoryId; + + @NotBlank + private String name; + + private String description; + + @NotNull + private Long price; + + @NotNull + private Long stockQuantity; + + public static ProductRequestDto.Create toDTO(ProductRequest.Create request) { + + return ProductRequestDto.Create.builder() + .partnerId(request.getPartnerId()) + .categoryId(request.getCategoryId()) + .name(request.getName()) + .description(request.getDescription()) + .price(request.getPrice()) + .stockQuantity(request.getStockQuantity()) + .build(); + } + } } diff --git a/src/main/java/org/example/commerce_site/representation/product/response/ProductResponse.java b/src/main/java/org/example/commerce_site/representation/product/response/ProductResponse.java index e64a35f..1543510 100644 --- a/src/main/java/org/example/commerce_site/representation/product/response/ProductResponse.java +++ b/src/main/java/org/example/commerce_site/representation/product/response/ProductResponse.java @@ -1,37 +1,38 @@ package org.example.commerce_site.representation.product.response; +import java.time.LocalDateTime; + +import org.example.commerce_site.application.product.dto.ProductResponseDto; + import lombok.Builder; import lombok.Getter; import lombok.ToString; -import org.example.commerce_site.application.product.dto.ProductResponseDto; - -import java.time.LocalDateTime; public class ProductResponse { - @Getter - @Builder - @ToString - public static class Create { - private Long id; - private Long partnerId; - private Long categoryId; - private String name; - private String description; - private Long price; - private Long stockQuantity; - private LocalDateTime createdAt; + @Getter + @Builder + @ToString + public static class Create { + private Long id; + private Long partnerId; + private Long categoryId; + private String name; + private String description; + private Long price; + private Long stockQuantity; + private LocalDateTime createdAt; - public static ProductResponse.Create of(ProductResponseDto.Create dto) { - return ProductResponse.Create.builder() - .id(dto.getId()) - .partnerId(dto.getPartnerId()) - .categoryId(dto.getCategoryId()) - .name(dto.getName()) - .description(dto.getDescription()) - .price(dto.getPrice()) - .stockQuantity(dto.getStockQuantity()) - .createdAt(dto.getCreatedAt()) - .build(); - } - } + public static ProductResponse.Create of(ProductResponseDto.Create dto) { + return ProductResponse.Create.builder() + .id(dto.getId()) + .partnerId(dto.getPartnerId()) + .categoryId(dto.getCategoryId()) + .name(dto.getName()) + .description(dto.getDescription()) + .price(dto.getPrice()) + .stockQuantity(dto.getStockQuantity()) + .createdAt(dto.getCreatedAt()) + .build(); + } + } } diff --git a/src/main/java/org/example/commerce_site/representation/user/request/UserRequest.java b/src/main/java/org/example/commerce_site/representation/user/request/UserRequest.java index b19ce7d..4c7f7c0 100644 --- a/src/main/java/org/example/commerce_site/representation/user/request/UserRequest.java +++ b/src/main/java/org/example/commerce_site/representation/user/request/UserRequest.java @@ -1,32 +1,33 @@ package org.example.commerce_site.representation.user.request; +import org.example.commerce_site.application.user.dto.UserRequestDto; + import jakarta.validation.constraints.NotBlank; import lombok.Getter; import lombok.ToString; -import org.example.commerce_site.application.user.dto.UserRequestDto; public class UserRequest { - @Getter - @ToString - public static class Create { - @NotBlank - private String name; + @Getter + @ToString + public static class Create { + @NotBlank + private String name; - @NotBlank - //TODO email 형식 체크 - private String email; + @NotBlank + //TODO email 형식 체크 + private String email; - @NotBlank - //TODO 패스워드 형식 체크 (8자리 이상 20자리 이하 영문 + 숫자) - private String password; + @NotBlank + //TODO 패스워드 형식 체크 (8자리 이상 20자리 이하 영문 + 숫자) + private String password; - public static UserRequestDto.Create toDTO(UserRequest.Create request) { - return UserRequestDto.Create.builder() - .name(request.getName()) - .email(request.getEmail()) - //TODO PWD 암호화 - .password(request.getPassword()) - .build(); - } - } + public static UserRequestDto.Create toDTO(UserRequest.Create request) { + return UserRequestDto.Create.builder() + .name(request.getName()) + .email(request.getEmail()) + //TODO PWD 암호화 + .password(request.getPassword()) + .build(); + } + } } diff --git a/src/main/java/org/example/commerce_site/representation/user/response/UserResponse.java b/src/main/java/org/example/commerce_site/representation/user/response/UserResponse.java index a5821ff..8d28a49 100644 --- a/src/main/java/org/example/commerce_site/representation/user/response/UserResponse.java +++ b/src/main/java/org/example/commerce_site/representation/user/response/UserResponse.java @@ -1,24 +1,25 @@ package org.example.commerce_site.representation.user.response; +import org.example.commerce_site.application.user.dto.UserResponseDto; + import lombok.Builder; import lombok.Getter; import lombok.ToString; -import org.example.commerce_site.application.user.dto.UserResponseDto; public class UserResponse { - @Getter - @Builder - @ToString - public static class Create { - private String name; - private String email; + @Getter + @Builder + @ToString + public static class Create { + private String name; + private String email; - public static UserResponse.Create of(UserResponseDto.Create dto) { - return UserResponse.Create.builder() - .name(dto.getName()) - .email(dto.getEmail()) - .build(); - } - } + public static UserResponse.Create of(UserResponseDto.Create dto) { + return UserResponse.Create.builder() + .name(dto.getName()) + .email(dto.getEmail()) + .build(); + } + } } From 8175f94e6f88ae2ceb791f870131bf93484a2cbe Mon Sep 17 00:00:00 2001 From: suhaoh Date: Sun, 1 Sep 2024 11:14:18 +0900 Subject: [PATCH 9/9] =?UTF-8?q?[10]=20failResponse=20,=20success=20respons?= =?UTF-8?q?e=20=EB=A5=BC=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 성공시 불필요한 code, message 제거 --- .../exception/CommonExceptionHandler.java | 15 +++++----- ...mmonResponse.java => ApiFailResponse.java} | 29 ++----------------- .../common/response/ApiSuccessResponse.java | 29 +++++++++++++++++++ .../partner/PartnerController.java | 6 ++-- .../product/ProductController.java | 6 ++-- .../representation/user/UserController.java | 6 ++-- 6 files changed, 48 insertions(+), 43 deletions(-) rename src/main/java/org/example/commerce_site/common/response/{CommonResponse.java => ApiFailResponse.java} (50%) create mode 100644 src/main/java/org/example/commerce_site/common/response/ApiSuccessResponse.java diff --git a/src/main/java/org/example/commerce_site/common/exception/CommonExceptionHandler.java b/src/main/java/org/example/commerce_site/common/exception/CommonExceptionHandler.java index 22b4b4f..4230139 100644 --- a/src/main/java/org/example/commerce_site/common/exception/CommonExceptionHandler.java +++ b/src/main/java/org/example/commerce_site/common/exception/CommonExceptionHandler.java @@ -1,6 +1,7 @@ package org.example.commerce_site.common.exception; -import org.example.commerce_site.common.response.CommonResponse; +import org.example.commerce_site.common.response.ApiFailResponse; +import org.example.commerce_site.common.response.ApiSuccessResponse; import org.springframework.beans.TypeMismatchException; import org.springframework.core.NestedExceptionUtils; import org.springframework.http.ResponseEntity; @@ -22,26 +23,26 @@ public class CommonExceptionHandler { @ExceptionHandler({MissingServletRequestParameterException.class, ServletRequestBindingException.class, TypeMismatchException.class, HttpMessageNotReadableException.class, MissingServletRequestPartException.class, HttpMessageNotReadableException.class, MethodArgumentNotValidException.class}) - protected ResponseEntity handleBadRequestException(final Exception e) { + protected ResponseEntity handleBadRequestException(final Exception e) { ErrorCode errorCode = ErrorCode.BAD_REQUEST; log.warn("[BadRequestException] error code : {}, error message : {}", errorCode, NestedExceptionUtils.getMostSpecificCause(e).getMessage()); - return CommonResponse.fail(errorCode); + return ApiFailResponse.fail(errorCode); } @ExceptionHandler(HttpRequestMethodNotSupportedException.class) - protected ResponseEntity handleHttpRequestMethodNotSupportedException( + protected ResponseEntity handleHttpRequestMethodNotSupportedException( final HttpRequestMethodNotSupportedException e) { log.error("[HttpRequestMethodNotSupportedException]", e); ErrorCode errorCode = ErrorCode.METHOD_NOT_ALLOWED; - return CommonResponse.fail(errorCode); + return ApiFailResponse.fail(errorCode); } @ExceptionHandler(CustomException.class) - protected ResponseEntity handleCustomException(CustomException e) { + protected ResponseEntity handleCustomException(CustomException e) { ErrorCode errorCode = e.getErrorCode(); log.warn("[CustomException] error code : {}, error message : {}", errorCode, NestedExceptionUtils.getMostSpecificCause(e).getMessage()); - return CommonResponse.fail(errorCode); + return ApiFailResponse.fail(errorCode); } } \ No newline at end of file diff --git a/src/main/java/org/example/commerce_site/common/response/CommonResponse.java b/src/main/java/org/example/commerce_site/common/response/ApiFailResponse.java similarity index 50% rename from src/main/java/org/example/commerce_site/common/response/CommonResponse.java rename to src/main/java/org/example/commerce_site/common/response/ApiFailResponse.java index ea3e2d7..e58b225 100644 --- a/src/main/java/org/example/commerce_site/common/response/CommonResponse.java +++ b/src/main/java/org/example/commerce_site/common/response/ApiFailResponse.java @@ -12,42 +12,17 @@ @Builder @AllArgsConstructor @NoArgsConstructor -public class CommonResponse { - private static final String MESSAGE_SUCCESS = "SUCCESS"; +public class ApiFailResponse { private int code; private String message; - public static CommonResponse.CommonData success(T data) { - return (CommonResponse.CommonData)CommonData.builder() - .message(MESSAGE_SUCCESS) - .data(data) - .build(); - } - - //for void api - public static CommonResponse success() { - return CommonResponse.builder() - .message(MESSAGE_SUCCESS) - .build(); - } - public static ResponseEntity fail(ErrorCode errorCode) { return (ResponseEntity)ResponseEntity .status(errorCode.getHttpStatus()) - .body(CommonResponse.builder() + .body(ApiFailResponse.builder() .code(errorCode.getCode()) .message(errorCode.getMessage()) .build() ); } - - @Getter - @Builder - @NoArgsConstructor - @AllArgsConstructor - public static class CommonData { - private int code; - private String message; - private T data; - } } diff --git a/src/main/java/org/example/commerce_site/common/response/ApiSuccessResponse.java b/src/main/java/org/example/commerce_site/common/response/ApiSuccessResponse.java new file mode 100644 index 0000000..bebffd7 --- /dev/null +++ b/src/main/java/org/example/commerce_site/common/response/ApiSuccessResponse.java @@ -0,0 +1,29 @@ +package org.example.commerce_site.common.response; + +import org.example.commerce_site.common.exception.ErrorCode; +import org.springframework.http.ResponseEntity; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class ApiSuccessResponse { + private T data; + + public static ApiSuccessResponse success(T data) { + return (ApiSuccessResponse)ApiSuccessResponse.builder() + .data(data) + .build(); + } + + //for void api + public static ApiSuccessResponse success() { + return ApiSuccessResponse.builder() + .build(); + } +} diff --git a/src/main/java/org/example/commerce_site/representation/partner/PartnerController.java b/src/main/java/org/example/commerce_site/representation/partner/PartnerController.java index 0a2166c..041f8c9 100644 --- a/src/main/java/org/example/commerce_site/representation/partner/PartnerController.java +++ b/src/main/java/org/example/commerce_site/representation/partner/PartnerController.java @@ -1,7 +1,7 @@ package org.example.commerce_site.representation.partner; import org.example.commerce_site.application.partner.PartnerService; -import org.example.commerce_site.common.response.CommonResponse; +import org.example.commerce_site.common.response.ApiSuccessResponse; import org.example.commerce_site.representation.partner.request.PartnerRequest; import org.example.commerce_site.representation.partner.response.PartnerResponse; import org.springframework.web.bind.annotation.PostMapping; @@ -21,9 +21,9 @@ public class PartnerController { private final PartnerService partnerService; @PostMapping() - public CommonResponse.CommonData createPartner( + public ApiSuccessResponse createPartner( @Valid @RequestBody PartnerRequest.Create request) { - return CommonResponse.success( + return ApiSuccessResponse.success( PartnerResponse.Create.of(partnerService.create(PartnerRequest.Create.toDTO(request)))); } } diff --git a/src/main/java/org/example/commerce_site/representation/product/ProductController.java b/src/main/java/org/example/commerce_site/representation/product/ProductController.java index 67ceaf1..8f01541 100644 --- a/src/main/java/org/example/commerce_site/representation/product/ProductController.java +++ b/src/main/java/org/example/commerce_site/representation/product/ProductController.java @@ -1,7 +1,7 @@ package org.example.commerce_site.representation.product; import org.example.commerce_site.application.product.ProductFacade; -import org.example.commerce_site.common.response.CommonResponse; +import org.example.commerce_site.common.response.ApiSuccessResponse; import org.example.commerce_site.representation.product.request.ProductRequest; import org.example.commerce_site.representation.product.response.ProductResponse; import org.springframework.web.bind.annotation.PostMapping; @@ -19,10 +19,10 @@ public class ProductController { private final ProductFacade productFacade; @PostMapping() - public CommonResponse.CommonData createProduct( + public ApiSuccessResponse createProduct( @Valid @RequestBody ProductRequest.Create request) { //TODO Partner 회원 외에는 접근할 수 없는 API 임 - return CommonResponse.success( + return ApiSuccessResponse.success( ProductResponse.Create.of(productFacade.createProduct(ProductRequest.Create.toDTO(request)))); } } diff --git a/src/main/java/org/example/commerce_site/representation/user/UserController.java b/src/main/java/org/example/commerce_site/representation/user/UserController.java index 037b966..8159af2 100644 --- a/src/main/java/org/example/commerce_site/representation/user/UserController.java +++ b/src/main/java/org/example/commerce_site/representation/user/UserController.java @@ -1,7 +1,7 @@ package org.example.commerce_site.representation.user; import org.example.commerce_site.application.user.UserService; -import org.example.commerce_site.common.response.CommonResponse; +import org.example.commerce_site.common.response.ApiSuccessResponse; import org.example.commerce_site.representation.user.request.UserRequest; import org.example.commerce_site.representation.user.response.UserResponse; import org.springframework.web.bind.annotation.PostMapping; @@ -21,7 +21,7 @@ public class UserController { private final UserService userService; @PostMapping() - public CommonResponse.CommonData createUser(@Valid @RequestBody UserRequest.Create request) { - return CommonResponse.success(UserResponse.Create.of(userService.create(UserRequest.Create.toDTO(request)))); + public ApiSuccessResponse createUser(@Valid @RequestBody UserRequest.Create request) { + return ApiSuccessResponse.success(UserResponse.Create.of(userService.create(UserRequest.Create.toDTO(request)))); } }