Skip to content

Commit

Permalink
refactor: be 카테고리 검색 Specification을 통해 검색조건 확장 쉽도록 바꿈
Browse files Browse the repository at this point in the history
Former-commit-id: b3433716dff68b6e885605011760fd8fd3a727d6
  • Loading branch information
SeonA1223 committed Oct 14, 2021
1 parent 3ad41b5 commit 1d217f8
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import java.util.List;

public interface HobbyClassRepository extends JpaRepository<HobbyClass, Long> {
public interface HobbyClassRepository extends JpaRepository<HobbyClass, Long>, JpaSpecificationExecutor<HobbyClass> {
//추천 카테고리 - 온라인
List<HobbyClass> findTop5BySmallCategoryIdAndSidoIdOrderByLikeCntDesc(Integer smallcategory_id, Integer sido_id);

Expand All @@ -19,7 +20,7 @@ public interface HobbyClassRepository extends JpaRepository<HobbyClass, Long> {
//인기 카테고리 상위 10개 가져오기
List<HobbyClass> findTop10ByBigCategoryIdOrderByLikeCntDesc(Integer bigCategoryId);

//카테고리별 검색하기
//카테고리별 검색하기 - 18개의 쿼리

//대분류로만 검색
Page<HobbyClass> findByBigCategoryIdOrderByLikeCntDesc(Integer bigCategoryId, Pageable pageable);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.ssafy.buki.domain.hobbyclass;

import com.ssafy.buki.exception.BusinessException;
import com.ssafy.buki.exception.ErrorCode;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

public class HobbyClassSpec {
//대분류
//소분류
//minPrice~ maxPrice
//minPrice
//시군구
//시도

public static Specification<HobbyClass> BigCategory(Integer bigCategoryId) {
return new Specification<HobbyClass>() {
@Override
public Predicate toPredicate(Root<HobbyClass> root,
CriteriaQuery<?> query,
CriteriaBuilder builder) {
if (bigCategoryId == null) throw new BusinessException(ErrorCode.NOT_RIGHT_DATA);

return builder.equal(root.get("bigCategory"), bigCategoryId);
}
};
}

public static Specification<HobbyClass> SmallCategory(Integer smallCategoryId) {
return new Specification<HobbyClass>() {
@Override
public Predicate toPredicate(Root<HobbyClass> root,
CriteriaQuery<?> query,
CriteriaBuilder builder) {
if (smallCategoryId == null) return null;

return builder.equal(root.get("smallCategory"), smallCategoryId);
}
};
}

public static Specification<HobbyClass> Sigungu(Integer sigunguId) {
return new Specification<HobbyClass>() {
@Override
public Predicate toPredicate(Root<HobbyClass> root,
CriteriaQuery<?> query,
CriteriaBuilder builder) {
if (sigunguId == null) return null;
Integer sidoId = null;
if (sigunguId.equals(1)) {
sidoId = 1;
}
if (sigunguId.equals(13)) {
sidoId = 2;
}
if (sidoId != null) {
return builder.equal(root.get("sido"), sidoId);
}
return builder.equal(root.get("sigunguId"), sigunguId);
}
};
}

public static Specification<HobbyClass> Price(Integer minPrice, Integer maxPrice) {
return new Specification<HobbyClass>() {
@Override
public Predicate toPredicate(Root<HobbyClass> root,
CriteriaQuery<?> query,
CriteriaBuilder builder) {
if (minPrice == null && maxPrice == null) return null;
if (maxPrice == null) throw new BusinessException(ErrorCode.NOT_RIGHT_DATA);
Integer minTempPrice = minPrice == null ? 0 : minPrice;

return builder.between(root.get("price"), minTempPrice, maxPrice);
}
};
}

}
227 changes: 125 additions & 102 deletions Backend/src/main/java/com/ssafy/buki/service/HobbyClassService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
Expand All @@ -28,6 +29,8 @@

import java.util.*;

import static org.springframework.data.jpa.domain.Specification.*;
import static com.ssafy.buki.domain.hobbyclass.HobbyClassSpec.*;
import static com.ssafy.buki.exception.ErrorCode.NOT_RIGHT_DATA;
import static com.ssafy.buki.exception.ErrorCode.NO_CATEGORY_DATA;

Expand Down Expand Up @@ -99,119 +102,139 @@ public List<List<HobbyClassResDto>> getPopularClass(User user) {
}


// 5. Get - 카테고리로 검색한 클래스 가져오기
// 5. Get - 카테고리로 검색한 클래스 가져오기, Specification을 사용하는 경우
public List<HobbyClassResDto> getClassSearchByCategory(User user, int classId, HobbyClassReqDto hobbyClassReqDto) {
Integer bigCategoryId = hobbyClassReqDto.getBigcategoryId();
Integer smallCategoryId = hobbyClassReqDto.getSmallcategoryId();
Integer sigunguId = hobbyClassReqDto.getSigunguId();
Integer minPrice = hobbyClassReqDto.getMinPrice();
Integer maxPrice = hobbyClassReqDto.getMaxPrice();
Integer sidoId = null;
boolean all = false;

if (bigCategoryId == null) throw new BusinessException(NOT_RIGHT_DATA);
if (sigunguId != null) {
if (sigunguId.equals(1)) {
all = true;
sidoId = 1;
}
if (sigunguId.equals(13)) {
all = true;
sidoId = 2;
}
}

PageRequest pageRequest = PageRequest.of(classId, 10, Sort.unsorted());

Page<HobbyClass> hobbyClassList = null;

if (smallCategoryId == null) { //smallCategory로 검색 안할 경우
if (sigunguId != null) {
if (maxPrice != null) {
if (minPrice == null) minPrice = 0;

if (all) {
hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSidoIdAndPriceBetweenOrderByLikeCntDesc(bigCategoryId, sidoId, minPrice, maxPrice, pageRequest);
} else {
hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSigunguIdAndPriceBetweenOrderByLikeCntDesc(bigCategoryId, sigunguId, minPrice, maxPrice, pageRequest);
}
} else {
if (minPrice == null) {
if (all) {
hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSidoIdOrderByLikeCntDesc(bigCategoryId, sidoId, pageRequest);
} else {
hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSigunguIdOrderByLikeCntDesc(bigCategoryId, sigunguId, pageRequest);
}
} else {
if (all) {
hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSidoIdAndPriceGreaterThanEqualOrderByLikeCntDesc(bigCategoryId, sidoId, minPrice, pageRequest);
} else {
hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSigunguIdAndPriceGreaterThanEqualOrderByLikeCntDesc(bigCategoryId, sigunguId, minPrice, pageRequest);
}

}
}

} else if (sigunguId == null) {
if (maxPrice == null) {
if (minPrice == null) {
hobbyClassList = hobbyClassRepository.findByBigCategoryIdOrderByLikeCntDesc(bigCategoryId, pageRequest);
} else {
hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndPriceGreaterThanEqualOrderByLikeCntDesc(bigCategoryId, minPrice, pageRequest);
}
} else {
if (minPrice == null) minPrice = 0;
hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndPriceBetweenOrderByLikeCntDesc(bigCategoryId, minPrice, maxPrice, pageRequest);
}
}

} else { // smallCategory로 검색할 경우
if (sigunguId != null) {
if (maxPrice != null) {
if (minPrice == null) minPrice = 0;

if (all) {
hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSmallCategoryIdAndSidoIdAndPriceBetweenOrderByLikeCntDesc(bigCategoryId, smallCategoryId, sidoId, minPrice, maxPrice, pageRequest);
} else {
hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSmallCategoryIdAndSigunguIdAndPriceBetweenOrderByLikeCntDesc(bigCategoryId, smallCategoryId, sigunguId, minPrice, maxPrice, pageRequest);
}
} else {
if (minPrice == null) {
if (all) {
hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSmallCategoryIdAndSidoIdOrderByLikeCntDesc(bigCategoryId, smallCategoryId, sidoId, pageRequest);
} else {
hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSmallCategoryIdAndSigunguIdOrderByLikeCntDesc(bigCategoryId, smallCategoryId, sigunguId, pageRequest);
}
} else {
if (all) {
hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSmallCategoryIdAndSidoIdAndPriceGreaterThanEqualOrderByLikeCntDesc(bigCategoryId, smallCategoryId, sidoId, minPrice, pageRequest);
} else {
hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSmallCategoryIdAndSigunguIdAndPriceGreaterThanEqualOrderByLikeCntDesc(bigCategoryId, smallCategoryId, sigunguId, minPrice, pageRequest);
}

}
}
} else {
if (maxPrice == null) {
if (minPrice == null) {
hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSmallCategoryIdOrderByLikeCntDesc(bigCategoryId, smallCategoryId, pageRequest);
} else {
hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSmallCategoryIdAndPriceGreaterThanEqualOrderByLikeCntDesc(bigCategoryId, smallCategoryId, minPrice, pageRequest);
}
} else {
if (minPrice == null) minPrice = 0;
hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSmallCategoryIdAndPriceBetweenOrderByLikeCntDesc(bigCategoryId, smallCategoryId, minPrice, maxPrice, pageRequest);
}
}

}

// 마지막 페이지인지에 대한 값을 주어야하나요?
// if(hobbyClassList.hasNext()) throw new BusinessException(IS_LAST_PAGE);
PageRequest pageRequest = PageRequest.of(classId, 10, Sort.by(Sort.Direction.DESC, "LikeCnt"));

Page<HobbyClass> hobbyClassList = hobbyClassRepository.
findAll(
where(BigCategory(bigCategoryId))
.and(SmallCategory(smallCategoryId))
.and(Sigungu(sigunguId))
.and(Price(minPrice, maxPrice))
, pageRequest);
if (user == null) return common.entityPageToDto(hobbyClassList);
return common.entityPageToDto(hobbyClassList, user);

}
// public List<HobbyClassResDto> getClassSearchByCategory(User user, int classId, HobbyClassReqDto hobbyClassReqDto) {
// Integer bigCategoryId = hobbyClassReqDto.getBigcategoryId();
// Integer smallCategoryId = hobbyClassReqDto.getSmallcategoryId();
// Integer sigunguId = hobbyClassReqDto.getSigunguId();
// Integer minPrice = hobbyClassReqDto.getMinPrice();
// Integer maxPrice = hobbyClassReqDto.getMaxPrice();
// Integer sidoId = null;
// boolean all = false;
//
// if (bigCategoryId == null) throw new BusinessException(NOT_RIGHT_DATA);
// if (sigunguId != null) {
// if (sigunguId.equals(1)) {
// all = true;
// sidoId = 1;
// }
// if (sigunguId.equals(13)) {
// all = true;
// sidoId = 2;
// }
// }
//
// PageRequest pageRequest = PageRequest.of(classId, 10, Sort.by(Sort.Direction.DESC, "LikeCnt"));
//
// Page<HobbyClass> hobbyClassList = null;
//
// if (smallCategoryId == null) { //smallCategory로 검색 안할 경우
// if (sigunguId != null) {
// if (maxPrice != null) {
// if (minPrice == null) minPrice = 0;
//
// if (all) {
// hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSidoIdAndPriceBetweenOrderByLikeCntDesc(bigCategoryId, sidoId, minPrice, maxPrice, pageRequest);
// } else {
// hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSigunguIdAndPriceBetweenOrderByLikeCntDesc(bigCategoryId, sigunguId, minPrice, maxPrice, pageRequest);
// }
// } else {
// if (minPrice == null) {
// if (all) {
// hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSidoIdOrderByLikeCntDesc(bigCategoryId, sidoId, pageRequest);
// } else {
// hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSigunguIdOrderByLikeCntDesc(bigCategoryId, sigunguId, pageRequest);
// }
// } else {
// if (all) {
// hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSidoIdAndPriceGreaterThanEqualOrderByLikeCntDesc(bigCategoryId, sidoId, minPrice, pageRequest);
// } else {
// hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSigunguIdAndPriceGreaterThanEqualOrderByLikeCntDesc(bigCategoryId, sigunguId, minPrice, pageRequest);
// }
//
// }
// }
//
// } else if (sigunguId == null) {
// if (maxPrice == null) {
// if (minPrice == null) {
// hobbyClassList = hobbyClassRepository.findByBigCategoryIdOrderByLikeCntDesc(bigCategoryId, pageRequest);
// } else {
// hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndPriceGreaterThanEqualOrderByLikeCntDesc(bigCategoryId, minPrice, pageRequest);
// }
// } else {
// if (minPrice == null) minPrice = 0;
// hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndPriceBetweenOrderByLikeCntDesc(bigCategoryId, minPrice, maxPrice, pageRequest);
// }
// }
//
// } else { // smallCategory로 검색할 경우
// if (sigunguId != null) {
// if (maxPrice != null) {
// if (minPrice == null) minPrice = 0;
//
// if (all) {
// hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSmallCategoryIdAndSidoIdAndPriceBetweenOrderByLikeCntDesc(bigCategoryId, smallCategoryId, sidoId, minPrice, maxPrice, pageRequest);
// } else {
// hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSmallCategoryIdAndSigunguIdAndPriceBetweenOrderByLikeCntDesc(bigCategoryId, smallCategoryId, sigunguId, minPrice, maxPrice, pageRequest);
// }
// } else {
// if (minPrice == null) {
// if (all) {
// hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSmallCategoryIdAndSidoIdOrderByLikeCntDesc(bigCategoryId, smallCategoryId, sidoId, pageRequest);
// } else {
// hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSmallCategoryIdAndSigunguIdOrderByLikeCntDesc(bigCategoryId, smallCategoryId, sigunguId, pageRequest);
// }
// } else {
// if (all) {
// hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSmallCategoryIdAndSidoIdAndPriceGreaterThanEqualOrderByLikeCntDesc(bigCategoryId, smallCategoryId, sidoId, minPrice, pageRequest);
// } else {
// hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSmallCategoryIdAndSigunguIdAndPriceGreaterThanEqualOrderByLikeCntDesc(bigCategoryId, smallCategoryId, sigunguId, minPrice, pageRequest);
// }
//
// }
// }
// } else {
// if (maxPrice == null) {
// if (minPrice == null) {
// hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSmallCategoryIdOrderByLikeCntDesc(bigCategoryId, smallCategoryId, pageRequest);
// } else {
// hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSmallCategoryIdAndPriceGreaterThanEqualOrderByLikeCntDesc(bigCategoryId, smallCategoryId, minPrice, pageRequest);
// }
// } else {
// if (minPrice == null) minPrice = 0;
// hobbyClassList = hobbyClassRepository.findByBigCategoryIdAndSmallCategoryIdAndPriceBetweenOrderByLikeCntDesc(bigCategoryId, smallCategoryId, minPrice, maxPrice, pageRequest);
// }
// }
//
// }
//
//// 마지막 페이지인지에 대한 값을 주어야하나요?
//// if(hobbyClassList.hasNext()) throw new BusinessException(IS_LAST_PAGE);

// if (user == null) return common.entityPageToDto(hobbyClassList);
// return common.entityPageToDto(hobbyClassList, user);
// }


// 6. Get - 키워드로 검색한 클래스 가져오기
Expand Down

0 comments on commit 1d217f8

Please sign in to comment.