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) : 메인 랭킹 top 조회 api 틀 구현 #144

Merged
merged 1 commit into from
Aug 16, 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
4 changes: 3 additions & 1 deletion src/main/java/trothly/trothcam/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void configure(WebSecurity web) {
"/auth/check-id/**", "/auth/signup",
"/auth/login", "/auth/logout",
"/h2-console/**", "/health-check", "/sample/**", "/api/image/**",
"/api/products/**", "/product-detail"); // TODO: 2023/08/13 개발 완료 후 경로 삭제
"/api/products/**", "/api/product-detail", "/api/product-ranking/**"); // TODO: 2023/08/13 개발 완료 후 경로 삭제
}

// 스프링시큐리티 설정
Expand All @@ -64,6 +64,8 @@ protected void configure(HttpSecurity http) throws Exception {
.antMatchers("/health-check").permitAll()
.antMatchers("/sample/**").permitAll()
.antMatchers("/api/image/**").permitAll()
.antMatchers("/api/product-detail").permitAll()
.antMatchers("/api/product-ranking/**").permitAll()


//.antMatchers("/**").permitAll() // 로그인 개발 끝나면 삭제
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.web.bind.annotation.*;
import trothly.trothcam.domain.member.Member;
import trothly.trothcam.dto.web.ProductDetailResDto;
import trothly.trothcam.dto.web.ProductRankResDto;
import trothly.trothcam.dto.web.ProductReqDto;
import trothly.trothcam.dto.web.ProductsResDto;
import trothly.trothcam.exception.base.BaseException;
Expand All @@ -16,8 +17,11 @@
import trothly.trothcam.exception.custom.BadRequestException;
import trothly.trothcam.service.web.ProductService;

import java.util.ArrayList;
import java.util.List;

import static trothly.trothcam.exception.base.ErrorCode.REQUEST_ERROR;

@Slf4j
@RequiredArgsConstructor
@RestController
Expand All @@ -39,14 +43,42 @@ public BaseResponse<List<ProductsResDto>> findPublicProducts(@RequestParam(value
return BaseResponse.onSuccess(result);
}

/* 상품 detail 화면 조회 */ // 구현은 했으나 테스트 아직 못해봄
/* 상품 detail 화면 조회 - 로그인 0*/
@GetMapping("/product-detail/{webId}")
public BaseResponse<ProductDetailResDto> findProductDetailOn(@PathVariable String webId, @RequestBody ProductReqDto req, @AuthenticationPrincipal Member member) {
if(req.getProductId() == null) {
throw new BadRequestException("존재하지 않는 상품 아이디 입니다.");
}

ProductDetailResDto res = productService.findProductDetailOn(req, member);
return BaseResponse.onSuccess(res);
}

/* 상품 detail 화면 조회 - 로그인 X*/
@GetMapping("/product-detail")
public BaseResponse<ProductDetailResDto> findProductDetail(@RequestBody ProductReqDto req, @AuthenticationPrincipal Member member) {
public BaseResponse<ProductDetailResDto> findProductDetail(@RequestBody ProductReqDto req) {
if(req.getProductId() == null) {
throw new BadRequestException("존재하지 않는 상품 아이디 입니다.");
}

ProductDetailResDto res = productService.findProductDetail(req, member);
ProductDetailResDto res = productService.findProductDetail(req);
return BaseResponse.onSuccess(res);
}

/* 메인 화면 랭킹 top - 로그인 x */
@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")) {

} else {
throw new BaseException(REQUEST_ERROR);
}

return BaseResponse.onSuccess(result);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
@Repository
public interface HistoryRepository extends JpaRepository<History, Long> {

List<History> findAllByProductId(Long productId);
List<History> findAllByProductIdOrderBySoldAtDesc(Long productId);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package trothly.trothcam.domain.product;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import trothly.trothcam.dto.web.ProductRankDto;

import java.util.List;

Expand All @@ -10,4 +12,11 @@ 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" +
"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();
}
23 changes: 23 additions & 0 deletions src/main/java/trothly/trothcam/dto/web/ProductRankDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package trothly.trothcam.dto.web;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class ProductRankDto {
private Long historyId;
private Long productId;
private Long sellerId;
private Long buyerId;
private Long price;
private LocalDateTime soldAt;
private Long imageId;
private String title;
private int tags;
}
24 changes: 24 additions & 0 deletions src/main/java/trothly/trothcam/dto/web/ProductRankResDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package trothly.trothcam.dto.web;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class ProductRankResDto {
private Long historyId;
private Long productId;
private String ownerToken;
private String ownerName;
private String authorshipToken;
private String title;
private int tags;
private String imageUrl;
private Long price;
private LocalDateTime soldAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class HistoryService {

// 거래 내역 전체 조회
public List<HistoryDto> findAllHistory(ProductReqDto req) {
List<History> findHistories = historyRepository.findAllByProductId(req.getProductId());
List<History> findHistories = historyRepository.findAllByProductIdOrderBySoldAtDesc(req.getProductId());
// if(findHistories == null || findHistories.isEmpty()) {
// throw new BaseException(HISTORIES_NOT_FOUND);
// }
Expand Down
54 changes: 48 additions & 6 deletions src/main/java/trothly/trothcam/service/web/ProductService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
import org.springframework.transaction.annotation.Transactional;
import trothly.trothcam.domain.history.History;
import trothly.trothcam.domain.history.HistoryRepository;
import trothly.trothcam.domain.image.Image;
import trothly.trothcam.domain.image.ImageRepository;
import trothly.trothcam.domain.like.LikeProduct;
import trothly.trothcam.domain.like.LikeProductRepository;
import trothly.trothcam.domain.member.Member;
import trothly.trothcam.domain.member.MemberRepository;
import trothly.trothcam.domain.product.Product;
import trothly.trothcam.domain.product.ProductRepository;
import trothly.trothcam.domain.product.PublicYn;
import trothly.trothcam.dto.web.HistoryDto;
import trothly.trothcam.dto.web.ProductDetailResDto;
import trothly.trothcam.dto.web.ProductReqDto;
import trothly.trothcam.dto.web.ProductsResDto;
import trothly.trothcam.dto.web.*;
import trothly.trothcam.exception.base.BaseException;
import trothly.trothcam.exception.base.ErrorCode;
import trothly.trothcam.exception.custom.BadRequestException;
Expand All @@ -37,6 +36,7 @@ public class ProductService {
private final LikeProductRepository likeProductRepository;
private final ImageRepository imageRepository;
private final HistoryService historyService;
private final MemberRepository memberRepository;

/* 공개 인증서 조회 */
@Transactional(readOnly = true)
Expand All @@ -56,9 +56,9 @@ public List<ProductsResDto> findPublicProducts(String webId) throws BaseExceptio
return collect;
}

/* 상품 detail 화면 조회 */
/* 상품 detail 화면 조회 - 로그인 0 */
@Transactional
public ProductDetailResDto findProductDetail(ProductReqDto req, Member member) {
public ProductDetailResDto findProductDetailOn(ProductReqDto req, Member member) {
Boolean liked = false;

Product product = productRepository.findById(req.getProductId()).orElseThrow(
Expand All @@ -85,4 +85,46 @@ public ProductDetailResDto findProductDetail(ProductReqDto req, Member member) {
product.getTags(), product.getPrice(), product.getDescription(),product.getViews(), likes, product.getPublicYn(), product.getCreatedAt(),
product.getLastModifiedAt(), liked, historyDto);
}

/* 상품 detail 화면 조회 - 로그인 x */
@Transactional
public ProductDetailResDto findProductDetail(ProductReqDto req) {
Boolean liked = false;

Product product = productRepository.findById(req.getProductId()).orElseThrow(
() -> new BadRequestException("존재하지 않는 상품입니다.")
);

// 조회수 갱신
product.updateViews(product.getViews() + 1);

Long likes = likeProductRepository.countByProductId(req.getProductId());

List<HistoryDto> historyDto = historyService.findAllHistory(req);

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, historyDto);
}

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

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

for (ProductRankDto 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());

rankResDtos.add(productRankResDto);
}

return rankResDtos;
}
}