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

[Refactor] 베스트 컨텐츠 조회 api 리팩토링 및 북마크 여부 제외 #135

Merged
merged 3 commits 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ out/
### log files ###
log/
*.log
*.gz
*.gz
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
import com.org.gunbbang.util.Security.SecurityUtil;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
Expand All @@ -23,14 +21,13 @@ public class BestContentController {
private final ReviewService reviewService;

@GetMapping("/bakeries")
@ResponseStatus(HttpStatus.OK)
public ApiResponse<List<BestBakeryListResponseDTO>> getBestBakeries() {
Long memberId = SecurityUtil.getLoginMemberId();
return ApiResponse.success(
SuccessType.GET_BEST_BAKERIES_SUCCESS, bakeryService.getBestBakeries());
SuccessType.GET_BEST_BAKERIES_SUCCESS, bakeryService.getBestBakeries(memberId));
}

@GetMapping("/reviews")
@ResponseStatus(HttpStatus.OK)
public ApiResponse<List<BestReviewListResponseDTO>> getBestReviews() {
Long memberId = SecurityUtil.getLoginMemberId();
return ApiResponse.success(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,5 @@
public class BakeryListResponseDTO extends BaseBakeryResponseDTO {
private long reviewCount;
private BreadTypeResponseDTO breadType;
private boolean isBookMarked;
private long bookMarkCount;

public boolean getIsBookMarked() {
return isBookMarked;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,5 @@
@SuperBuilder
public class BestBakeryListResponseDTO extends BaseBakeryResponseDTO {
private long reviewCount;
private boolean isBookMarked;
private long bookMarkCount;

public boolean getIsBookMarked() {
return isBookMarked;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,5 @@ public class BestReviewListResponseDTO extends BaseBakeryResponseDTO {
private String reviewText;
private String firstMaxRecommendKeyword;
private String secondMaxRecommendKeyword;
private boolean isBookMarked;
private Long bookMarkCount;

public boolean getIsBookMarked() {
return isBookMarked;
}
}
114 changes: 44 additions & 70 deletions api/src/main/java/com/org/gunbbang/service/BakeryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.org.gunbbang.entity.*;
import com.org.gunbbang.errorType.ErrorType;
import com.org.gunbbang.repository.*;
import com.org.gunbbang.util.Security.SecurityUtil;
import com.org.gunbbang.util.mapper.BakeryMapper;
import com.org.gunbbang.util.mapper.BreadTypeMapper;
import java.util.*;
Expand All @@ -25,7 +24,6 @@
@RequiredArgsConstructor
public class BakeryService {
private final CategoryRepository categoryRepository;
private final BakeryCategoryRepository bakeryCategoryRepository;
private final BookMarkRepository bookMarkRepository;
private final MemberRepository memberRepository;
private final BakeryRepository bakeryRepository;
Expand All @@ -38,7 +36,6 @@ public List<BakeryListResponseDTO> getBakeryList(
List<Category> categoryList = getCategoryList(isHard, isDessert, isBrunch);
List<BakeryListResponseDTO> responseDtoList = new ArrayList<>();
BreadTypeResponseDTO breadType;
boolean isBookMarked;
List<Bakery> bakeryList;

if (categoryList.isEmpty()) {
Expand All @@ -49,9 +46,7 @@ public List<BakeryListResponseDTO> getBakeryList(
bakeryList = bakeryRepository.findAll(sortOption);
for (Bakery bakery : bakeryList) {
breadType = getBreadType(bakery);
isBookMarked = isBookMarked(memberId, bakery.getBakeryId());
BakeryListResponseDTO bakeryListResponseDto =
getBakeryResponseDTO(bakery, isBookMarked, breadType);
BakeryListResponseDTO bakeryListResponseDto = getBakeryResponseDTO(bakery, breadType);
responseDtoList.add(bakeryListResponseDto);
}
return responseDtoList;
Expand All @@ -61,9 +56,7 @@ public List<BakeryListResponseDTO> getBakeryList(
bakeryList = bakeryRepository.findBakeriesByCategoryAndReview(categoryList);
for (Bakery bakery : bakeryList) {
breadType = getBreadType(bakery);
isBookMarked = isBookMarked(memberId, bakery.getBakeryId());
BakeryListResponseDTO bakeryListResponseDto =
getBakeryResponseDTO(bakery, isBookMarked, breadType);
BakeryListResponseDTO bakeryListResponseDto = getBakeryResponseDTO(bakery, breadType);
responseDtoList.add(bakeryListResponseDto);
}
return responseDtoList;
Expand All @@ -72,9 +65,7 @@ public List<BakeryListResponseDTO> getBakeryList(
bakeryList = bakeryRepository.findBakeriesByCategory(categoryList);
for (Bakery bakery : bakeryList) {
breadType = getBreadType(bakery);
isBookMarked = isBookMarked(memberId, bakery.getBakeryId());
BakeryListResponseDTO bakeryListResponseDto =
getBakeryResponseDTO(bakery, isBookMarked, breadType);
BakeryListResponseDTO bakeryListResponseDto = getBakeryResponseDTO(bakery, breadType);
responseDtoList.add(bakeryListResponseDto);
}
return responseDtoList;
Expand Down Expand Up @@ -129,81 +120,67 @@ public BakeryDetailResponseDTO getBakeryDetail(Long memberId, Long bakeryId) {
.build();
}

public List<BestBakeryListResponseDTO> getBestBakeries() {
Long memberId = SecurityUtil.getLoginMemberId();
public List<BestBakeryListResponseDTO> getBestBakeries(Long memberId) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best 건빵집 조회가 확실히 복잡하긴하네유... 10개가 안될 경우 계속 계속 채우는 느낌이라 2차 릴리즈 때는 확실히 알고리즘 짜는게 좋아보입니다 랜덤으로 던져주는 것도 한계가 있어보여서...!!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

동의합니더,,

List<Long> alreadyFoundBakeryIds = new ArrayList<>();
alreadyFoundBakeryIds.add(Long.MAX_VALUE);
System.out.println("alreadyFoundBakeryIds 값 확인: " + alreadyFoundBakeryIds);
alreadyFoundBakeryIds.add(-1L);

Member foundMember =
memberRepository
.findById(memberId)
.orElseThrow(() -> new NotFoundException(ErrorType.NOT_FOUND_USER_EXCEPTION));

PageRequest bestPageRequest = PageRequest.of(0, maxBestBakeryCount);
List<Bakery> bestBakeries =
bakeryRepository.findBestBakeries(
foundMember.getBreadType().getBreadTypeId(),
foundMember.getMainPurpose(),
bestPageRequest // TODO: 일케하는게맞냐????
);
List<Bakery> bestBakeries = getBestBakeries(foundMember);

if (bestBakeries.size() == maxBestBakeryCount) {
log.info("베스트 베이커리 10개 조회 완료. 추가 조회 쿼리 없이 바로 반환");
return getResponseBakeries(foundMember, bestBakeries);
return BakeryMapper.INSTANCE.toBestBakeryListResponseDTO(bestBakeries);
}

System.out.println("alreadyFoundBakeryIds 값 확인: " + alreadyFoundBakeryIds);
System.out.println("alreadyFoundBakeryIds 엠티 여부: " + alreadyFoundBakeryIds.isEmpty());

alreadyFoundBakeryIds.addAll(
bestBakeries.stream().map(Bakery::getBakeryId).collect(Collectors.toList()));

log.info("빵유형 일치 베이커리 조회 시작. 현재까지 조회된 베이커리 수: " + bestBakeries.size());
bestPageRequest = PageRequest.of(0, maxBestBakeryCount - bestBakeries.size());
bestBakeries.addAll(
bakeryRepository.findRestBakeriesByBreadTypeId(
foundMember.getBreadType(), alreadyFoundBakeryIds, bestPageRequest));
setAlreadyFoundBakeryIds(alreadyFoundBakeryIds, bestBakeries);
getRestBakeries(alreadyFoundBakeryIds, foundMember.getBreadType(), bestBakeries);

if (bestBakeries.size() == maxBestBakeryCount) {
log.info("빵유형 10개 조회 완료. 추가 조회 쿼리 없이 바로 반환");
return getResponseBakeries(foundMember, bestBakeries);
return BakeryMapper.INSTANCE.toBestBakeryListResponseDTO(bestBakeries);
}

setAlreadyFoundBakeryIds(alreadyFoundBakeryIds, bestBakeries);
getRandomBakeries(alreadyFoundBakeryIds, bestBakeries);

return BakeryMapper.INSTANCE.toBestBakeryListResponseDTO(bestBakeries);
}

private void setAlreadyFoundBakeryIds(
List<Long> alreadyFoundBakeryIds, List<Bakery> bestBakeries) {
alreadyFoundBakeryIds.addAll(
bestBakeries.stream().map(Bakery::getBakeryId).collect(Collectors.toList()));
}

bestPageRequest = PageRequest.of(0, maxBestBakeryCount - bestBakeries.size());
log.info("찐 나머지만 고르는 베이커리. 현재까지 조회된 베이커리 수: " + bestBakeries.size());
private void getRandomBakeries(List<Long> alreadyFoundBakeryIds, List<Bakery> bestBakeries) {
PageRequest bestPageRequest = PageRequest.of(0, maxBestBakeryCount - bestBakeries.size());
log.info("나머지만 랜덤으로 고르는 베이커리. 현재까지 조회된 베이커리 수: " + bestBakeries.size());
bestBakeries.addAll(
bakeryRepository.findRestBakeriesRandomly(alreadyFoundBakeryIds, bestPageRequest));
}

return getResponseBakeries(foundMember, bestBakeries);
private void getRestBakeries(
List<Long> alreadyFoundBakeryIds, BreadType breadType, List<Bakery> bestBakeries) {
log.info("빵유형 일치 베이커리 조회 시작. 현재까지 조회된 베이커리 수: " + bestBakeries.size());
PageRequest bestPageRequest = PageRequest.of(0, maxBestBakeryCount - bestBakeries.size());
bestBakeries.addAll(
bakeryRepository.findRestBakeriesByBreadTypeId(
breadType, alreadyFoundBakeryIds, bestPageRequest));
}

// TODO: 이거 DTO 안에 static 메서드로 못빼나??
private List<BestBakeryListResponseDTO> getResponseBakeries(
Member member, List<Bakery> bakeries) {
List<BestBakeryListResponseDTO> responseDtoList = new ArrayList<>();
for (Bakery bestBakery : bakeries) {
boolean isBookMarked = isBookMarked(member.getMemberId(), bestBakery.getBakeryId());
BestBakeryListResponseDTO response =
BestBakeryListResponseDTO.builder()
.bakeryId(bestBakery.getBakeryId())
.bakeryName(bestBakery.getBakeryName())
.bakeryPicture(bestBakery.getBakeryPicture())
.isHACCP(bestBakery.getIsHACCP())
.isVegan(bestBakery.getIsVegan())
.isNonGMO(bestBakery.getIsNonGMO())
.firstNearStation(bestBakery.getFirstNearStation())
.secondNearStation(bestBakery.getSecondNearStation())
.isBookMarked(isBookMarked)
.bookMarkCount(bestBakery.getBookMarkCount())
.reviewCount(bestBakery.getReviewCount())
.build();
responseDtoList.add(response);
}
return responseDtoList;
private List<Bakery> getBestBakeries(Member foundMember) {
log.info("베스트 건빵집 조회 시작.");
PageRequest bestPageRequest = PageRequest.of(0, maxBestBakeryCount);
List<Bakery> bestBakeries =
bakeryRepository.findBestBakeries(
foundMember.getBreadType().getBreadTypeId(),
foundMember.getMainPurpose(),
bestPageRequest);
return bestBakeries;
}

public BakerySearchResponseDTO getBakeriesByName(String bakeryName, Long memberId) {
Expand All @@ -213,22 +190,20 @@ public BakerySearchResponseDTO getBakeriesByName(String bakeryName, Long memberI

List<Bakery> foundBakeries = bakeryRepository.findBakeryByBakeryName(bakeryName);
List<BakeryListResponseDTO> bakeryListResponseDTOs =
getBakeryListResponseDTOList(memberId, foundBakeries);
getBakeryListResponseDTOList(foundBakeries);

return BakeryMapper.INSTANCE.toBakerySearchResponseDTO(
bakeryListResponseDTOs.size(), bakeryListResponseDTOs);
}

private List<BakeryListResponseDTO> getBakeryListResponseDTOList(
Long memberId, List<Bakery> foundBakeries) {
private List<BakeryListResponseDTO> getBakeryListResponseDTOList(List<Bakery> foundBakeries) {
List<BakeryListResponseDTO> bakeryListResponseDTOs = new ArrayList<>();

for (Bakery foundBakery : foundBakeries) {
boolean isBookMarked = isBookMarked(memberId, foundBakery.getBakeryId());
BreadTypeResponseDTO breadType =
BreadTypeMapper.INSTANCE.toBreadTypeResponseDTO(foundBakery.getBreadType());
BakeryListResponseDTO bakeryListResponseDTO =
BakeryMapper.INSTANCE.toBakeryListResponseDTO(foundBakery, isBookMarked, breadType);
BakeryMapper.INSTANCE.toBakeryListResponseDTO(foundBakery, breadType);
bakeryListResponseDTOs.add(bakeryListResponseDTO);
}

Expand Down Expand Up @@ -259,7 +234,7 @@ public BakerySearchResponseDTOV2 getBakeriesByNameV2(String bakeryName, Long mem

public List<BakeryListResponseDTO> getBookMarkedBakeries(Long memberId) {
List<Bakery> bookMarkedBakeries = bakeryRepository.findBookMarkedBakeries(memberId);
return getBakeryListResponseDTOList(memberId, bookMarkedBakeries);
return getBakeryListResponseDTOList(bookMarkedBakeries);
}

// TODO: 공통적으로 사용되는 메서드라 다른 곳으로는 못뺄지
Expand All @@ -282,7 +257,7 @@ private BreadTypeResponseDTO getBreadType(Bakery bakery) {
}

private BakeryListResponseDTO getBakeryResponseDTO(
Bakery bakery, boolean isBookMarked, BreadTypeResponseDTO breadType) {
Bakery bakery, BreadTypeResponseDTO breadType) {
return BakeryListResponseDTO.builder()
.bakeryId(bakery.getBakeryId())
.bakeryName(bakery.getBakeryName())
Expand All @@ -292,7 +267,6 @@ private BakeryListResponseDTO getBakeryResponseDTO(
.isNonGMO(bakery.getIsNonGMO())
.firstNearStation(bakery.getFirstNearStation())
.secondNearStation(bakery.getSecondNearStation())
.isBookMarked(isBookMarked)
.bookMarkCount(bakery.getBookMarkCount())
.reviewCount(bakery.getReviewCount())
.breadType(breadType)
Expand Down
Loading