Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#125) : product detail api 로직 틀 구현 (테스트 전) #135

Merged
merged 1 commit into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}