Skip to content

Commit

Permalink
Merge pull request #135 from Troth-Cam/feat/product-detail
Browse files Browse the repository at this point in the history
feat(#125) : product detail api 로직 틀 구현 (테스트 전)
  • Loading branch information
yeon015 authored Aug 12, 2023
2 parents aaa32f9 + 8693e22 commit ff8a657
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -34,4 +36,14 @@ public class ProductController {
// return new BaseResponse<ProductsResDto>(collect);
// }

/* 상품 detail 화면 조회 */ // 구현은 했으나 테스트 아직 못해봄
// @GetMapping("/product-detail")
// public BaseResponse<ProductDetailResDto> findProductDetail(@RequestBody ProductReqDto req, @AuthenticationPrincipal Member member) {
// if(req.getProductId() == null) {
// throw new BadRequestException("존재하지 않는 상품 아이디 입니다.");
// }
//
// ProductDetailResDto res = productService.findProductDetail(req, member);
// return BaseResponse.onSuccess(res);
// }
}
4 changes: 4 additions & 0 deletions src/main/java/trothly/trothcam/domain/product/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
31 changes: 31 additions & 0 deletions src/main/java/trothly/trothcam/service/web/ProductService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
public class ProductService {

private final ProductRepository productRepository;
private final HistoryRepository historyRepository;
private final LikeProductRepository likeProductRepository;

//
// /* 공개 인증서 조회 */
Expand All @@ -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<LikeProduct> isLiked = likeProductRepository.findByProductIdAndMemberId(req.getProductId(), member.getId());

if(isLiked.isPresent()) {
liked = true;
} else {
liked = false;
}

List<History> 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);
}
}

0 comments on commit ff8a657

Please sign in to comment.