From d73700ebe25bef303923dd615dc9cba63ea665d2 Mon Sep 17 00:00:00 2001 From: Ahn Su-gyeong <88311377+ahnsugyeong@users.noreply.github.com> Date: Sat, 10 Feb 2024 02:35:36 +0900 Subject: [PATCH] =?UTF-8?q?[Feat/#94]=20=EA=B3=84=EC=A0=95=20=EC=B9=B4?= =?UTF-8?q?=ED=85=8C=EA=B3=A0=EB=A6=AC=20=EB=AA=A9=EB=A1=9D=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20API=20=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95=20(#9?= =?UTF-8?q?5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CategoryController.java | 5 +- .../category/converter/CategoryConverter.java | 48 +++++++++++++------ .../service/CategoryQueryService.java | 3 ++ .../service/CategoryQueryServiceImpl.java | 8 ++++ .../TransactionCommandServiceImpl.java | 4 +- 5 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/friends/easybud/category/controller/CategoryController.java b/src/main/java/com/friends/easybud/category/controller/CategoryController.java index cc97d35..a4a72ac 100644 --- a/src/main/java/com/friends/easybud/category/controller/CategoryController.java +++ b/src/main/java/com/friends/easybud/category/controller/CategoryController.java @@ -85,7 +85,10 @@ public ResponseDto deleteTertiaryCategory(@AuthMember Member member, @GetMapping public ResponseDto getAccountCategories(@AuthMember Member member) { return ResponseDto.onSuccess( - CategoryConverter.toAccountCategoryListDto(categoryQueryService.getTertiaryCategories(member))); + CategoryConverter.toAccountCategoryListDto( + categoryQueryService.getSecondaryCategories(), + categoryQueryService.getTertiaryCategories(member) + )); } } diff --git a/src/main/java/com/friends/easybud/category/converter/CategoryConverter.java b/src/main/java/com/friends/easybud/category/converter/CategoryConverter.java index 4c7355d..d7a5a0f 100644 --- a/src/main/java/com/friends/easybud/category/converter/CategoryConverter.java +++ b/src/main/java/com/friends/easybud/category/converter/CategoryConverter.java @@ -1,33 +1,51 @@ package com.friends.easybud.category.converter; +import com.friends.easybud.category.domain.SecondaryCategory; import com.friends.easybud.category.domain.TertiaryCategory; import com.friends.easybud.category.dto.CategoryResponse.AccountCategoryDto; import com.friends.easybud.category.dto.CategoryResponse.AccountCategoryListDto; +import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class CategoryConverter { - public static AccountCategoryDto toAccountCategoryDto(TertiaryCategory tertiaryCategory) { - return AccountCategoryDto.builder() - .primaryCategoryId(tertiaryCategory.getSecondaryCategory().getPrimaryCategory().getId()) - .primaryCategoryContent(tertiaryCategory.getSecondaryCategory().getPrimaryCategory().getContent()) - .secondaryCategoryId(tertiaryCategory.getSecondaryCategory().getId()) - .secondaryCategoryContent(tertiaryCategory.getSecondaryCategory().getContent()) - .tertiaryCategoryId(tertiaryCategory.getId()) - .tertiaryCategoryContent(tertiaryCategory.getContent()) - .isDefault(tertiaryCategory.getIsDefault()) - .build(); + public static AccountCategoryDto toAccountCategoryDto(SecondaryCategory secondaryCategory, + TertiaryCategory tertiaryCategory) { + AccountCategoryDto.AccountCategoryDtoBuilder builder = AccountCategoryDto.builder() + .primaryCategoryId(secondaryCategory.getPrimaryCategory().getId()) + .primaryCategoryContent(secondaryCategory.getPrimaryCategory().getContent()) + .secondaryCategoryId(secondaryCategory.getId()) + .secondaryCategoryContent(secondaryCategory.getContent()); + + if (tertiaryCategory != null) { + builder.tertiaryCategoryId(tertiaryCategory.getId()) + .tertiaryCategoryContent(tertiaryCategory.getContent()) + .isDefault(tertiaryCategory.getIsDefault()); + } + + return builder.build(); } - public static AccountCategoryListDto toAccountCategoryListDto(List tertiaryCategories) { - List accountCategoryDtos = tertiaryCategories.stream() - .map(CategoryConverter::toAccountCategoryDto) - .collect(Collectors.toList()); + public static AccountCategoryListDto toAccountCategoryListDto(List secondaryCategories, + List tertiaryCategories) { + List accountCategoryDtos = new ArrayList<>(); + + for (SecondaryCategory secondaryCategory : secondaryCategories) { + List relatedTertiaryCategories = tertiaryCategories.stream() + .filter(tc -> tc.getSecondaryCategory().equals(secondaryCategory)) + .collect(Collectors.toList()); + + if (relatedTertiaryCategories.isEmpty()) { + accountCategoryDtos.add(toAccountCategoryDto(secondaryCategory, null)); + } else { + relatedTertiaryCategories.forEach(tertiaryCategory -> + accountCategoryDtos.add(toAccountCategoryDto(secondaryCategory, tertiaryCategory))); + } + } return AccountCategoryListDto.builder() .accountCategories(accountCategoryDtos) .build(); } - } diff --git a/src/main/java/com/friends/easybud/category/service/CategoryQueryService.java b/src/main/java/com/friends/easybud/category/service/CategoryQueryService.java index dd38f00..20ac019 100644 --- a/src/main/java/com/friends/easybud/category/service/CategoryQueryService.java +++ b/src/main/java/com/friends/easybud/category/service/CategoryQueryService.java @@ -1,5 +1,6 @@ package com.friends.easybud.category.service; +import com.friends.easybud.category.domain.SecondaryCategory; import com.friends.easybud.category.domain.TertiaryCategory; import com.friends.easybud.member.domain.Member; import java.util.List; @@ -8,4 +9,6 @@ public interface CategoryQueryService { List getTertiaryCategories(Member member); + List getSecondaryCategories(); + } diff --git a/src/main/java/com/friends/easybud/category/service/CategoryQueryServiceImpl.java b/src/main/java/com/friends/easybud/category/service/CategoryQueryServiceImpl.java index d7002f5..20c62b9 100644 --- a/src/main/java/com/friends/easybud/category/service/CategoryQueryServiceImpl.java +++ b/src/main/java/com/friends/easybud/category/service/CategoryQueryServiceImpl.java @@ -1,6 +1,8 @@ package com.friends.easybud.category.service; +import com.friends.easybud.category.domain.SecondaryCategory; import com.friends.easybud.category.domain.TertiaryCategory; +import com.friends.easybud.category.repository.SecondaryCategoryRepository; import com.friends.easybud.category.repository.TertiaryCategoryRepository; import com.friends.easybud.member.domain.Member; import java.util.List; @@ -13,6 +15,7 @@ @Service public class CategoryQueryServiceImpl implements CategoryQueryService { + private final SecondaryCategoryRepository secondaryCategoryRepository; private final TertiaryCategoryRepository tertiaryCategoryRepository; @Override @@ -20,4 +23,9 @@ public List getTertiaryCategories(Member member) { return tertiaryCategoryRepository.findByMemberIdOrIsDefaultTrue(member.getId()); } + @Override + public List getSecondaryCategories() { + return secondaryCategoryRepository.findAll(); + } + } diff --git a/src/main/java/com/friends/easybud/transaction/service/TransactionCommandServiceImpl.java b/src/main/java/com/friends/easybud/transaction/service/TransactionCommandServiceImpl.java index 0ad9dc7..af74df0 100644 --- a/src/main/java/com/friends/easybud/transaction/service/TransactionCommandServiceImpl.java +++ b/src/main/java/com/friends/easybud/transaction/service/TransactionCommandServiceImpl.java @@ -55,10 +55,12 @@ private void createAndSaveAccount(AccountCreateDto accountDto, Transaction trans Card card = cardRepository.findById(accountDto.getCardId()) .orElseThrow(() -> new GeneralException(ErrorStatus.CARD_NOT_FOUND)); account = buildAccount(accountDto, card, null); - } else { + } else if (accountDto.getTertiaryCategoryId() != null) { TertiaryCategory tertiaryCategory = tertiaryCategoryRepository.findById(accountDto.getTertiaryCategoryId()) .orElseThrow(() -> new GeneralException(ErrorStatus.TERTIARY_CATEGORY_NOT_FOUND)); account = buildAccount(accountDto, null, tertiaryCategory); + } else { + account = buildAccount(accountDto, null, null); } account.addTransaction(transaction);