Skip to content

Commit

Permalink
Merge pull request #22 from KUIT-Couphone/feature/brand
Browse files Browse the repository at this point in the history
fix: 브랜드 조회 API
  • Loading branch information
versatile0010 authored Aug 6, 2023
2 parents 014abfe + 660cca5 commit 7b5a292
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public interface CouponItemRepository extends JpaRepository<CouponItem, Long> {

@Query("select c from CouponItem c " +
"where c.brand.id = :bid and c.member.id = :mid " +
"and c.status <> 'EXPIRED'")
"and c.status <> 'EXPIRED' " +
"ORDER BY c.stampCount DESC, c.createdDate ASC")
CouponItem findByMemberIdAndBrandIdAndStatusNotExpired(@Param("mid") Long mid, @Param("bid") Long bid);

List<CouponItem> findAllByMemberIdAndBrandId(Long mid, Long bid);
Expand Down
45 changes: 19 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,21 @@ 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;
}
}
6 changes: 3 additions & 3 deletions src/main/resources/import.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ INSERT INTO brand (brand_id, category_id, created_date, modified_date, brand_ima
INSERT INTO brand (brand_id, category_id, created_date, modified_date, brand_image_url, name, reward_description, status) VALUES (3, 1, '2023-07-30 10:00:00', '2023-07-30 10:05:00', 'https://example.com/images/brand2.jpg', '컴포즈 커피', '아이스 아메리카노 한 잔 무료 증정', 'ACTIVE');

-- 샘플 데이터 생성: store 테이블
INSERT INTO store (store_id, latitude, longitude, brand_id, created_date, modified_date, name, status, address) VALUES (1, 37.12345, -122.67890, 1, '2023-07-31 13:00:00', '2023-07-31 13:00:00', '메가커피 건대입구점', 'ACTIVE', '서울특별시 건대입구로 1111');
INSERT INTO store (store_id, latitude, longitude, brand_id, created_date, modified_date, name, status, address) VALUES (2, 37.12345, -110.67890, 1, '2023-07-31 13:00:00', '2023-07-31 13:00:00', '메가커피 세종대입구점', 'ACTIVE', '서울특별시 세종대입구로 2222');
INSERT INTO store (store_id, latitude, longitude, brand_id, created_date, modified_date, name, status, address) VALUES (3, 37.12345, -100.67890, 2, '2023-07-31 13:00:00', '2023-07-31 13:00:00', '컴포즈커피 건대입구점', 'ACTIVE', '서울특별시 건대입구로 1111');
INSERT INTO store (store_id, latitude, longitude, brand_id, created_date, modified_date, address, name, status) VALUES (1, 37.12345, -122.67890, 1, '2023-07-31 13:00:00', '2023-07-31 13:00:00', '서울특별시 메가커피 건대입구점 건대입구로 11111', '메가커피', 'ACTIVE');
INSERT INTO store (store_id, latitude, longitude, brand_id, created_date, modified_date, address, name, status) VALUES (2, 37.12345, -110.67890, 1, '2023-07-31 13:00:00', '2023-07-31 13:00:00', '서울특별시 메가커피 세종대입구점 세종대입구로 22222', '메가커피', 'ACTIVE');
INSERT INTO store (store_id, latitude, longitude, brand_id, created_date, modified_date, address, name, status) VALUES (3, 37.12345, -100.67890, 2, '2023-07-31 13:00:00', '2023-07-31 13:00:00', '서울특별시 컴포즈커피 건대입구점 건대입구로 11111', '컴포즈커피', 'ACTIVE');

-- 샘플 데이터 생성: coupon_item 테이블
INSERT INTO coupon_item (coupon_item_id, stamp_count, brand_id, created_date, member_id, modified_date, status) VALUES (1, 5, 1, '2023-07-31 12:45:00', 1, '2023-07-31 12:45:00', 'INACTIVE');
Expand Down

0 comments on commit 7b5a292

Please sign in to comment.