From 829b74557cd1ed1559d86fafd43e2b610d3856a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=84=EC=A4=80?= <11dlguswns@naver.com> Date: Thu, 24 Oct 2024 13:57:24 +0900 Subject: [PATCH 1/9] =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EC=9C=84=EC=B9=98=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mango/amango/domain/image/{ => entity}/Image.java | 4 ++-- src/main/java/com/mango/amango/domain/order/Order.java | 2 +- .../com/mango/amango/domain/tag/{ => entity}/Category.java | 2 +- .../java/com/mango/amango/domain/tag/{ => entity}/Tag.java | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) rename src/main/java/com/mango/amango/domain/image/{ => entity}/Image.java (79%) rename src/main/java/com/mango/amango/domain/tag/{ => entity}/Category.java (79%) rename src/main/java/com/mango/amango/domain/tag/{ => entity}/Tag.java (81%) diff --git a/src/main/java/com/mango/amango/domain/image/Image.java b/src/main/java/com/mango/amango/domain/image/entity/Image.java similarity index 79% rename from src/main/java/com/mango/amango/domain/image/Image.java rename to src/main/java/com/mango/amango/domain/image/entity/Image.java index 89c8fac..fc2b61a 100644 --- a/src/main/java/com/mango/amango/domain/image/Image.java +++ b/src/main/java/com/mango/amango/domain/image/entity/Image.java @@ -1,6 +1,6 @@ -package com.mango.amango.domain.image; +package com.mango.amango.domain.image.entity; -import com.mango.amango.domain.product.Product; +import com.mango.amango.domain.product.entity.Product; import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Builder; diff --git a/src/main/java/com/mango/amango/domain/order/Order.java b/src/main/java/com/mango/amango/domain/order/Order.java index a6ca500..cf08378 100644 --- a/src/main/java/com/mango/amango/domain/order/Order.java +++ b/src/main/java/com/mango/amango/domain/order/Order.java @@ -1,6 +1,6 @@ package com.mango.amango.domain.order; -import com.mango.amango.domain.product.Product; +import com.mango.amango.domain.product.entity.Product; import com.mango.amango.domain.user.entity.User; import com.mango.amango.global.entity.BaseEntity; import jakarta.persistence.*; diff --git a/src/main/java/com/mango/amango/domain/tag/Category.java b/src/main/java/com/mango/amango/domain/tag/entity/Category.java similarity index 79% rename from src/main/java/com/mango/amango/domain/tag/Category.java rename to src/main/java/com/mango/amango/domain/tag/entity/Category.java index 8da38bf..4d0bde1 100644 --- a/src/main/java/com/mango/amango/domain/tag/Category.java +++ b/src/main/java/com/mango/amango/domain/tag/entity/Category.java @@ -1,4 +1,4 @@ -package com.mango.amango.domain.tag; +package com.mango.amango.domain.tag.entity; public enum Category { DISPOSABLES, diff --git a/src/main/java/com/mango/amango/domain/tag/Tag.java b/src/main/java/com/mango/amango/domain/tag/entity/Tag.java similarity index 81% rename from src/main/java/com/mango/amango/domain/tag/Tag.java rename to src/main/java/com/mango/amango/domain/tag/entity/Tag.java index 753c9a4..380488e 100644 --- a/src/main/java/com/mango/amango/domain/tag/Tag.java +++ b/src/main/java/com/mango/amango/domain/tag/entity/Tag.java @@ -1,6 +1,6 @@ -package com.mango.amango.domain.tag; +package com.mango.amango.domain.tag.entity; -import com.mango.amango.domain.product.Product; +import com.mango.amango.domain.product.entity.Product; import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Builder; From 1e45d6ebde1f9d197b4c5e97a35663a97c5a64a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=84=EC=A4=80?= <11dlguswns@naver.com> Date: Thu, 24 Oct 2024 13:58:44 +0900 Subject: [PATCH 2/9] =?UTF-8?q?refactor=20::=20Product=20Entity=20?= =?UTF-8?q?=EA=B8=B0=EB=B3=B8=20=EA=B0=92=20=EC=B6=94=EA=B0=80,=20?= =?UTF-8?q?=EC=9E=90=EB=A3=8C=ED=98=95=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../amango/domain/product/{ => entity}/Product.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) rename src/main/java/com/mango/amango/domain/product/{ => entity}/Product.java (79%) diff --git a/src/main/java/com/mango/amango/domain/product/Product.java b/src/main/java/com/mango/amango/domain/product/entity/Product.java similarity index 79% rename from src/main/java/com/mango/amango/domain/product/Product.java rename to src/main/java/com/mango/amango/domain/product/entity/Product.java index a857916..e90e09d 100644 --- a/src/main/java/com/mango/amango/domain/product/Product.java +++ b/src/main/java/com/mango/amango/domain/product/entity/Product.java @@ -1,4 +1,4 @@ -package com.mango.amango.domain.product; +package com.mango.amango.domain.product.entity; import com.mango.amango.domain.user.entity.User; import com.mango.amango.global.entity.BaseEntity; @@ -25,14 +25,16 @@ public class Product extends BaseEntity { private String description; - private String price; + private Long price; @ManyToOne private User user; - private Integer view; + @Builder.Default + private Integer view = 0; - private Integer likes; + @Builder.Default + private Integer likes = 0; private LocalDateTime expirTime; From e4f03f6260d2312055b35adc21bb415466cc7816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=84=EC=A4=80?= <11dlguswns@naver.com> Date: Thu, 24 Oct 2024 14:14:33 +0900 Subject: [PATCH 3/9] =?UTF-8?q?feat=20::=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=20=EA=B8=B0=EB=8A=A5=20=EA=B0=9C=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mango/amango/domain/image/ImageUtil.java | 65 +++++++++++++++++++ .../image/repository/ImageRepository.java | 7 ++ .../domain/image/service/ImageService.java | 31 +++++++++ 3 files changed, 103 insertions(+) create mode 100644 src/main/java/com/mango/amango/domain/image/ImageUtil.java create mode 100644 src/main/java/com/mango/amango/domain/image/repository/ImageRepository.java create mode 100644 src/main/java/com/mango/amango/domain/image/service/ImageService.java diff --git a/src/main/java/com/mango/amango/domain/image/ImageUtil.java b/src/main/java/com/mango/amango/domain/image/ImageUtil.java new file mode 100644 index 0000000..c18ed43 --- /dev/null +++ b/src/main/java/com/mango/amango/domain/image/ImageUtil.java @@ -0,0 +1,65 @@ +package com.mango.amango.domain.image; + +import com.mango.amango.global.exception.CustomErrorCode; +import com.mango.amango.global.exception.CustomException; +import org.springframework.stereotype.Component; +import org.springframework.web.multipart.MultipartFile; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.UUID; + +@Component +public class ImageUtil { + + public static String storeImage(MultipartFile file) { + if (file.isEmpty()) { + return null; + } + + makeDir(); + + String originalFilename = file.getOriginalFilename(); + String storeFilename = createStoreFilename(originalFilename); + try { + file.transferTo(new File(getFullPath(storeFilename))); + } catch (IOException e) { + throw new CustomException(CustomErrorCode.FILE_PROCESSING_ERROR); + } + + return storeFilename; + } + + public static String getFullPath(String fileName) { + if (fileName.isEmpty()) { + return null; + } + + return Paths.get(getFilePath(), fileName).toString(); + } + + private static String getFilePath() { + return Paths.get(System.getProperty("user.dir"), "src", "main", "resources", "static", "images").toString(); + } + + private static void makeDir() { + File file = new File(getFilePath()); + if(!file.exists() && !file.mkdirs()) { + throw new CustomException(CustomErrorCode.FILE_CREATE_ERROR); + } + } + + private static String createStoreFilename(String filename) { + String ext = extractExt(filename); + String uuid = UUID.randomUUID().toString(); + return uuid + "." + ext; + } + + private static String extractExt(String filename) { + int pos = filename.lastIndexOf("."); + return filename.substring(pos + 1); + } + + +} diff --git a/src/main/java/com/mango/amango/domain/image/repository/ImageRepository.java b/src/main/java/com/mango/amango/domain/image/repository/ImageRepository.java new file mode 100644 index 0000000..696379b --- /dev/null +++ b/src/main/java/com/mango/amango/domain/image/repository/ImageRepository.java @@ -0,0 +1,7 @@ +package com.mango.amango.domain.image.repository; + +import com.mango.amango.domain.image.entity.Image; +import org.springframework.data.repository.CrudRepository; + +public interface ImageRepository extends CrudRepository { +} diff --git a/src/main/java/com/mango/amango/domain/image/service/ImageService.java b/src/main/java/com/mango/amango/domain/image/service/ImageService.java new file mode 100644 index 0000000..e400d73 --- /dev/null +++ b/src/main/java/com/mango/amango/domain/image/service/ImageService.java @@ -0,0 +1,31 @@ +package com.mango.amango.domain.image.service; + +import com.mango.amango.domain.image.entity.Image; +import com.mango.amango.domain.image.repository.ImageRepository; +import com.mango.amango.domain.product.entity.Product; +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; + +import java.util.List; + +import static com.mango.amango.domain.image.ImageUtil.*; + +@Service +@Transactional +@AllArgsConstructor +public class ImageService { + + private final ImageRepository imageRepository; + + public void saveImage(Product product, List files) { + for (MultipartFile file : files) { + imageRepository.save(Image.builder() + .image(storeImage(file)) + .product(product) + .build() + ); + } + } +} From 2d3dfa67e0037dfc9f3dc38c4530789865f855e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=84=EC=A4=80?= <11dlguswns@naver.com> Date: Thu, 24 Oct 2024 14:15:13 +0900 Subject: [PATCH 4/9] =?UTF-8?q?feat=20::=20=ED=83=9C=EA=B7=B8=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5=20=EA=B8=B0=EB=8A=A5=20=EA=B0=9C=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/tag/repository/TagRepository.java | 7 +++++ .../amango/domain/tag/service/TagService.java | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/main/java/com/mango/amango/domain/tag/repository/TagRepository.java create mode 100644 src/main/java/com/mango/amango/domain/tag/service/TagService.java diff --git a/src/main/java/com/mango/amango/domain/tag/repository/TagRepository.java b/src/main/java/com/mango/amango/domain/tag/repository/TagRepository.java new file mode 100644 index 0000000..be715ac --- /dev/null +++ b/src/main/java/com/mango/amango/domain/tag/repository/TagRepository.java @@ -0,0 +1,7 @@ +package com.mango.amango.domain.tag.repository; + +import com.mango.amango.domain.tag.entity.Tag; +import org.springframework.data.repository.CrudRepository; + +public interface TagRepository extends CrudRepository { +} diff --git a/src/main/java/com/mango/amango/domain/tag/service/TagService.java b/src/main/java/com/mango/amango/domain/tag/service/TagService.java new file mode 100644 index 0000000..244cbe9 --- /dev/null +++ b/src/main/java/com/mango/amango/domain/tag/service/TagService.java @@ -0,0 +1,28 @@ +package com.mango.amango.domain.tag.service; + +import com.mango.amango.domain.product.entity.Product; +import com.mango.amango.domain.tag.entity.Category; +import com.mango.amango.domain.tag.entity.Tag; +import com.mango.amango.domain.tag.repository.TagRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Service +@Transactional +@RequiredArgsConstructor +public class TagService { + + private final TagRepository tagRepository; + + public void saveTag(Product product, List categories) { + categories.forEach(category -> { + tagRepository.save(Tag.builder() + .category(category) + .product(product) + .build()); + }); + } +} From a43d392b074607cc1cc8c0dfee5fdcd371f1c1f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=84=EC=A4=80?= <11dlguswns@naver.com> Date: Thu, 24 Oct 2024 14:17:46 +0900 Subject: [PATCH 5/9] =?UTF-8?q?feat=20::=20=EC=83=81=ED=92=88=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/controller/ProductController.java | 30 +++++++++++++ .../entity/dto/request/CreateProductReq.java | 31 ++++++++++++++ .../product/repository/ProductRepository.java | 7 ++++ .../product/service/ProductService.java | 42 +++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 src/main/java/com/mango/amango/domain/product/controller/ProductController.java create mode 100644 src/main/java/com/mango/amango/domain/product/entity/dto/request/CreateProductReq.java create mode 100644 src/main/java/com/mango/amango/domain/product/repository/ProductRepository.java create mode 100644 src/main/java/com/mango/amango/domain/product/service/ProductService.java diff --git a/src/main/java/com/mango/amango/domain/product/controller/ProductController.java b/src/main/java/com/mango/amango/domain/product/controller/ProductController.java new file mode 100644 index 0000000..c0488fd --- /dev/null +++ b/src/main/java/com/mango/amango/domain/product/controller/ProductController.java @@ -0,0 +1,30 @@ +package com.mango.amango.domain.product.controller; + +import com.mango.amango.domain.product.entity.dto.request.CreateProductReq; +import com.mango.amango.domain.product.service.ProductService; +import jakarta.validation.Valid; +import jakarta.validation.constraints.Size; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import java.util.List; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/product") +public class ProductController { + + public final ProductService productService; + + @PostMapping + public ResponseEntity createProduct( + @Valid @RequestPart("request") CreateProductReq request, + @Size(max = 3, message = "이미지는 최대 3장까지 입니다.") @RequestPart("images") List images + ) { + productService.createProduct(request, images); + return ResponseEntity.ok().build(); + } + +} diff --git a/src/main/java/com/mango/amango/domain/product/entity/dto/request/CreateProductReq.java b/src/main/java/com/mango/amango/domain/product/entity/dto/request/CreateProductReq.java new file mode 100644 index 0000000..1d4fdf2 --- /dev/null +++ b/src/main/java/com/mango/amango/domain/product/entity/dto/request/CreateProductReq.java @@ -0,0 +1,31 @@ +package com.mango.amango.domain.product.entity.dto.request; + +import com.mango.amango.domain.tag.entity.Category; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.time.LocalDateTime; +import java.util.List; + +@Getter +@AllArgsConstructor +public class CreateProductReq { + + @NotBlank(message = "제목은 비어 있을 수 없습니다.") + private final String title; + + private final String description; + + private final Long price; + + @Size(min = 1, message = "태그는 최소 1개 이상 포함되어야 합니다.") + private final List tags; + + private final Long auctionPrice; + + @NotNull(message = "시간은 필수 입력 값 입니다.") + private final LocalDateTime expirTime; +} diff --git a/src/main/java/com/mango/amango/domain/product/repository/ProductRepository.java b/src/main/java/com/mango/amango/domain/product/repository/ProductRepository.java new file mode 100644 index 0000000..92aaaee --- /dev/null +++ b/src/main/java/com/mango/amango/domain/product/repository/ProductRepository.java @@ -0,0 +1,7 @@ +package com.mango.amango.domain.product.repository; + +import com.mango.amango.domain.product.entity.Product; +import org.springframework.data.repository.CrudRepository; + +public interface ProductRepository extends CrudRepository { +} diff --git a/src/main/java/com/mango/amango/domain/product/service/ProductService.java b/src/main/java/com/mango/amango/domain/product/service/ProductService.java new file mode 100644 index 0000000..8c24079 --- /dev/null +++ b/src/main/java/com/mango/amango/domain/product/service/ProductService.java @@ -0,0 +1,42 @@ +package com.mango.amango.domain.product.service; + +import com.mango.amango.domain.image.service.ImageService; +import com.mango.amango.domain.product.entity.Product; +import com.mango.amango.domain.product.entity.dto.request.CreateProductReq; +import com.mango.amango.domain.product.repository.ProductRepository; +import com.mango.amango.domain.tag.service.TagService; +import com.mango.amango.domain.user.service.UserService; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; + +import java.util.List; + +@Service +@Transactional +@RequiredArgsConstructor +public class ProductService { + + private final ProductRepository productRepository; + private final UserService userService; + private final TagService tagService; + private final ImageService imageService; + + + public void createProduct(CreateProductReq request, List images) { + + Product product = Product.builder() + .title(request.getTitle()) + .description(request.getDescription()) + .price(request.getPrice()) + .user(userService.getCurrentUser()) + .expirTime(request.getExpirTime()) + .auctionPrice(request.getAuctionPrice()) + .build(); + + productRepository.save(product); + tagService.saveTag(product, request.getTags()); + imageService.saveImage(product, images); + } +} From 56b04f9b7065f456d9cbfcbb9f4b944f3bf441b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=84=EC=A4=80?= <11dlguswns@naver.com> Date: Thu, 24 Oct 2024 14:24:40 +0900 Subject: [PATCH 6/9] =?UTF-8?q?feat=20::=20Validation=20=EC=98=88=EC=99=B8?= =?UTF-8?q?=20=EC=B2=98=EB=A6=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/exception/CustomErrorCode.java | 5 +-- .../handler/CustomExceptionHandler.java | 36 ++++++++++++++----- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/mango/amango/global/exception/CustomErrorCode.java b/src/main/java/com/mango/amango/global/exception/CustomErrorCode.java index 5fa6f87..fd42491 100644 --- a/src/main/java/com/mango/amango/global/exception/CustomErrorCode.java +++ b/src/main/java/com/mango/amango/global/exception/CustomErrorCode.java @@ -11,8 +11,9 @@ public enum CustomErrorCode { MALFORMED_TOKEN(HttpStatus.BAD_REQUEST, "잘못된 토큰 형식 입니다."), USER_NOT_FOUND(HttpStatus.BAD_REQUEST, "존재하지 않는 유저 입니다."), REFRESH_TOKEN_NOT_FOUND(HttpStatus.BAD_REQUEST, "Refresh 토큰을 찾을 수 없습니다."), - VALIDATION_FAILED(HttpStatus.BAD_REQUEST, "유효하지 않는 요청 형식입니다.") - ; + VALIDATION_FAILED(HttpStatus.BAD_REQUEST, "유효하지 않는 요청 형식입니다."), + FILE_CREATE_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "파일 경로를 찾을 수 없습니다."), + FILE_PROCESSING_ERROR(HttpStatus.BAD_REQUEST, "처리 할 수 없는 파일입니다."); private final HttpStatus statusCode; private final String statusMessage; diff --git a/src/main/java/com/mango/amango/global/exception/handler/CustomExceptionHandler.java b/src/main/java/com/mango/amango/global/exception/handler/CustomExceptionHandler.java index 76d7938..0d24810 100644 --- a/src/main/java/com/mango/amango/global/exception/handler/CustomExceptionHandler.java +++ b/src/main/java/com/mango/amango/global/exception/handler/CustomExceptionHandler.java @@ -5,12 +5,16 @@ import com.mango.amango.global.exception.CustomErrorRes; import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; +import org.springframework.context.MessageSourceResolvable; import org.springframework.context.support.DefaultMessageSourceResolvable; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.method.annotation.HandlerMethodValidationException; + +import java.util.Objects; import static org.springframework.http.HttpStatus.*; @@ -37,18 +41,34 @@ public ResponseEntity handlerException( @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity methodArgumentNotValidException(MethodArgumentNotValidException e) { - CustomErrorRes response = CustomErrorRes.builder() - .status(CustomErrorCode.VALIDATION_FAILED) - .statusMessage(getErrorMessage(e)) - .build(); + String errorMessage = e.getBindingResult().getFieldErrors().stream() + .findFirst() + .map(DefaultMessageSourceResolvable::getDefaultMessage) + .orElse("유효성 검사에 실패했습니다."); - return ResponseEntity.status(BAD_REQUEST).body(response); + return getErrorResponse(errorMessage); } - private String getErrorMessage(MethodArgumentNotValidException e) { - return e.getBindingResult().getFieldErrors().stream() + @ExceptionHandler(HandlerMethodValidationException.class) + public ResponseEntity handlerMethodValidationException(HandlerMethodValidationException e) { + String errorMessage = e.getAllValidationResults() .stream() .findFirst() - .map(DefaultMessageSourceResolvable::getDefaultMessage) + .map(valueResult -> valueResult.getResolvableErrors().stream() + .map(MessageSourceResolvable::getDefaultMessage) + .filter(Objects::nonNull) + .findFirst() + .orElse("유효성 검사에 실패했습니다.")) .orElse("유효성 검사에 실패했습니다."); + + return getErrorResponse(errorMessage); + } + + private ResponseEntity getErrorResponse(String errorMessage) { + CustomErrorRes response = CustomErrorRes.builder() + .status(CustomErrorCode.VALIDATION_FAILED) + .statusMessage(errorMessage) + .build(); + + return ResponseEntity.status(BAD_REQUEST).body(response); } } From 4a6021fd449caec625a6c1a24480a7053742beed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=84=EC=A4=80?= <11dlguswns@naver.com> Date: Thu, 24 Oct 2024 15:34:22 +0900 Subject: [PATCH 7/9] =?UTF-8?q?refactor=20::=20ProductRepository=20?= =?UTF-8?q?=EC=83=81=EC=86=8D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../amango/domain/product/repository/ProductRepository.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/mango/amango/domain/product/repository/ProductRepository.java b/src/main/java/com/mango/amango/domain/product/repository/ProductRepository.java index 92aaaee..f6a1b33 100644 --- a/src/main/java/com/mango/amango/domain/product/repository/ProductRepository.java +++ b/src/main/java/com/mango/amango/domain/product/repository/ProductRepository.java @@ -1,7 +1,8 @@ package com.mango.amango.domain.product.repository; import com.mango.amango.domain.product.entity.Product; +import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.repository.CrudRepository; -public interface ProductRepository extends CrudRepository { +public interface ProductRepository extends JpaRepository { } From ea7fc42db5ffc2c2ff1685914d17867968e674a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=84=EC=A4=80?= <11dlguswns@naver.com> Date: Thu, 24 Oct 2024 15:45:54 +0900 Subject: [PATCH 8/9] =?UTF-8?q?refactor=20::=20createProduct=20=EC=83=81?= =?UTF-8?q?=ED=83=9C=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../amango/domain/product/controller/ProductController.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/mango/amango/domain/product/controller/ProductController.java b/src/main/java/com/mango/amango/domain/product/controller/ProductController.java index c0488fd..3b3357c 100644 --- a/src/main/java/com/mango/amango/domain/product/controller/ProductController.java +++ b/src/main/java/com/mango/amango/domain/product/controller/ProductController.java @@ -11,6 +11,8 @@ import java.util.List; +import static org.springframework.http.HttpStatus.*; + @RestController @RequiredArgsConstructor @RequestMapping("/product") @@ -24,7 +26,7 @@ public ResponseEntity createProduct( @Size(max = 3, message = "이미지는 최대 3장까지 입니다.") @RequestPart("images") List images ) { productService.createProduct(request, images); - return ResponseEntity.ok().build(); + return ResponseEntity.status(CREATED).build(); } } From 60f49b0a0e6007d96840b076800fad4645a2abf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=84=EC=A4=80?= <11dlguswns@naver.com> Date: Thu, 24 Oct 2024 18:23:12 +0900 Subject: [PATCH 9/9] =?UTF-8?q?refactor=20::=20product=20Entity=EB=A5=BC?= =?UTF-8?q?=20record=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entity/dto/request/CreateProductReq.java | 26 ++++++++----------- .../product/service/ProductService.java | 12 ++++----- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/mango/amango/domain/product/entity/dto/request/CreateProductReq.java b/src/main/java/com/mango/amango/domain/product/entity/dto/request/CreateProductReq.java index 1d4fdf2..bc35376 100644 --- a/src/main/java/com/mango/amango/domain/product/entity/dto/request/CreateProductReq.java +++ b/src/main/java/com/mango/amango/domain/product/entity/dto/request/CreateProductReq.java @@ -4,28 +4,24 @@ import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; -import lombok.AllArgsConstructor; -import lombok.Getter; - import java.time.LocalDateTime; import java.util.List; -@Getter -@AllArgsConstructor -public class CreateProductReq { +public record CreateProductReq( + @NotBlank(message = "제목은 비어 있을 수 없습니다.") + String title, - @NotBlank(message = "제목은 비어 있을 수 없습니다.") - private final String title; + String description, - private final String description; + Long price, - private final Long price; + @Size(min = 1, message = "태그는 최소 1개 이상 포함되어야 합니다.") + List tags, - @Size(min = 1, message = "태그는 최소 1개 이상 포함되어야 합니다.") - private final List tags; + Long auctionPrice, - private final Long auctionPrice; + @NotNull(message = "시간은 필수 입력 값 입니다.") + LocalDateTime expirTime +) { - @NotNull(message = "시간은 필수 입력 값 입니다.") - private final LocalDateTime expirTime; } diff --git a/src/main/java/com/mango/amango/domain/product/service/ProductService.java b/src/main/java/com/mango/amango/domain/product/service/ProductService.java index 8c24079..4369a76 100644 --- a/src/main/java/com/mango/amango/domain/product/service/ProductService.java +++ b/src/main/java/com/mango/amango/domain/product/service/ProductService.java @@ -27,16 +27,16 @@ public class ProductService { public void createProduct(CreateProductReq request, List images) { Product product = Product.builder() - .title(request.getTitle()) - .description(request.getDescription()) - .price(request.getPrice()) + .title(request.title()) + .description(request.description()) + .price(request.price()) .user(userService.getCurrentUser()) - .expirTime(request.getExpirTime()) - .auctionPrice(request.getAuctionPrice()) + .expirTime(request.expirTime()) + .auctionPrice(request.auctionPrice()) .build(); productRepository.save(product); - tagService.saveTag(product, request.getTags()); + tagService.saveTag(product, request.tags()); imageService.saveImage(product, images); } }