Skip to content

Commit

Permalink
feat(#125) : 메인 랭킹뷰 top/latest api 구현 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
yeon015 committed Aug 17, 2023
1 parent 921944a commit 2606582
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,21 @@ public BaseResponse<ProductDetailResDto> findProductDetail(@RequestBody ProductR
return BaseResponse.onSuccess(res);
}

/* 메인 화면 랭킹 top - 로그인 x */
/* 메인 화면 랭킹 top */
@GetMapping("/product-ranking/{type}")
public BaseResponse<List<ProductRankResDto>> findRanking(@PathVariable String type) {
List<ProductRankResDto> result = new ArrayList<>();

if(type.equals("top")) {
result = productService.findProductRankTop();
} else if(type.equals("latest")) {

result = productService.findProductRankLatest();
} else {
throw new BaseException(REQUEST_ERROR);
}

return BaseResponse.onSuccess(result);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import trothly.trothcam.domain.member.Member;
import trothly.trothcam.dto.web.ProductRankDto;

import java.time.LocalDateTime;
import java.util.List;

import static trothly.trothcam.domain.product.PublicYn.N;
Expand All @@ -16,13 +17,30 @@ public interface ProductRepository extends JpaRepository<Product, Long> {
List<Product> findAllByMember_WebIdAndPublicYn(String webId, PublicYn publicYn); // 인증서 조회
List<Product> findAllByMember_IdAndPublicYn(Long id, PublicYn publicYn); // 인증서 조회

@Query(value = "select ap.history_id, ap.product_id, ap.seller_id, ap.buyer_id, ap.price, ap.sold_at, p.image_id, p.title, p.tags\n" +
@Query(value = "select ap.history_id as historyId, ap.product_id as productId, ap.seller_id as sellerId, ap.buyer_id as buyerId, ap.price as price, ap.sold_at as soldAt, p.image_id as imageId, p.title as title, p.tags as tags\n" +
"from (select *, rank() over (partition by h.product_id order by price desc, sold_at asc) as rank from history h) as ap join product p on ap.product_id = p.product_id\n" +
"where ap.rank <= 1\n" +
"order by price desc, sold_at asc\n" +
"LIMIT 10", nativeQuery = true)
List<ProductRankDto> findProductRandDto();
List<ProductTop> findProductRandDto();

@Query(value = "select h.history_id as historyId, h.product_id as productId, h.seller_id as sellerId, h.buyer_id as buyerId, h.price as price, h.sold_at as soldAt, p.image_id as imageId, p.title as title, p.tags as tags \n" +
"from history h join product p on h.product_id = p.product_id order by sold_at desc LIMIT 10", nativeQuery = true)
List<ProductTop> findProductLatestDto();


// 비공개 인증서 리스트 조회
List<Product> findAllByMemberAndIdAndPublicYn(Member member, Long productId, PublicYn publicYn);

interface ProductTop {
Long getHistoryId();
Long getProductId();
Long getSellerId();
Long getBuyerId();
Long getPrice();
LocalDateTime getSoldAt();
Long getImageId();
String getTitle();
int getTags();
}
}
45 changes: 38 additions & 7 deletions src/main/java/trothly/trothcam/service/web/ProductService.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,55 @@ public ProductDetailResDto findProductDetail(ProductReqDto req) {
product.getLastModifiedAt(), liked, historyDto);
}

/* 메인 랭킹 top 조회 - 로그인 x */
/* 메인 랭킹 top 조회 */
@Transactional
public List<ProductRankResDto> findProductRankTop() {
List<ProductRankDto> productRankDtos = productRepository.findProductRandDto();
List<ProductRepository.ProductTop> productRankDtos = productRepository.findProductRandDto();

List<ProductRankResDto> rankResDtos = new ArrayList<>();

for (ProductRankDto productRank : productRankDtos) {
for (ProductRepository.ProductTop productRank : productRankDtos) {
Optional<Member> owner = memberRepository.findById(productRank.getBuyerId());
Optional<Image> image = imageRepository.findById(productRank.getImageId());

ProductRankResDto productRankResDto = new ProductRankResDto(productRank.getHistoryId(), productRank.getProductId(), owner.get().getWebToken(), owner.get().getName(),
image.get().getMember().getWebToken(), productRank.getTitle(), productRank.getTags(), image.get().getImageUrl(),
productRank.getPrice(), productRank.getSoldAt());
log.info(owner.get().getId().toString());
log.info(image.get().getId().toString());

rankResDtos.add(productRankResDto);
if(owner.isPresent() && image.isPresent()) {
ProductRankResDto productRankResDto = new ProductRankResDto(productRank.getHistoryId(), productRank.getProductId(), owner.get().getWebToken(), owner.get().getName(),
image.get().getMember().getWebToken(), productRank.getTitle(), productRank.getTags(), image.get().getImageUrl(),
productRank.getPrice(), productRank.getSoldAt());

rankResDtos.add(productRankResDto);
}
}

return rankResDtos;
}

/* 메인 랭킹 latest 조회 */
@Transactional
public List<ProductRankResDto> findProductRankLatest() {
List<ProductRepository.ProductTop> productLatestDtos = productRepository.findProductLatestDto();

List<ProductRankResDto> latestResDtos = new ArrayList<>();

for (ProductRepository.ProductTop productLatest : productLatestDtos) {
Optional<Member> owner = memberRepository.findById(productLatest.getBuyerId());
Optional<Image> image = imageRepository.findById(productLatest.getImageId());

log.info(owner.get().getId().toString());
log.info(image.get().getId().toString());

if(owner.isPresent() && image.isPresent()) {
ProductRankResDto productRankResDto = new ProductRankResDto(productLatest.getHistoryId(), productLatest.getProductId(), owner.get().getWebToken(), owner.get().getName(),
image.get().getMember().getWebToken(), productLatest.getTitle(), productLatest.getTags(), image.get().getImageUrl(),
productLatest.getPrice(), productLatest.getSoldAt());

latestResDtos.add(productRankResDto);
}
}

return latestResDtos;
}
}

0 comments on commit 2606582

Please sign in to comment.