Skip to content

Commit

Permalink
Merge pull request #24 from Team-A-Mango/23-buy-product-function
Browse files Browse the repository at this point in the history
상품 구매 기능
  • Loading branch information
ta2ye0n authored Dec 17, 2024
2 parents 9ef8a48 + 4260770 commit 494dcf9
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mango.amango.domain.order;
package com.mango.amango.domain.order.entity;

import com.mango.amango.domain.product.entity.Product;
import com.mango.amango.domain.user.entity.User;
Expand Down
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);
}
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();
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mango.amango.domain.product.presentation.dto.request.CreateProductReq;
import com.mango.amango.domain.product.presentation.dto.response.GetProductRes;
import com.mango.amango.domain.product.presentation.dto.response.FindAllProductRes;
import com.mango.amango.domain.product.service.BuyProductService;
import com.mango.amango.domain.product.service.CreateProductService;
import com.mango.amango.domain.product.service.GetProductService;
import com.mango.amango.domain.product.service.FindAllProductService;
Expand All @@ -22,9 +23,10 @@
@RequestMapping("/product")
public class ProductController {

public final CreateProductService createProductService;
public final GetProductService getProductService;
public final FindAllProductService findAllProductService;
private final CreateProductService createProductService;
private final GetProductService getProductService;
private final FindAllProductService findAllProductService;
private final BuyProductService buyProductService;

@PostMapping
public ResponseEntity<Void> createProduct(
Expand All @@ -40,11 +42,17 @@ public ResponseEntity<GetProductRes> getProduct(@PathVariable Long productId) {
GetProductRes res = getProductService.execute(productId);
return ResponseEntity.ok(res);
}

@GetMapping
public ResponseEntity<List<FindAllProductRes>> findAllProducts() {
List<FindAllProductRes> res = findAllProductService.execute();
return ResponseEntity.ok(res);

}

@PostMapping("/{productId}")
public ResponseEntity<Void> orderProduct(@PathVariable Long productId) {
buyProductService.execute(productId);
return ResponseEntity.status(CREATED).build();
}
}
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);
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public enum CustomErrorCode {
FILE_PROCESSING_ERROR(HttpStatus.BAD_REQUEST, "처리 할 수 없는 파일입니다."),
MAIL_DELIVERY_FAILED(HttpStatus.INTERNAL_SERVER_ERROR, "매일 전송을 실패하였습니다."),

NOT_FOUND_PRODUCT(HttpStatus.NOT_FOUND,"해당 상품을 찾을 수 없습니다.")
NOT_FOUND_PRODUCT(HttpStatus.NOT_FOUND,"해당 상품을 찾을 수 없습니다."),

PRODUCT_ALREADY_TRADED(HttpStatus.BAD_REQUEST, "이미 거래된 상품입니다.")
;

private final HttpStatus statusCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.requestMatchers(POST, "/email").permitAll()
.requestMatchers(GET, "/product/**").permitAll()
.requestMatchers(POST, "/product").hasAuthority(USER.getKey())
.requestMatchers(POST, "/product/**").hasAuthority(USER.getKey())
.requestMatchers(POST, "/inquiry/**").hasAuthority(USER.getKey())
.anyRequest().authenticated()
)
Expand Down

0 comments on commit 494dcf9

Please sign in to comment.