-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
✨ [FEATURE] 찜하기 기능 구현 (#13)
- Loading branch information
Showing
7 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
src/main/java/umc/unimade/domain/accounts/repository/BuyerRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package umc.unimade.domain.accounts.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import umc.unimade.domain.accounts.entity.Buyer; | ||
|
||
public interface BuyerRepository extends JpaRepository<Buyer, Long> { | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/umc/unimade/domain/favorite/repository/FavoriteProductRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package umc.unimade.domain.favorite.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import umc.unimade.domain.accounts.entity.Buyer; | ||
import umc.unimade.domain.favorite.entity.FavoriteProduct; | ||
import umc.unimade.domain.products.entity.Products; | ||
|
||
import java.util.Optional; | ||
|
||
public interface FavoriteProductRepository extends JpaRepository<FavoriteProduct, Long> { | ||
Optional<FavoriteProduct> findByProductAndBuyer(Products product, Buyer buyer); | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/umc/unimade/domain/favorite/repository/FavoriteSellerRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package umc.unimade.domain.favorite.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import umc.unimade.domain.favorite.entity.FavoriteSeller; | ||
|
||
|
||
public interface FavoriteSellerRepository extends JpaRepository<FavoriteSeller, Long> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/umc/unimade/domain/products/service/ProductsCommandService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package umc.unimade.domain.products.service; | ||
|
||
import io.netty.util.internal.ObjectPool; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import umc.unimade.domain.accounts.entity.Buyer; | ||
import umc.unimade.domain.accounts.repository.BuyerRepository; | ||
import umc.unimade.domain.favorite.entity.FavoriteProduct; | ||
import umc.unimade.domain.favorite.repository.FavoriteProductRepository; | ||
import umc.unimade.domain.favorite.repository.FavoriteSellerRepository; | ||
import umc.unimade.domain.products.ProductRepository; | ||
import umc.unimade.domain.products.entity.Products; | ||
import umc.unimade.global.common.ApiResponse; | ||
import umc.unimade.global.common.ErrorCode; | ||
import umc.unimade.global.common.exception.ProductsExceptionHandler; | ||
import umc.unimade.global.common.exception.UserExceptionHandler; | ||
|
||
import java.util.Optional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ProductsCommandService { | ||
|
||
private final FavoriteProductRepository favoriteProductRepository; | ||
private final FavoriteSellerRepository favoriteSellerRepository; | ||
private final ProductRepository productRepository; | ||
private final BuyerRepository buyerRepository; | ||
|
||
@Transactional | ||
public ApiResponse<Void> toggleFavoriteProduct(Long productId, Long buyerId) { | ||
Products product = findProductById(productId); | ||
Buyer buyer = findBuyerById(buyerId); | ||
Optional<FavoriteProduct> existingFavorite = findFavoriteProduct(product,buyer); | ||
|
||
if (existingFavorite.isPresent()) { | ||
favoriteProductRepository.delete(existingFavorite.get()); | ||
return ApiResponse.CANCELED_LIKE(); | ||
}else{ | ||
FavoriteProduct favoriteProduct = FavoriteProduct.builder() | ||
.buyer(buyer) | ||
.product(product) | ||
.build(); | ||
favoriteProductRepository.save(favoriteProduct); | ||
return ApiResponse.SUCCESS_LIKE(); | ||
} | ||
} | ||
|
||
|
||
|
||
private Buyer findBuyerById(Long buyerId) { | ||
return buyerRepository.findById(buyerId).orElseThrow(() -> new UserExceptionHandler(ErrorCode.BUYER_NOT_FOUND)); | ||
} | ||
private Products findProductById(Long productId){ | ||
return productRepository.findById(productId).orElseThrow(() -> new ProductsExceptionHandler(ErrorCode.PRODUCT_NOT_FOUND)); | ||
} | ||
private Optional<FavoriteProduct> findFavoriteProduct(Products product, Buyer buyer) { | ||
return favoriteProductRepository.findByProductAndBuyer(product, buyer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/umc/unimade/global/common/exception/UserExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package umc.unimade.global.common.exception; | ||
|
||
import umc.unimade.global.common.BaseErrorCode; | ||
|
||
public class UserExceptionHandler extends CustomException{ | ||
|
||
public UserExceptionHandler (BaseErrorCode code ){ | ||
super(code); | ||
} | ||
} |