Skip to content

Commit

Permalink
[Refactor] 리팩토링에 의해 발생한 api 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
seonghooni committed Nov 12, 2024
1 parent e9bf182 commit d7390e5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ResponseEntity<BookDto> getBookDetails(@RequestParam(name ="id") Long id)
@Operation(summary = "중분류에 해당하는 book 리스트 요청", description = "중분류에 해당하는 book들을 페이징해서 " +
"요청 예) /api/book/list/middle?name=한국소설&page=0&size=5")
@GetMapping(value = "/list/middle")
public ResponseEntity<Page<BookSearchResponseDto>> getBookListByMiddleCategory(@RequestParam(name = "categoryId") Long categoryId,
public ResponseEntity<Page<BookSearchResponseDto>> getBookListByMiddleCategory(@RequestParam(name = "middleCategoryId") Long categoryId,
@RequestParam(name = "page") int page, @RequestParam(name = "size") int size) {
PageRequest pageRequest = PageRequest.of(page, size, Sort.by("view_count").descending());
Page<BookSearchResponseDto> result = bookService.getBookPageByMiddleCategory(categoryId, pageRequest);
Expand All @@ -56,7 +56,7 @@ public ResponseEntity<Page<BookSearchResponseDto>> getBookListByMiddleCategory(@

@Operation(summary = "대분류에 속하는 중분류들에 대한 각각의 book 리스트 요청", description = "Map<중분류 이름, 책리스트>로 반환")
@GetMapping(value = "/list/big")
public ResponseEntity<Object> getListMapByBigCategory(@RequestParam(name = "id") Long bigCategoryId){
public ResponseEntity<Object> getListMapByBigCategory(@RequestParam(name = "bigCategoryId") Long bigCategoryId){

List<Map<String, Object>> result = bookService.getBookListByBigCategoryChildren(bigCategoryId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class CategoryController {

@Operation(summary = "대분류 출력", description = "대분류에 해당하는 카테고리 불러오기, Map<대분류, List<중분류>> 형식으로 반환함")
@GetMapping(value = "/big")
public ResponseEntity<Map<String, List<String>>> getCategoryGroupByBig(){
Map<String, List<String>> result = categoryService.getAllCategoryGrouped();
public ResponseEntity<List<Map<String, Object>>> getCategoryGroupByBig(){
List<Map<String, Object>> result = categoryService.getAllCategoryGrouped();
return ResponseEntity.ok(result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public interface CategoryRepository extends JpaRepository<Categories, Long> {
/**
* 대분류(level=1)들을 조회하기
* */
@Query("select c from Categories c where c.level = 1")
@Query("select c from Categories c where c.level = 1 order by c.id")
List<Categories> findBigCategory();

@Query("select c from Categories c join fetch c.parentCategory pc where c.level = 2 order by pc.id")
@Query("select c from Categories c join fetch c.parentCategory pc where c.level = 2 order by c.id")
List<Categories> findChildCategories();

@Query("select c from Categories c where c.name in :categoryList")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,39 @@ public List<CategoryDto> getChildCategory(Long bigCategoryId) {
return categoryDtoList;
}

public Map<String, List<String>> getAllCategoryGrouped() {
List<Categories> allCategories = categoryRepository.findChildCategories();

Map<String, List<String>> map = new LinkedHashMap<>();

for (Categories category : allCategories) {
String key = category.getParentCategory().getName();
String value = category.getName();
// 키가 이미 존재하는지 확인
if (map.containsKey(key)) {
// 키가 이미 존재하면 기존 리스트에 추가
map.get(key).add(value);
} else {
// 키가 존재하지 않으면 새로운 리스트를 생성하고 추가
List<String> newList = new ArrayList<>();
newList.add(value);
map.put(key, newList);
}
public List<Map<String, Object>> getAllCategoryGrouped() {

List<Map<String, Object>> result = new ArrayList<>(10);

List<Categories> bigCategoryList = categoryRepository.findBigCategory();
List<Categories> childCategoryList = categoryRepository.findChildCategories();

List<List<Map<String, Object>>> items = new ArrayList<>(10);
for (int i = 0; i < 10; i++) {
items.add(new ArrayList<>()); // 각 인덱스에 빈 리스트 추가
}

for (Categories category : childCategoryList) {
Long parentId = category.getParentCategory().getId();
Long id = category.getId();
String name = category.getName();

Map<String, Object> item = new LinkedHashMap<>();
item.put("id", id);
item.put("name", name);

int index = parentId.intValue() - 1;
items.get(index).add(item);
}

for (Categories categories : bigCategoryList) {
Map<String, Object> big = new LinkedHashMap<>();
big.put("id", categories.getId());
big.put("name", categories.getName());
big.put("items", items.get(categories.getId().intValue() - 1));
result.add(big);
}

return map;
return result;
}
}

0 comments on commit d7390e5

Please sign in to comment.