Skip to content

Commit

Permalink
Merge pull request #174 from nhnacademy-be4-ckin/feature/category-cache
Browse files Browse the repository at this point in the history
[FEATURE] 카테고리 캐싱기능 추가
  • Loading branch information
nayoseb authored Mar 25, 2024
2 parents 2cf2493 + 827e8f5 commit e624919
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.springframework.web.bind.annotation.RestController;
import store.ckin.api.category.dto.request.CategoryCreateRequestDto;
import store.ckin.api.category.dto.request.CategoryUpdateRequestDto;
import store.ckin.api.category.dto.response.CategoryCacheResponseDto;
import store.ckin.api.category.dto.response.CategoryResponseDto;
import store.ckin.api.category.service.CategoryService;

Expand Down Expand Up @@ -71,6 +72,12 @@ public ResponseEntity<List<CategoryResponseDto>> getSubcategories(@PathVariable
return ResponseEntity.ok(subcategories);
}

@GetMapping("/redis")
public ResponseEntity<List<CategoryCacheResponseDto>> getAllCategories() {
List<CategoryCacheResponseDto> categories = categoryService.gerAllCategories();
return ResponseEntity.ok(categories);
}

/**
* 지정된 ID의 카테고리를 업데이트합니다.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package store.ckin.api.category.dto.response;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
* CategoryCacheResponseDto.
*
* @author 나국로
* @version 2024. 03. 24.
*/
@Getter
@NoArgsConstructor
public class CategoryCacheResponseDto {


private Long categoryId;

private Long parentCategoryId;

private String categoryName;

private Integer categoryPriority;

@Builder
public CategoryCacheResponseDto(Long categoryId, Long parentCategoryId, String categoryName,
Integer categoryPriority) {
this.categoryId = categoryId;
this.parentCategoryId = parentCategoryId;
this.categoryName = categoryName;
this.categoryPriority = categoryPriority;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import store.ckin.api.category.dto.request.CategoryCreateRequestDto;
import store.ckin.api.category.dto.request.CategoryUpdateRequestDto;
import store.ckin.api.category.dto.response.CategoryCacheResponseDto;
import store.ckin.api.category.dto.response.CategoryResponseDto;

/**
Expand Down Expand Up @@ -69,4 +70,6 @@ public interface CategoryService {
* @return 부모 카테고리 아이디 목록
*/
List<Long> getParentIds(List<Long> categoryIds);

List<CategoryCacheResponseDto> gerAllCategories();
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package store.ckin.api.category.service.impl;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import store.ckin.api.book.repository.BookRepository;
import store.ckin.api.category.dto.request.CategoryCreateRequestDto;
import store.ckin.api.category.dto.request.CategoryUpdateRequestDto;
import store.ckin.api.category.dto.response.CategoryCacheResponseDto;
import store.ckin.api.category.dto.response.CategoryResponseDto;
import store.ckin.api.category.entity.Category;
import store.ckin.api.category.exception.CategoryNotFoundException;
Expand All @@ -26,7 +27,6 @@
public class CategoryServiceImpl implements CategoryService {

private final CategoryRepository categoryRepository;
private final BookRepository bookRepository;
private static final int DEFAULT_CATEGORY_PRIORITY = 1;

/**
Expand Down Expand Up @@ -131,6 +131,20 @@ public List<Long> getParentIds(List<Long> bookIds) {
return categoryRepository.getParentIds(bookIds);
}

@Override
public List<CategoryCacheResponseDto> gerAllCategories() {
List<Category> subcategories = categoryRepository.findAll();
return subcategories.stream()
.map(category -> CategoryCacheResponseDto.builder()
.categoryId(category.getCategoryId())
.parentCategoryId(Optional.ofNullable(category.getParentCategory())
.map(Category::getCategoryId)
.orElse(null))
.categoryName(category.getCategoryName())
.categoryPriority(category.getCategoryPriority())
.build())
.collect(Collectors.toList());
}

}

Expand Down

0 comments on commit e624919

Please sign in to comment.