Skip to content

Commit

Permalink
fix: 검색어로 브랜드 조회 시 'EXPIRED' 상태가 아닌 쿠폰을 반환
Browse files Browse the repository at this point in the history
  • Loading branch information
limsubinn committed Aug 5, 2023
1 parent a64e080 commit 4ba05a5
Showing 1 changed file with 20 additions and 26 deletions.
46 changes: 20 additions & 26 deletions src/main/java/com/example/couphoneserver/service/BrandService.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public PostBrandResponse saveBrand(PostBrandRequest request) {
}

public List<GetBrandResponse> findByCategoryId(Principal principal, Long categoryId) {
List<GetBrandResponse> brandList = new ArrayList<>();

// 멤버 ID
Long memberId = findMemberIdByPrincipal(principal);
Expand All @@ -68,22 +67,10 @@ public List<GetBrandResponse> findByCategoryId(Principal principal, Long categor
List<Brand> brands = brandRepository.findAllByCategoryId(categoryId)
.orElseThrow(() -> new BrandException(BRAND_NOT_FOUND));

for (Brand brand : brands) {
// 쿠폰 찾기
CouponItem couponItem = couponItemRepository.findByMemberIdAndBrandIdAndStatusNotExpired(memberId, brand.getId());

if (couponItem == null) { // 해당 브랜드에 쿠폰이 없을 경우
brandList.add(new GetBrandResponse(brand, 0));
} else { // 해당 브랜드에 쿠폰이 있을 경우
brandList.add(new GetBrandResponse(brand, couponItem.getStampCount()));
}
}

return brandList;
return getNotExpiredBrandList(memberId, brands);
}

public List<GetBrandResponse> findByNameContaining(Principal principal, String name) {
List<GetBrandResponse> brandList = new ArrayList<>();

// 멤버 ID
Long memberId = findMemberIdByPrincipal(principal);
Expand All @@ -95,18 +82,7 @@ public List<GetBrandResponse> findByNameContaining(Principal principal, String n
List<Brand> brands = brandRepository.findAllByNameContaining(name)
.orElseThrow(() -> new BrandException(BRAND_NOT_FOUND));

for (Brand brand : brands) {
// 쿠폰 찾기
CouponItem couponItem = couponItemRepository.findByMemberIdAndBrandIdAndStatus(memberId, brand.getId(), CouponItemStatus.ACTIVE);

if (couponItem == null) { // 해당 브랜드에 쿠폰이 없을 경우
brandList.add(new GetBrandResponse(brand, 0));
} else { // 해당 브랜드에 쿠폰이 있을 경우
brandList.add(new GetBrandResponse(brand, couponItem.getStampCount()));
}
}

return brandList;
return getNotExpiredBrandList(memberId, brands);
}

public GetBrandDetailResponse getBrandDetail(Long brandId, Principal principal) {
Expand Down Expand Up @@ -154,4 +130,22 @@ private Long findMemberIdByPrincipal(Principal principal) {
String email = principal.getName();
return memberService.findOneByEmail(email).getId();
}

private List<GetBrandResponse> getNotExpiredBrandList(Long memberId, List<Brand> brands) {
List<GetBrandResponse> brandList = new ArrayList<>();

for (Brand brand : brands) {
// 쿠폰 찾기

CouponItem couponItem = couponItemRepository.findByMemberIdAndBrandIdAndStatusNotExpired(memberId, brand.getId());

if (couponItem == null) { // 해당 브랜드에 쿠폰이 없을 경우
brandList.add(new GetBrandResponse(brand, 0));
} else { // 해당 브랜드에 쿠폰이 있을 경우
brandList.add(new GetBrandResponse(brand, couponItem.getStampCount()));
}
}

return brandList;
}
}

0 comments on commit 4ba05a5

Please sign in to comment.