Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: 카테고리 API 로직 수정 #101

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import zip.ootd.ootdzip.category.data.CategoryRes;
import zip.ootd.ootdzip.category.data.CategorySearch;
import zip.ootd.ootdzip.category.data.CategoryType;
import zip.ootd.ootdzip.category.service.CategoryService;
import zip.ootd.ootdzip.common.response.ApiResponse;

Expand All @@ -23,9 +21,9 @@ public class CategoryController {

private final CategoryService categoryService;

@GetMapping("/")
public ApiResponse<List<CategoryRes>> getCategories(@RequestParam CategoryType categoryType,
@RequestParam Long parentCategoryId) {
return new ApiResponse<>(categoryService.getCategories(new CategorySearch(categoryType, parentCategoryId)));
@GetMapping("")
@Operation(summary = "카테고리 조회 API", description = "모든 카테고리 조회하는 API")
public ApiResponse<List<CategoryRes>> getCategories() {
return new ApiResponse<>(categoryService.getCategories());
}
}
47 changes: 40 additions & 7 deletions src/main/java/zip/ootd/ootdzip/category/data/CategoryRes.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,54 @@
package zip.ootd.ootdzip.category.data;

import lombok.Data;
import java.util.List;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import zip.ootd.ootdzip.category.domain.Category;

@Data
@Getter
@NoArgsConstructor
public class CategoryRes {

private Long id;

private String name;

private CategoryType type;
private List<DetailCategory> detailCategories;

@Builder
private CategoryRes(Long id, String name, List<DetailCategory> detailCategories) {
this.id = id;
this.name = name;
this.detailCategories = detailCategories;
}

public static CategoryRes of(Category largeCategory, List<Category> detailCategories) {
return CategoryRes.builder()
.id(largeCategory.getId())
.name(largeCategory.getName())
.detailCategories(detailCategories.stream().map(DetailCategory::of).toList())
.build();
}

static class DetailCategory {

private final Long id;

private final String name;

public CategoryRes(Category category) {
@Builder
private DetailCategory(Long id, String name) {
this.id = id;
this.name = name;
}

this.id = category.getId();
this.name = category.getName();
this.type = category.getType();
public static DetailCategory of(Category category) {
return DetailCategory.builder()
.id(category.getId())
.name(category.getName())
.build();
}
}
}
13 changes: 0 additions & 13 deletions src/main/java/zip/ootd/ootdzip/category/data/CategorySearch.java

This file was deleted.

23 changes: 23 additions & 0 deletions src/main/java/zip/ootd/ootdzip/category/domain/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,27 @@ public class Category {
@JoinColumn(name = "parent_id", nullable = true)
private Category parentCategory;

@Builder
private Category(String name, CategoryType type, Category parentCategory) {
this.name = name;
this.type = type;
this.parentCategory = parentCategory;
}

public static Category createLargeCategoryBy(String name) {
return Category.builder()
.name(name)
.type(CategoryType.LargeCategory)
.parentCategory(null)
.build();
}

public static Category createDetailCategoryBy(String name, Category parentCategory) {
return Category.builder()
.name(name)
.type(CategoryType.DetailCategory)
.parentCategory(parentCategory)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,50 +1,38 @@
package zip.ootd.ootdzip.category.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import lombok.RequiredArgsConstructor;
import zip.ootd.ootdzip.category.data.CategoryRes;
import zip.ootd.ootdzip.category.data.CategorySearch;
import zip.ootd.ootdzip.category.data.CategoryType;
import zip.ootd.ootdzip.category.domain.Category;
import zip.ootd.ootdzip.category.repository.CategoryRepository;
import zip.ootd.ootdzip.common.exception.CustomException;
import zip.ootd.ootdzip.common.exception.code.ErrorCode;

@Service
@RequiredArgsConstructor
public class CategoryService {

private final CategoryRepository categoryRepository;

public List<CategoryRes> getCategories(CategorySearch request) {
//TODO : 페이징 넣을 지 확인 필요
List<Category> findCategories;
public List<CategoryRes> getCategories() {
List<Category> findCategories = categoryRepository.findAll();
List<CategoryRes> result = new ArrayList<>();

// 대분류 카테고리 조회 시 모든 대분류 카테고리 조회
if (request.getCategoryType().equals(CategoryType.LargeCategory)) {
findCategories = categoryRepository.findCategoriesByType(request.getCategoryType());
return findCategories.stream()
.map(CategoryRes::new)
for (Category parentCategory : findCategories.stream()
.filter(x -> x.getType().equals(CategoryType.LargeCategory))
.toList()) {
List<Category> detailCategories = findCategories.stream()
.filter(x -> x.getParentCategory() != null
&& x.getParentCategory().getId().equals(parentCategory.getId()))
.toList();
}

// 소분류 카테고리 조회 시 전달받은 부모 카테고리의 하위 카테고리 조회
Category parentCategory = categoryRepository.findById(request.getParentCategoryId())
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_ERROR));

findCategories = categoryRepository.findCategoriesByParentCategoryAndType(parentCategory,
request.getCategoryType());

if (findCategories == null || findCategories.isEmpty()) {
throw new CustomException(ErrorCode.NOT_FOUND_ERROR);
result.add(CategoryRes.of(parentCategory, detailCategories));
}

return findCategories.stream()
.map(CategoryRes::new)
.toList();
return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package zip.ootd.ootdzip.category.service;

import static org.assertj.core.api.Assertions.*;

import java.util.List;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import zip.ootd.ootdzip.IntegrationTestSupport;
import zip.ootd.ootdzip.category.data.CategoryRes;
import zip.ootd.ootdzip.category.domain.Category;
import zip.ootd.ootdzip.category.repository.CategoryRepository;

class CategoryServiceTest extends IntegrationTestSupport {

@Autowired
private CategoryRepository categoryRepository;

@Autowired
private CategoryService categoryService;

@DisplayName("상위 카테고리 ID로 카테고리를 조회한다.")
@Test
void getCategories() {
// given
Category parentCategory = Category.createLargeCategoryBy("부모카테고리1");
Category savedParentCategory = categoryRepository.save(parentCategory);

Category detailCategory = Category.createDetailCategoryBy("하위카테고리1", savedParentCategory);
Category savedDetailCategory = categoryRepository.save(detailCategory);

// when
List<CategoryRes> result = categoryService.getCategories();

//then
assertThat(result).hasSize(1)
.extracting("id", "name")
.contains(tuple(savedParentCategory.getId(), savedParentCategory.getName()));

assertThat(result.get(0).getDetailCategories()).hasSize(1)
.extracting("id", "name")
.contains(tuple(savedDetailCategory.getId(), savedDetailCategory.getName()));

}

}