Skip to content

Commit

Permalink
Merge pull request #71 from MarketPlace-O2O-Platform/issue/#70
Browse files Browse the repository at this point in the history
[Issue/#70] 가장 많은 찜 순 매장 페이징 조회 관련 멤버 찜 여부 추가 & 데이터 건너 뜀 방지 조건 추가
  • Loading branch information
82everywin authored Nov 29, 2024
2 parents 5b50a5b + 7d268f7 commit 08680d7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public ResponseEntity<CommonResponse<MarketDetailsRes>> getMarket(@PathVariable

// size가 null로 들어올 시 기본 값을 지정해주기 위해 integer로 값을 받아온다.
@Operation(summary = "전체 매장 조회",
description = "처음 요청 시, pageSize만 필요합니다. 기본값은 5입니다. <br>" +
description = "처음 요청 시, pageSize만 필요합니다. 기본값은 10입니다. <br>" +
"카테고리 별 매장을 조회하시려면 category도 필요합니다. <br>" +
"최신순으로 보여줍니다.")
@GetMapping
Expand All @@ -44,7 +44,7 @@ public ResponseEntity<CommonResponse<MarketPageRes<MarketRes>>> getMarket(
@Parameter(description = "페이지의 마지막 marketId")
@RequestParam(required = false, name = "lastPageIndex") Long marketId,
@RequestParam(required = false, name = "category") String major,
@RequestParam(defaultValue = "5", name = "pageSize") Integer size)
@RequestParam(defaultValue = "10", name = "pageSize") Integer size)
{
return ResponseEntity
.ok(CommonResponse.from(MARKET_FOUND.getMessage()
Expand All @@ -53,41 +53,43 @@ public ResponseEntity<CommonResponse<MarketPageRes<MarketRes>>> getMarket(

@Operation(summary = "자신이 찜한 매장 조회",
description = "자신이 찜한 매장 정보 리스트를 반환합니다. <br>" +
"처음 요청 시엔 pageSize만 필요합니다.기본값은 5입니다. <br>" +
"처음 요청 시엔 pageSize만 필요합니다.기본값은 10입니다. <br>" +
"JWT 구현 전 까지는 memberId를 추가해야합니다. <br>" +
"최신 찜 순으로 보여줍니다.")
@GetMapping("/my-favorite")
public ResponseEntity<CommonResponse<MarketPageRes<MyFavoriteMarketRes>>> getMemberFavoriteMarketList(
@RequestParam Long memberId,
@Parameter(description = "페이지의 마지막 favoriteModifiedAt (e.g. 2024-11-25 11:19:26.378948 )")
@RequestParam(required = false, name = "lastModifiedAt") LocalDateTime lastModifiedAt,
@RequestParam(defaultValue = "5", name = "pageSize") Integer size) {
@RequestParam(defaultValue = "10", name = "pageSize") Integer size) {
return ResponseEntity
.ok(CommonResponse.from(MARKET_FOUND.getMessage()
,marketService.getMyFavoriteMarketPage(memberId,lastModifiedAt,size)));
}

@Operation(summary = "찜 수가 가장 많은 매장 더보기 조회",
description = "사용자들이 가장 많이 찜한 매장 정보 리스트를 반환합니다. <br>" +
"처음 요청 시엔 pageSize만 필요합니다. 기본값은 5입니다. <br>" +
"처음 요청 시엔 pageSize만 필요합니다. 기본값은 10입니다. <br>" +
"찜 수가 가장 많은 순으로 보여줍니다.")
@GetMapping("/favorite")
public ResponseEntity<CommonResponse<MarketPageRes<FavoriteMarketRes>>> getTopFavoriteMarketList(
@RequestParam Long memberId,
@Parameter(description = "페이지의 마지막 marketId")
@RequestParam(required = false, name = "lastMarketId") Long marketId,
@Parameter(description = "페이지의 마지막 favoriteCount")
@RequestParam(required = false, name = "lastFavoriteCount") Long count,
@RequestParam(defaultValue = "5", name = "pageSize") Integer size) {
@RequestParam(defaultValue = "10", name = "pageSize") Integer size) {
return ResponseEntity
.ok(CommonResponse.from(MARKET_FOUND.getMessage()
,marketService.getFavoriteMarketPage(memberId,count,size)));
,marketService.getFavoriteMarketPage(memberId,marketId,count,size)));
}

@Operation(summary = "찜 수가 가장 많은 매장 TOP 조회",
description = "사용자들이 가장 많이 찜한 매장 Top을 반환합니다. <br>" +
"size의 기본값은 5입니다.")
"size의 기본값은 10입니다.")
@GetMapping("/top-favorite")
public ResponseEntity<CommonResponse<List<TopFavoriteMarketRes>>> getTopFavoriteMarketList(
@RequestParam(defaultValue = "5", name = "size") Integer size) {
@RequestParam(defaultValue = "10", name = "size") Integer size) {
return ResponseEntity
.ok(CommonResponse.from(MARKET_FOUND.getMessage()
,marketService.getTopFavoriteMarkets(size)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface MarketRepositoryCustom {

public List<MyFavoriteMarketRes> findMyFavoriteMarketList(Long memberId, LocalDateTime lastModifiedAt, Integer size);

public List<FavoriteMarketRes> findFavoriteMarketList(Long memberId, Long count, Integer size);
public List<FavoriteMarketRes> findFavoriteMarketList(Long memberId,Long marketId, Long count, Integer size);

public List<TopFavoriteMarketRes> findTopFavoriteMarkets(Integer size);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.appcenter.marketplace.domain.market.repository;

import com.appcenter.marketplace.domain.coupon.QCoupon;
import com.appcenter.marketplace.domain.favorite.QFavorite;
import com.appcenter.marketplace.domain.image.dto.res.QImageRes;
import com.appcenter.marketplace.domain.market.dto.res.*;
import com.querydsl.core.BooleanBuilder;
Expand Down Expand Up @@ -36,7 +37,7 @@ public List<MarketDetailsRes> findMarketDetailList(Long marketId) {
// market과 image를 조인 하여 매장 정보와 순서에 오름차순인 이미지 리스트를 dto에 매핑한다.
return jpaQueryFactory
.from(market)
.innerJoin(image.market,market)
.innerJoin(image).on(image.market.eq(market))
.where(market.id.eq(marketId)) // 매장 ID로 필터링
.orderBy(image.sequence.asc())
// transfrom을 통해 쿼리 결과를 원하는 형태로 변환한다.
Expand Down Expand Up @@ -67,8 +68,9 @@ public List<MarketRes> findMarketList(Long memberId, Long marketId, Integer size
market.thumbnail,
favorite.id.isNotNull()))
.from(market)
.leftJoin(favorite.market,market).on(favorite.member.id.eq(memberId)
.and(favorite.isDeleted.eq(false)))
.leftJoin(favorite).on(market.eq(favorite.market)
.and(favorite.member.id.eq(memberId)
.and(favorite.isDeleted.eq(false))))
.innerJoin(market.local,local)
.innerJoin(local.metro,metro)
.where(ltMarketId(marketId))
Expand All @@ -89,8 +91,9 @@ public List<MarketRes> findMarketListByCategory(Long memberId, Long marketId, In
market.thumbnail,
favorite.id.isNotNull()))
.from(market)
.leftJoin(favorite.market,market).on(favorite.member.id.eq(memberId)
.and(favorite.isDeleted.eq(false)))
.leftJoin(favorite).on(market.eq(favorite.market)
.and(favorite.member.id.eq(memberId)
.and(favorite.isDeleted.eq(false))))
.innerJoin(market.category,category)
.innerJoin(market.local,local)
.innerJoin(local.metro,metro)
Expand All @@ -113,8 +116,9 @@ public List<MyFavoriteMarketRes> findMyFavoriteMarketList(Long memberId, LocalDa
favorite.id.isNotNull(),
favorite.modifiedAt))
.from(market)
.innerJoin(favorite.market,market).on(favorite.member.id.eq(memberId)
.and(favorite.isDeleted.eq(false)))
.innerJoin(favorite).on(market.eq(favorite.market)
.and(favorite.member.id.eq(memberId)
.and(favorite.isDeleted.eq(false))))
.innerJoin(market.local,local)
.innerJoin(local.metro,metro)
.where(ltFavoriteModifiedAt(lastModifiedAt)) // 회원 자신이 동시간대에 찜할 수 없으므로 lt이다.
Expand All @@ -125,25 +129,31 @@ public List<MyFavoriteMarketRes> findMyFavoriteMarketList(Long memberId, LocalDa

// 찜 수가 가장 많은 매장 페이징 조회
@Override
public List<FavoriteMarketRes> findFavoriteMarketList(Long memberId, Long count, Integer size) {
public List<FavoriteMarketRes> findFavoriteMarketList(Long memberId,Long marketId, Long count, Integer size) {
QFavorite favoriteMember = new QFavorite("favoriteMember"); // 해당 사용자의 각 매장의 찜 여부 확인을 위한 별칭 생성

return jpaQueryFactory
.select(new QFavoriteMarketRes(
market.id,
market.name,
market.description,
metro.name.concat(" ").concat(local.name),
market.thumbnail,
favorite.id.isNotNull(),
favorite.member.id.count()))
favoriteMember.id.isNotNull(),
favorite.id.count()))
.from(market)
// 모든 사용자 기준 찜 데이터 JOIN
.leftJoin(favorite).on(market.eq(favorite.market)
.and(favorite.member.id.eq(memberId)
.and(favorite.isDeleted.eq(false))))
.and(favorite.isDeleted.eq(false)))
// 특정 사용자의 찜 여부 확인을 위한 JOIN
.leftJoin(favoriteMember).on(market.eq(favoriteMember.market)
.and(favoriteMember.member.id.eq(memberId)
.and(favoriteMember.isDeleted.eq(false))))
.innerJoin(local).on(market.local.eq(local))
.innerJoin(metro).on(local.metro.eq(metro))
.where(loeFavoriteCount(count)) // 삭제되지 않은 찜만 필터링
.groupBy(market.id, market.name, market.description, metro.name, local.name, market.thumbnail,favorite.id)
.orderBy(favorite.member.id.count().desc(),market.id.desc()) // 찜 수가 많은 순으로 정렬
.groupBy(market.id, market.name, market.description, metro.name, local.name, market.thumbnail,favoriteMember.id)
.having(loeFavoriteCountAndLtMarketId(count,marketId))
.orderBy(favorite.id.count().desc(),market.id.desc()) // 찜 수가 많은 순으로 정렬
.limit(size+1) // 반환할 리스트 크기 제한
.fetch(); // 결과 반환
}
Expand All @@ -157,7 +167,8 @@ public List<TopFavoriteMarketRes> findTopFavoriteMarkets(Integer size) {
market.name,
market.thumbnail))
.from(market)
.leftJoin(favorite.market,market).on(favorite.isDeleted.eq(false))
.leftJoin(favorite).on(market.eq(favorite.market)
.and(favorite.isDeleted.eq(false)))
.groupBy(market.id, market.name, market.thumbnail)
.orderBy(favorite.id.count().desc()) // 찜 수가 많은 순으로 정렬
.fetch();
Expand Down Expand Up @@ -278,10 +289,16 @@ private BooleanBuilder ltMarketId(Long marketId){
}

// loe= less or equal = <=(~보다 작거나 같은)
private BooleanBuilder loeFavoriteCount(Long count){
private BooleanBuilder loeFavoriteCountAndLtMarketId(Long count,Long marketId){
BooleanBuilder builder = new BooleanBuilder();
if (count != null) {
builder.and(favorite.member.id.count().loe(count));
if (count != null && marketId!=null) {
builder.and(favorite.id.count().loe(count));

// A or B 여서 둘중 하나의 조건만 만족하면 true
// count보다 작으면. A가 true이기 때문에 B는 실행되지않음
// A가 false이면 market.id 필터링한 행들만 true
// 따라서 favorite.id.count()가 count와 같을 때만 market.id 필터링
builder.and(favorite.id.count().lt(count).or(market.id.lt(marketId)));
}
return builder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface MarketService {

MarketPageRes<MyFavoriteMarketRes> getMyFavoriteMarketPage(Long memberId, LocalDateTime lastModifiedAt, Integer size);

MarketPageRes<FavoriteMarketRes> getFavoriteMarketPage(Long memberId, Long count, Integer size);
MarketPageRes<FavoriteMarketRes> getFavoriteMarketPage(Long memberId, Long marketId, Long count, Integer size);
List<TopFavoriteMarketRes> getTopFavoriteMarkets(Integer size);

List<TopLatestCouponRes> getTopLatestCoupons(Integer size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public MarketPageRes<MyFavoriteMarketRes> getMyFavoriteMarketPage(Long memberId,

// 찜 수가 가장 많은 매장 더보기 조회
@Override
public MarketPageRes<FavoriteMarketRes> getFavoriteMarketPage(Long memberId, Long count, Integer size) {
List<FavoriteMarketRes> favoriteMarketResList = marketRepository.findFavoriteMarketList(memberId, count, size);
public MarketPageRes<FavoriteMarketRes> getFavoriteMarketPage(Long memberId, Long marketId, Long count, Integer size) {
List<FavoriteMarketRes> favoriteMarketResList = marketRepository.findFavoriteMarketList(memberId, marketId, count, size);

return checkNextPageAndReturn(favoriteMarketResList, size);
}
Expand Down

0 comments on commit 08680d7

Please sign in to comment.