diff --git a/src/main/java/trothly/trothcam/controller/web/ProductController.java b/src/main/java/trothly/trothcam/controller/web/ProductController.java index 8f16a47..ce0fd8c 100644 --- a/src/main/java/trothly/trothcam/controller/web/ProductController.java +++ b/src/main/java/trothly/trothcam/controller/web/ProductController.java @@ -1,13 +1,15 @@ package trothly.trothcam.controller.web; import lombok.RequiredArgsConstructor; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.*; +import trothly.trothcam.domain.member.Member; import trothly.trothcam.domain.product.Product; +import trothly.trothcam.dto.web.ProductDetailResDto; +import trothly.trothcam.dto.web.ProductReqDto; import trothly.trothcam.dto.web.ProductsResDto; import trothly.trothcam.exception.base.BaseResponse; +import trothly.trothcam.exception.custom.BadRequestException; import trothly.trothcam.service.web.ProductService; import java.util.List; @@ -34,4 +36,14 @@ public class ProductController { // return new BaseResponse(collect); // } + /* 상품 detail 화면 조회 */ // 구현은 했으나 테스트 아직 못해봄 +// @GetMapping("/product-detail") +// public BaseResponse findProductDetail(@RequestBody ProductReqDto req, @AuthenticationPrincipal Member member) { +// if(req.getProductId() == null) { +// throw new BadRequestException("존재하지 않는 상품 아이디 입니다."); +// } +// +// ProductDetailResDto res = productService.findProductDetail(req, member); +// return BaseResponse.onSuccess(res); +// } } diff --git a/src/main/java/trothly/trothcam/domain/product/Product.java b/src/main/java/trothly/trothcam/domain/product/Product.java index 7fd52d1..5ad4b04 100644 --- a/src/main/java/trothly/trothcam/domain/product/Product.java +++ b/src/main/java/trothly/trothcam/domain/product/Product.java @@ -59,4 +59,8 @@ public class Product extends BaseTimeEntity { @Column(name = "created_at", nullable = false) private LocalDateTime createdAt; + + public void updateViews(int views) { + this.views = views; + } } diff --git a/src/main/java/trothly/trothcam/service/web/ProductService.java b/src/main/java/trothly/trothcam/service/web/ProductService.java index a64aad8..8a1c99f 100644 --- a/src/main/java/trothly/trothcam/service/web/ProductService.java +++ b/src/main/java/trothly/trothcam/service/web/ProductService.java @@ -32,6 +32,8 @@ public class ProductService { private final ProductRepository productRepository; + private final HistoryRepository historyRepository; + private final LikeProductRepository likeProductRepository; // // /* 공개 인증서 조회 */ @@ -44,4 +46,33 @@ public class ProductService { // return findProducts; // } + /* 상품 detail 화면 조회 */ + @Transactional(readOnly = true) + public ProductDetailResDto findProductDetail(ProductReqDto req, Member member) { + Boolean liked = false; + + Product product = productRepository.findById(req.getProductId()).orElseThrow( + () -> new BadRequestException("존재하지 않는 상품입니다.") + ); + + // 조회수 갱신 + product.updateViews(product.getViews() + 1); + + Long likes = likeProductRepository.countByProductId(req.getProductId()); + + //좋아요 여부 + Optional isLiked = likeProductRepository.findByProductIdAndMemberId(req.getProductId(), member.getId()); + + if(isLiked.isPresent()) { + liked = true; + } else { + liked = false; + } + + List histories = historyRepository.findAllByProductId(req.getProductId()); + + return new ProductDetailResDto(req.getProductId(), product.getImage().getId(), product.getMember().getId(), product.getTitle(), + product.getTags(), product.getPrice(), product.getDescription(),product.getViews(), likes, product.getPublicYn(), product.getCreatedAt(), + product.getLastModifiedAt(), liked, histories); + } }