-
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.
Merge pull request #24 from Team-A-Mango/23-buy-product-function
상품 구매 기능
- Loading branch information
Showing
9 changed files
with
93 additions
and
5 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
.../com/mango/amango/domain/order/Order.java → ...ngo/amango/domain/order/entity/Order.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
8 changes: 8 additions & 0 deletions
8
src/main/java/com/mango/amango/domain/order/repository/OrderRepository.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 com.mango.amango.domain.order.repository; | ||
|
||
import com.mango.amango.domain.order.entity.Order; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface OrderRepository extends JpaRepository<Order, Long> { | ||
boolean existsByProductId(Long productId); | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/mango/amango/domain/order/util/OrderConverter.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,15 @@ | ||
package com.mango.amango.domain.order.util; | ||
|
||
import com.mango.amango.domain.order.entity.Order; | ||
import com.mango.amango.domain.product.entity.Product; | ||
import com.mango.amango.domain.user.entity.User; | ||
|
||
public abstract class OrderConverter { | ||
|
||
public static Order toEntity (Product product, User user) { | ||
return Order.builder() | ||
.product(product) | ||
.user(user) | ||
.build(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/mango/amango/domain/product/exception/ProductAlreadyTradedException.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 com.mango.amango.domain.product.exception; | ||
|
||
import com.mango.amango.global.exception.CustomErrorCode; | ||
import com.mango.amango.global.exception.CustomException; | ||
|
||
public class ProductAlreadyTradedException extends CustomException { | ||
public ProductAlreadyTradedException() { | ||
super(CustomErrorCode.PRODUCT_ALREADY_TRADED); | ||
} | ||
} |
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
5 changes: 5 additions & 0 deletions
5
src/main/java/com/mango/amango/domain/product/service/BuyProductService.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,5 @@ | ||
package com.mango.amango.domain.product.service; | ||
|
||
public interface BuyProductService { | ||
void execute(Long productId); | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/mango/amango/domain/product/service/impl/BuyProductServiceImpl.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,39 @@ | ||
package com.mango.amango.domain.product.service.impl; | ||
|
||
import com.mango.amango.domain.order.entity.Order; | ||
import com.mango.amango.domain.order.repository.OrderRepository; | ||
import com.mango.amango.domain.order.util.OrderConverter; | ||
import com.mango.amango.domain.product.entity.Product; | ||
import com.mango.amango.domain.product.exception.NotFoundProductException; | ||
import com.mango.amango.domain.product.exception.ProductAlreadyTradedException; | ||
import com.mango.amango.domain.product.repository.ProductRepository; | ||
import com.mango.amango.domain.product.service.BuyProductService; | ||
import com.mango.amango.domain.user.entity.User; | ||
import com.mango.amango.domain.user.service.UserService; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class BuyProductServiceImpl implements BuyProductService { | ||
|
||
private final ProductRepository productRepository; | ||
private final UserService userService; | ||
private final OrderRepository orderRepository; | ||
|
||
public void execute(Long productId) { | ||
User user = userService.getCurrentUser(); | ||
|
||
Product product = productRepository.findById(productId) | ||
.orElseThrow(NotFoundProductException::new); | ||
|
||
if (orderRepository.existsByProductId(product.getId())) { | ||
throw new ProductAlreadyTradedException(); | ||
} else { | ||
Order order = OrderConverter.toEntity(product, user); | ||
orderRepository.save(order); | ||
} | ||
} | ||
} |
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
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