From 614c68ba6343b65dfcc33aed61c3dca1daf3bc56 Mon Sep 17 00:00:00 2001 From: oohyj Date: Tue, 26 Nov 2024 18:09:07 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat=20:=20=EC=B9=B4=ED=85=8C=EA=B3=A0?= =?UTF-8?q?=EB=A6=AC=EB=B3=84=20=EC=A1=B0=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/controller/ProductController.java | 6 +++++ .../karma/domain/product/entity/Product.java | 3 ++- .../product/repository/ProductRepository.java | 18 +++++++++++++ .../product/service/ProductService.java | 26 ++++++++++++++++++- .../karma/global/exception/ErrorCode.java | 1 + 5 files changed, 52 insertions(+), 2 deletions(-) diff --git a/karma/src/main/java/com/ecyce/karma/domain/product/controller/ProductController.java b/karma/src/main/java/com/ecyce/karma/domain/product/controller/ProductController.java index 10fb54a..daa05f4 100644 --- a/karma/src/main/java/com/ecyce/karma/domain/product/controller/ProductController.java +++ b/karma/src/main/java/com/ecyce/karma/domain/product/controller/ProductController.java @@ -109,4 +109,10 @@ public ResponseEntity SearchAllByWord(@AuthUser User user ,@RequestParam(name return productService.Search(user , searchWord); } + /* 카테고리별 조회 */ + @GetMapping("/category") + public ResponseEntity categorySort(@AuthUser User user , @RequestParam(name ="code") Long category){ + return productService.categorySort(user , category); + } + } diff --git a/karma/src/main/java/com/ecyce/karma/domain/product/entity/Product.java b/karma/src/main/java/com/ecyce/karma/domain/product/entity/Product.java index 8b9a453..d0240b8 100644 --- a/karma/src/main/java/com/ecyce/karma/domain/product/entity/Product.java +++ b/karma/src/main/java/com/ecyce/karma/domain/product/entity/Product.java @@ -4,6 +4,7 @@ import com.ecyce.karma.domain.order.entity.Orders; import com.ecyce.karma.domain.product.dto.request.ModifyProductRequest; import com.ecyce.karma.domain.user.entity.User; +import com.ecyce.karma.global.entity.BaseTimeEntity; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Builder; @@ -16,7 +17,7 @@ @Entity @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class Product { +public class Product extends BaseTimeEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/karma/src/main/java/com/ecyce/karma/domain/product/repository/ProductRepository.java b/karma/src/main/java/com/ecyce/karma/domain/product/repository/ProductRepository.java index 372c95b..f17c5f7 100644 --- a/karma/src/main/java/com/ecyce/karma/domain/product/repository/ProductRepository.java +++ b/karma/src/main/java/com/ecyce/karma/domain/product/repository/ProductRepository.java @@ -19,5 +19,23 @@ public interface ProductRepository extends JpaRepository { @Query("select p from Product p where p.productName LIKE %:searchWord%") List findByProductName(@Param("searchWord") String word); + /* 모든 제품을 rating 기준으로 내림차순 정렬 (NULLS LAST 포함)*/ + @Query("SELECT p FROM Product p ORDER BY p.rating DESC NULLS LAST") + List findAllOrderByRatingDesc(); + + + + /* 모든 제품을 북마크 순으로 정렬 */ + @Query("SELECT p " + + "FROM Product p " + + "LEFT JOIN p.bookmarkList b " + + "GROUP BY p " + + "ORDER BY COUNT(b) DESC") + List findAllOrderByBookmarkCountDesc(); + + + /* 최신순으로 정렬 */ + @Query("SELECT p FROM Product p ORDER BY p.createdAt DESC") + List findAllOrderByCreatedAtDesc(); } diff --git a/karma/src/main/java/com/ecyce/karma/domain/product/service/ProductService.java b/karma/src/main/java/com/ecyce/karma/domain/product/service/ProductService.java index 5af4f42..c6d3483 100644 --- a/karma/src/main/java/com/ecyce/karma/domain/product/service/ProductService.java +++ b/karma/src/main/java/com/ecyce/karma/domain/product/service/ProductService.java @@ -19,7 +19,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.security.access.method.P; +import org.springframework.security.core.parameters.P; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -168,4 +168,28 @@ public ResponseEntity Search(User user, String searchWord) { return ResponseEntity.status(HttpStatus.OK).body(productSimpleResponseList); } + + /* 카테고리별 조회*/ + public ResponseEntity categorySort(User user, Long category) { + /* 1이면 후기순 */ + if(category.equals(1L)){ + List sortedProducts = productRepository.findAllOrderByRatingDesc(); // 정렬된 결과 가져오기 + // 로그인 여부에 따른 북마크 상태 포함 + List productSimpleResponses = getProductSimpleResponses(user, sortedProducts); + return ResponseEntity.ok(productSimpleResponses); + } + else if(category.equals(2L)){ + List sortedProducts = productRepository.findAllOrderByBookmarkCountDesc(); + // 로그인 여부에 따른 북마크 상태 포함 + List productSimpleResponses = getProductSimpleResponses(user, sortedProducts); + return ResponseEntity.ok(productSimpleResponses); + } + else if(category.equals(3L)){ + List sortedProducts = productRepository.findAllOrderByCreatedAtDesc(); + List productSimpleResponse = getProductSimpleResponses(user , sortedProducts); + return ResponseEntity.ok(productSimpleResponse); + } + else throw new CustomException(ErrorCode.INVALID_REQUEST); + + } } diff --git a/karma/src/main/java/com/ecyce/karma/global/exception/ErrorCode.java b/karma/src/main/java/com/ecyce/karma/global/exception/ErrorCode.java index 2f949a9..15f225d 100644 --- a/karma/src/main/java/com/ecyce/karma/global/exception/ErrorCode.java +++ b/karma/src/main/java/com/ecyce/karma/global/exception/ErrorCode.java @@ -22,6 +22,7 @@ public enum ErrorCode { PRODUCT_NOT_FOUND(HttpStatus.NOT_FOUND , "해당하는 제품을 찾을 수 없습니다."), PRODUCT_OPTION_NOT_FOUND(HttpStatus.NOT_FOUND, "해당하는 옵션을 찾을 수 없습니다."), INVALID_ACCESS(HttpStatus.BAD_REQUEST , "해당 사용자는 제품 정보를 수정할 수 없습니다."), + INVALID_REQUEST(HttpStatus.BAD_REQUEST , "잘못된 요청입니다."), // bookmark BOOKMARK_NOT_FOUND(HttpStatus.NOT_FOUND, "북마크가 존재하지 않습니다. 북마크를 등록한 후 취소할 수 있습니다."), From 89980f7be339d3223ab19f48e1605342292edcf4 Mon Sep 17 00:00:00 2001 From: oohyj Date: Tue, 26 Nov 2024 18:40:26 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat.=20:=20=EC=83=81=ED=92=88=20=EC=98=B5?= =?UTF-8?q?=EC=85=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ecyce/karma/domain/product/controller/ProductController.java | 1 + 1 file changed, 1 insertion(+) diff --git a/karma/src/main/java/com/ecyce/karma/domain/product/controller/ProductController.java b/karma/src/main/java/com/ecyce/karma/domain/product/controller/ProductController.java index daa05f4..c90ad3e 100644 --- a/karma/src/main/java/com/ecyce/karma/domain/product/controller/ProductController.java +++ b/karma/src/main/java/com/ecyce/karma/domain/product/controller/ProductController.java @@ -115,4 +115,5 @@ public ResponseEntity categorySort(@AuthUser User user , @RequestParam(name = return productService.categorySort(user , category); } + } From 4540d18c8dedd3509a09d7470da1e67f1a5132bb Mon Sep 17 00:00:00 2001 From: oohyj Date: Wed, 27 Nov 2024 02:13:03 +0900 Subject: [PATCH 3/4] =?UTF-8?q?feat=20:=20=EC=98=B5=EC=85=98=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=EB=B0=8F=20=EC=98=B5=EC=85=98=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/controller/ProductController.java | 14 +++++++++ .../product/dto/request/ProductRequest.java | 4 +-- .../product/service/ProductService.java | 30 +++++++++++++++---- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/karma/src/main/java/com/ecyce/karma/domain/product/controller/ProductController.java b/karma/src/main/java/com/ecyce/karma/domain/product/controller/ProductController.java index c90ad3e..ef73d60 100644 --- a/karma/src/main/java/com/ecyce/karma/domain/product/controller/ProductController.java +++ b/karma/src/main/java/com/ecyce/karma/domain/product/controller/ProductController.java @@ -4,6 +4,7 @@ import com.ecyce.karma.domain.bookmark.service.BookmarkService; import com.ecyce.karma.domain.product.dto.request.ModifyOptionRequest; import com.ecyce.karma.domain.product.dto.request.ModifyProductRequest; +import com.ecyce.karma.domain.product.dto.request.OptionRequest; import com.ecyce.karma.domain.product.dto.response.OptionResponse; import com.ecyce.karma.domain.product.dto.response.ProductDetailResponse; import com.ecyce.karma.domain.product.dto.request.ProductRequest; @@ -115,5 +116,18 @@ public ResponseEntity categorySort(@AuthUser User user , @RequestParam(name = return productService.categorySort(user , category); } + /* 옵션 삭제 */ + @DeleteMapping("/{productId}/option/{optionId}") + public String deleteOption(@AuthUser User user ,@PathVariable("productId") Long productId , @PathVariable("optionId") Long optionId){ + productService.deleteOption(user , productId , optionId); + return "삭제되었습니다."; + } + + /* 옵션 추가 */ + @PostMapping("/{productId}/option") + public ResponseEntity addOption(@AuthUser User user , @PathVariable("productId") Long productId , @RequestBody OptionRequest request){ + return productService.addOption(user , productId , request); + } + } diff --git a/karma/src/main/java/com/ecyce/karma/domain/product/dto/request/ProductRequest.java b/karma/src/main/java/com/ecyce/karma/domain/product/dto/request/ProductRequest.java index eaa5bbc..f518f3d 100644 --- a/karma/src/main/java/com/ecyce/karma/domain/product/dto/request/ProductRequest.java +++ b/karma/src/main/java/com/ecyce/karma/domain/product/dto/request/ProductRequest.java @@ -21,7 +21,7 @@ public class ProductRequest { private Integer deliveryFee; // 배송비 private String materialInfo; // 소재의 정보 private String buyerNotice; // 구매자 안내사항 - private List option; + private List options; public ProductRequest(String productName , int price , String content , int duration , ProductState productState , Integer deliveryFee , String materialInfo , String buyerNotice, @@ -34,7 +34,7 @@ public ProductRequest(String productName , int price , String content , int dura this.deliveryFee = deliveryFee; this.materialInfo = materialInfo; this.buyerNotice = buyerNotice; - this.option = optionRequest; + this.options = optionRequest; } public static Product toEntity(User user , ProductRequest requestDto){ diff --git a/karma/src/main/java/com/ecyce/karma/domain/product/service/ProductService.java b/karma/src/main/java/com/ecyce/karma/domain/product/service/ProductService.java index c6d3483..4e7d4e6 100644 --- a/karma/src/main/java/com/ecyce/karma/domain/product/service/ProductService.java +++ b/karma/src/main/java/com/ecyce/karma/domain/product/service/ProductService.java @@ -56,8 +56,8 @@ public ProductDetailResponse registerProduct(User user, ProductRequest dto) { Product product = ProductRequest.toEntity(user , dto); productRepository.save(product); // 옵션이 있다면 - if (dto.getOption() != null) { - for (OptionRequest optionDto : dto.getOption()) { + if (dto.getOptions() != null) { + for (OptionRequest optionDto : dto.getOptions()) { ProductOption productOption = optionDto.toEntity(product , optionDto); productOptionRepository.save(productOption); } @@ -171,20 +171,20 @@ public ResponseEntity Search(User user, String searchWord) { /* 카테고리별 조회*/ public ResponseEntity categorySort(User user, Long category) { - /* 1이면 후기순 */ - if(category.equals(1L)){ + + if(category.equals(1L)){ // 후기순 List sortedProducts = productRepository.findAllOrderByRatingDesc(); // 정렬된 결과 가져오기 // 로그인 여부에 따른 북마크 상태 포함 List productSimpleResponses = getProductSimpleResponses(user, sortedProducts); return ResponseEntity.ok(productSimpleResponses); } - else if(category.equals(2L)){ + else if(category.equals(2L)){ // 북마크 개수 순 List sortedProducts = productRepository.findAllOrderByBookmarkCountDesc(); // 로그인 여부에 따른 북마크 상태 포함 List productSimpleResponses = getProductSimpleResponses(user, sortedProducts); return ResponseEntity.ok(productSimpleResponses); } - else if(category.equals(3L)){ + else if(category.equals(3L)){ // 최근순 List sortedProducts = productRepository.findAllOrderByCreatedAtDesc(); List productSimpleResponse = getProductSimpleResponses(user , sortedProducts); return ResponseEntity.ok(productSimpleResponse); @@ -192,4 +192,22 @@ else if(category.equals(3L)){ else throw new CustomException(ErrorCode.INVALID_REQUEST); } + + /* 옵션 삭제 */ + public void deleteOption(User user , Long productId , Long optionId) { + ProductOption productOption = productOptionRepository.findByProductIdAndOptionId(productId , optionId); + if(!user.getUserId().equals(productOption.getProduct().getUser().getUserId())){ + throw new CustomException(ErrorCode.INVALID_ACCESS); + } + productOptionRepository.delete(productOption); + } + + /* 옵션 추가 */ + public ResponseEntity addOption(User user, Long productId, OptionRequest request) { + Product product = findProduct(productId); + ProductOption productOption = OptionRequest.toEntity(product ,request); + productOptionRepository.save(productOption); + OptionResponse response = OptionResponse.from(productOption); + return ResponseEntity.status(HttpStatus.CREATED).body(response); + } } From 2daec0385509c960bf79c3406d7ca50cddbcb495 Mon Sep 17 00:00:00 2001 From: oohyj Date: Wed, 27 Nov 2024 02:17:42 +0900 Subject: [PATCH 4/4] =?UTF-8?q?fix=20:=20=EB=8D=B0=EC=9D=B4=ED=84=B0=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 --- .../ecyce/karma/domain/product/service/ProductService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/karma/src/main/java/com/ecyce/karma/domain/product/service/ProductService.java b/karma/src/main/java/com/ecyce/karma/domain/product/service/ProductService.java index ae7ccb2..4e7d4e6 100644 --- a/karma/src/main/java/com/ecyce/karma/domain/product/service/ProductService.java +++ b/karma/src/main/java/com/ecyce/karma/domain/product/service/ProductService.java @@ -56,8 +56,8 @@ public ProductDetailResponse registerProduct(User user, ProductRequest dto) { Product product = ProductRequest.toEntity(user , dto); productRepository.save(product); // 옵션이 있다면 - if (dto.getOption() != null) { - for (OptionRequest optionDto : dto.getOption()) { + if (dto.getOptions() != null) { + for (OptionRequest optionDto : dto.getOptions()) { ProductOption productOption = optionDto.toEntity(product , optionDto); productOptionRepository.save(productOption); }