Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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 @@ -18,7 +18,7 @@
@Tag(name = "05. Admin")
@WebAdapter
@RequiredArgsConstructor
@RequestMapping("/api/management")
@RequestMapping("/api/managements")
public class AddCategoryController {
private final AddCategoryUsecase addCategoryUsecase;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@Tag(name = "05. Admin")
@WebAdapter
@RequiredArgsConstructor
@RequestMapping("/api/management")
@RequestMapping("/api/managements")
public class DeleteCategoryController {
private final DeleteCategoryUsecase deleteCategoryUsecase;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class FindCategoryController {

@Operation(summary = "모든 카테고리 조회")
@GetMapping("/category")
public ResponseEntity<FindAllCategoryResponse> findAllCategory() {
public ResponseEntity<List<FindAllCategoryResponse>> findAllCategory() {
return ResponseEntity.ok(findAllCategoryUsecase.findAllCategory());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@Tag(name = "05. Admin")
@WebAdapter
@RequiredArgsConstructor
@RequestMapping("/api/management")
@RequestMapping("/api/managements")
public class UpdateCategoryController {
private final UpdateCategoryUsecase updateCategoryUsecase;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import java.util.List;

public record FindAllCategoryResponse(
List<FindMainCategoryResponse> mainCategory,
Long id,
String name,
String code,
List<FindSubCategoryResponse> subCategory
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
public class CategoryResponseMapper {

public static FindAllCategoryResponse toFindAllCategoryResponse(
List<FindMainCategoryResponse> mainCategoryResponses,
Long id,
String name,
String code,
List<FindSubCategoryResponse> subCategoryResponses) {
return new FindAllCategoryResponse(mainCategoryResponses, subCategoryResponses);
return new FindAllCategoryResponse(id, name, code, subCategoryResponses);
}

public static FindMainCategoryResponse toFindMainCategoryResponse(Category category) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import clap.server.adapter.inbound.web.dto.admin.FindAllCategoryResponse;

import java.util.List;

public interface FindAllCategoryUsecase {
FindAllCategoryResponse findAllCategory();
List<FindAllCategoryResponse> findAllCategory();
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package clap.server.application.service.admin;

import clap.server.application.port.inbound.admin.DeleteCategoryUsecase;
import clap.server.application.port.outbound.member.LoadMemberPort;
import clap.server.application.port.inbound.domain.MemberService;
import clap.server.application.port.outbound.task.LoadCategoryPort;
import clap.server.common.annotation.architecture.ApplicationService;
import clap.server.domain.model.member.Member;
import clap.server.exception.ApplicationException;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

import static clap.server.exception.code.MemberErrorCode.ACTIVE_MEMBER_NOT_FOUND;
import static clap.server.exception.code.TaskErrorCode.CATEGORY_NOT_FOUND;

@ApplicationService
@RequiredArgsConstructor
public class DeleteCategoryService implements DeleteCategoryUsecase {
private final LoadCategoryPort loadCategoryPort;
private final LoadMemberPort loadMemberPort;
private final MemberService memberService;

@Override
@Transactional
public void deleteCategory(Long adminId, Long categoryId) {
Member admin = loadMemberPort.findActiveMemberById(adminId).orElseThrow(() -> new ApplicationException(ACTIVE_MEMBER_NOT_FOUND));
Member admin = memberService.findActiveMember(adminId);
loadCategoryPort.findById(categoryId)
.orElseThrow(() -> new ApplicationException(CATEGORY_NOT_FOUND))
.deleteCategory(admin);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package clap.server.application.service.admin;

import clap.server.adapter.inbound.web.dto.admin.FindAllCategoryResponse;
import clap.server.adapter.inbound.web.dto.admin.FindMainCategoryResponse;
import clap.server.adapter.inbound.web.dto.admin.FindSubCategoryResponse;
import clap.server.application.mapper.response.CategoryResponseMapper;
import clap.server.application.port.inbound.admin.FindAllCategoryUsecase;
import clap.server.application.port.outbound.task.LoadCategoryPort;
import clap.server.common.annotation.architecture.ApplicationService;
import clap.server.domain.model.task.Category;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

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

@ApplicationService
Expand All @@ -20,16 +19,23 @@ public class FindAllCategoryService implements FindAllCategoryUsecase {

@Override
@Transactional(readOnly = true)
public FindAllCategoryResponse findAllCategory() {
List<FindMainCategoryResponse> mainCategoryResponses = new ArrayList<>();
List<FindSubCategoryResponse> subCategoryResponses = new ArrayList<>();
loadCategoryPort.findAll().forEach(category -> {
if (category.getMainCategory() == null) {
mainCategoryResponses.add(CategoryResponseMapper.toFindMainCategoryResponse(category));
} else {
subCategoryResponses.add(CategoryResponseMapper.toFindSubCategoryResponse(category));
}
});
return CategoryResponseMapper.toFindAllCategoryResponse(mainCategoryResponses, subCategoryResponses);
public List<FindAllCategoryResponse> findAllCategory() {
List<Category> categories = loadCategoryPort.findAll();
return categories.stream().filter(category -> category.getMainCategory() == null)
.map(parent -> CategoryResponseMapper.toFindAllCategoryResponse(
parent.getCategoryId(),
parent.getName(),
parent.getCode(),
getSubCategories(categories, parent) // 2차 카테고리 리스트 변환
))
.toList();
}

private List<FindSubCategoryResponse> getSubCategories(List<Category> categories, Category parent) {
return categories.stream()
.filter(category -> category.getMainCategory() != null &&
category.getMainCategory().getCategoryId().equals(parent.getCategoryId())) // 부모가 같은 것들 필터링
.map(CategoryResponseMapper::toFindSubCategoryResponse)
.toList();
}
}