Skip to content

Commit

Permalink
feat : 상품의 상태값 변경 (판매시 구매자 정보 저장)
Browse files Browse the repository at this point in the history
  • Loading branch information
fnzl54 committed Mar 8, 2024
1 parent 1faafc1 commit 5809f2f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserPurchase, Long> {
boolean existsBySellerIdAndProducts(Users sellerId, Products products);


Page<UserPurchase> findByBuyerId(Users buyerId, Pageable pageable);

@Query("SELECT p FROM UserPurchase p WHERE p.products.id = :productId")
Optional<UserPurchase> findUserPurchaseByProducts(@Param("productId") Long productId);

void deleteByProducts(Products products);

// List<UserPurchase> findByBuyer(Users buyer, Pageable pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ public ResponseEntity<String> updateProductState(
String email = userDetailsImpl.getUsername();
// 요청 바디에서 state 값을 가져옴
String state = requestBody.get("state");
String buyerEmail = requestBody.getOrDefault("buyerEmail" , null);
// 상태가 올바른지 확인
try{
ProductState productState = ProductState.valueOf(state);
} catch (IllegalArgumentException e) {
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");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

Expand Down

0 comments on commit 5809f2f

Please sign in to comment.