From 5809f2fed8d5c29662a3c45ab5deb2ba4848e9ad Mon Sep 17 00:00:00 2001 From: fnzl54 Date: Sat, 9 Mar 2024 01:08:27 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20=EC=83=81=ED=92=88=EC=9D=98=20?= =?UTF-8?q?=EC=83=81=ED=83=9C=EA=B0=92=20=EB=B3=80=EA=B2=BD=20(=ED=8C=90?= =?UTF-8?q?=EB=A7=A4=EC=8B=9C=20=EA=B5=AC=EB=A7=A4=EC=9E=90=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=20=EC=A0=80=EC=9E=A5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/UserPurchaseRepository.java | 7 ++++++- .../controller/ProductController.java | 3 ++- .../products/service/ProductSubService.java | 20 +++++++++++++++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/teamjo/techeermarket/domain/mypage/repository/UserPurchaseRepository.java b/src/main/java/com/teamjo/techeermarket/domain/mypage/repository/UserPurchaseRepository.java index 82dea08..d6ba0b6 100644 --- a/src/main/java/com/teamjo/techeermarket/domain/mypage/repository/UserPurchaseRepository.java +++ b/src/main/java/com/teamjo/techeermarket/domain/mypage/repository/UserPurchaseRepository.java @@ -3,16 +3,21 @@ import com.teamjo.techeermarket.domain.mypage.entity.UserPurchase; import com.teamjo.techeermarket.domain.products.entity.Products; import com.teamjo.techeermarket.domain.users.entity.Users; +import java.util.Optional; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; public interface UserPurchaseRepository extends JpaRepository { boolean existsBySellerIdAndProducts(Users sellerId, Products products); - Page findByBuyerId(Users buyerId, Pageable pageable); + @Query("SELECT p FROM UserPurchase p WHERE p.products.id = :productId") + Optional findUserPurchaseByProducts(@Param("productId") Long productId); + void deleteByProducts(Products products); // List findByBuyer(Users buyer, Pageable pageable); diff --git a/src/main/java/com/teamjo/techeermarket/domain/products/controller/ProductController.java b/src/main/java/com/teamjo/techeermarket/domain/products/controller/ProductController.java index 7be4163..e6f9d0a 100644 --- a/src/main/java/com/teamjo/techeermarket/domain/products/controller/ProductController.java +++ b/src/main/java/com/teamjo/techeermarket/domain/products/controller/ProductController.java @@ -63,6 +63,7 @@ public ResponseEntity updateProductState( String email = userDetailsImpl.getUsername(); // 요청 바디에서 state 값을 가져옴 String state = requestBody.get("state"); + String buyerEmail = requestBody.getOrDefault("buyerEmail" , null); // 상태가 올바른지 확인 try{ ProductState productState = ProductState.valueOf(state); @@ -70,7 +71,7 @@ public ResponseEntity updateProductState( throw new InvalidProductStateException() ; } // 서비스를 통해 상태 변경 - productSubService.updateProductState(productId, ProductState.valueOf(state),email); + productSubService.updateProductState(productId, ProductState.valueOf(state), email, buyerEmail); return ResponseEntity.status(HttpStatus.OK).body("Product state updated successfully"); } diff --git a/src/main/java/com/teamjo/techeermarket/domain/products/service/ProductSubService.java b/src/main/java/com/teamjo/techeermarket/domain/products/service/ProductSubService.java index 6c20f6f..19bde0f 100644 --- a/src/main/java/com/teamjo/techeermarket/domain/products/service/ProductSubService.java +++ b/src/main/java/com/teamjo/techeermarket/domain/products/service/ProductSubService.java @@ -1,5 +1,9 @@ package com.teamjo.techeermarket.domain.products.service; +import static com.teamjo.techeermarket.domain.products.entity.ProductState.RESERVED; +import static com.teamjo.techeermarket.domain.products.entity.ProductState.SALE; +import static com.teamjo.techeermarket.domain.products.entity.ProductState.SOLD; + import com.teamjo.techeermarket.domain.images.repository.ProductImageRepository; import com.teamjo.techeermarket.domain.images.service.ProductImageService; import com.teamjo.techeermarket.domain.mypage.entity.UserLike; @@ -84,9 +88,10 @@ public void increaseViewCount (Long productId) { // 상품 게시물 상태 변경 */ @Transactional - public void updateProductState(Long productId, ProductState newState, String email) { + public void updateProductState(Long productId, ProductState newState, String email, String buyerEmail) { Users findUsers = userRepository.findUserByEmail(email) .orElseThrow(UserNotFoundException::new); + // 상품을 데이터베이스에서 찾음 Products product = productRepository.findById(productId) .orElseThrow(ProductNotFoundException::new); @@ -96,7 +101,18 @@ public void updateProductState(Long productId, ProductState newState, String ema throw new NotYourProductException(); } // 상태 업데이트 - product.setProductState(newState); + if (newState.equals(RESERVED) || newState.equals(SALE)) { + product.setProductState(newState); + } else if (newState.equals(SOLD)) { // 판매 경우 + Users BuyerUsers = userRepository.findUserByEmail(email) + .orElseThrow(UserNotFoundException::new); + product.setProductState(newState); + + UserPurchase userPurchase = userPurchaseRepository.findUserPurchaseByProducts(productId) + .orElseThrow(ProductNotFoundException::new); + userPurchase.setBuyerId(BuyerUsers); + } + productRepository.save(product); }